Skip to content

Commit

Permalink
Ensure embedded spec contains all necessary OpenAPI documents
Browse files Browse the repository at this point in the history
When using a schema that uses `$ref`s to an external file, although we
generate the mappings appropriately, this then leads to the embedded
spec being misaligned, as it continues to use the `$ref` to that
external file, which then is not available when calling i.e.
`GetSwagger`.

We can take advantage of the `InternalizeRefs` method to perform an
in-place modification to apply those external references into a single,
embeddable spec.

This appears to only work when a full file is referenced, instead of a
path within a file.
  • Loading branch information
jamietanna authored and Jamie Tanna committed Oct 24, 2022
1 parent 45eb6bd commit 8a93137
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
12 changes: 7 additions & 5 deletions internal/test/externalref/externalref.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/test/externalref/object_c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "object",
"properties": {},
"additionalProperties": true
}
6 changes: 3 additions & 3 deletions internal/test/externalref/packageA/externalref.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/test/externalref/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ components:
$ref: ./packageA/spec.yaml#/components/schemas/ObjectA
object_b:
$ref: ./packageB/spec.yaml#/components/schemas/ObjectB
object_c:
$ref: ./object_c.json
3 changes: 3 additions & 0 deletions pkg/codegen/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package codegen
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"fmt"
"text/template"
Expand All @@ -26,6 +27,8 @@ import (
// This generates a gzipped, base64 encoded JSON representation of the
// swagger definition, which we embed inside the generated code.
func GenerateInlinedSpec(t *template.Template, importMapping importMap, swagger *openapi3.T) (string, error) {
// ensure that any external file references are embedded into the embedded spec
swagger.InternalizeRefs(context.Background(), nil)
// Marshal to json
encoded, err := swagger.MarshalJSON()
if err != nil {
Expand Down

0 comments on commit 8a93137

Please sign in to comment.