Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions experimental/examples/callback/treefarm.gen.go

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

22 changes: 11 additions & 11 deletions experimental/examples/petstore-expanded/petstore.gen.go

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

22 changes: 11 additions & 11 deletions experimental/examples/webhook/doorbadge.gen.go

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

24 changes: 12 additions & 12 deletions experimental/internal/codegen/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// generateEmbeddedSpec produces Go code that embeds the raw OpenAPI spec as
// gzip+base64 encoded data, with a public GetSwaggerSpecJSON() function to
// gzip+base64 encoded data, with a public GetOpenAPISpecJSON() function to
// retrieve the decompressed JSON bytes.
func generateEmbeddedSpec(specData []byte) (string, error) {
// Gzip compress
Expand Down Expand Up @@ -43,15 +43,15 @@ func generateEmbeddedSpec(specData []byte) (string, error) {
var b strings.Builder

b.WriteString("// Base64-encoded, gzip-compressed OpenAPI spec.\n")
b.WriteString("var swaggerSpecJSON = []string{\n")
b.WriteString("var openAPISpecJSON = []string{\n")
for _, chunk := range chunks {
fmt.Fprintf(&b, "\t%q,\n", chunk)
}
b.WriteString("}\n\n")

b.WriteString("// decodeSwaggerSpec decodes and decompresses the embedded spec.\n")
b.WriteString("func decodeSwaggerSpec() ([]byte, error) {\n")
b.WriteString("\tjoined := strings.Join(swaggerSpecJSON, \"\")\n")
b.WriteString("// decodeOpenAPISpec decodes and decompresses the embedded spec.\n")
b.WriteString("func decodeOpenAPISpec() ([]byte, error) {\n")
b.WriteString("\tjoined := strings.Join(openAPISpecJSON, \"\")\n")
b.WriteString("\traw, err := base64.StdEncoding.DecodeString(joined)\n")
b.WriteString("\tif err != nil {\n")
b.WriteString("\t\treturn nil, fmt.Errorf(\"decoding base64: %w\", err)\n")
Expand All @@ -68,24 +68,24 @@ func generateEmbeddedSpec(specData []byte) (string, error) {
b.WriteString("\treturn out.Bytes(), nil\n")
b.WriteString("}\n\n")

b.WriteString("// decodeSwaggerSpecCached returns a closure that caches the decoded spec.\n")
b.WriteString("func decodeSwaggerSpecCached() func() ([]byte, error) {\n")
b.WriteString("// decodeOpenAPISpecCached returns a closure that caches the decoded spec.\n")
b.WriteString("func decodeOpenAPISpecCached() func() ([]byte, error) {\n")
b.WriteString("\tvar cached []byte\n")
b.WriteString("\tvar cachedErr error\n")
b.WriteString("\tvar once sync.Once\n")
b.WriteString("\treturn func() ([]byte, error) {\n")
b.WriteString("\t\tonce.Do(func() {\n")
b.WriteString("\t\t\tcached, cachedErr = decodeSwaggerSpec()\n")
b.WriteString("\t\t\tcached, cachedErr = decodeOpenAPISpec()\n")
b.WriteString("\t\t})\n")
b.WriteString("\t\treturn cached, cachedErr\n")
b.WriteString("\t}\n")
b.WriteString("}\n\n")

b.WriteString("var swaggerSpec = decodeSwaggerSpecCached()\n\n")
b.WriteString("var openAPISpec = decodeOpenAPISpecCached()\n\n")

b.WriteString("// GetSwaggerSpecJSON returns the raw OpenAPI spec as JSON bytes.\n")
b.WriteString("func GetSwaggerSpecJSON() ([]byte, error) {\n")
b.WriteString("\treturn swaggerSpec()\n")
b.WriteString("// GetOpenAPISpecJSON returns the raw OpenAPI spec as JSON bytes.\n")
b.WriteString("func GetOpenAPISpecJSON() ([]byte, error) {\n")
b.WriteString("\treturn openAPISpec()\n")
b.WriteString("}\n")

return b.String(), nil
Expand Down
18 changes: 9 additions & 9 deletions experimental/internal/codegen/inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ func TestGenerateEmbeddedSpec(t *testing.T) {
require.NoError(t, err)

// Should contain the chunked base64 variable
assert.Contains(t, code, "var swaggerSpecJSON = []string{")
assert.Contains(t, code, "var openAPISpecJSON = []string{")

// Should contain the decode function
assert.Contains(t, code, "func decodeSwaggerSpec() ([]byte, error)")
assert.Contains(t, code, "func decodeOpenAPISpec() ([]byte, error)")

// Should contain the cached decode function
assert.Contains(t, code, "func decodeSwaggerSpecCached() func() ([]byte, error)")
assert.Contains(t, code, "func decodeOpenAPISpecCached() func() ([]byte, error)")

// Should contain the public API
assert.Contains(t, code, "func GetSwaggerSpecJSON() ([]byte, error)")
assert.Contains(t, code, "func GetOpenAPISpecJSON() ([]byte, error)")

// Should contain the cached var
assert.Contains(t, code, "var swaggerSpec = decodeSwaggerSpecCached()")
assert.Contains(t, code, "var openAPISpec = decodeOpenAPISpecCached()")
}

func TestGenerateEmbeddedSpecRoundTrip(t *testing.T) {
Expand Down Expand Up @@ -101,8 +101,8 @@ components:
assert.Contains(t, code, "type Pet struct")

// Should contain the embedded spec
assert.Contains(t, code, "GetSwaggerSpecJSON")
assert.Contains(t, code, "swaggerSpecJSON")
assert.Contains(t, code, "GetOpenAPISpecJSON")
assert.Contains(t, code, "openAPISpecJSON")
}

func TestGenerateWithNilSpecData(t *testing.T) {
Expand Down Expand Up @@ -134,6 +134,6 @@ components:
assert.Contains(t, code, "type Pet struct")

// Should NOT contain the embedded spec
assert.NotContains(t, code, "GetSwaggerSpecJSON")
assert.NotContains(t, code, "swaggerSpecJSON")
assert.NotContains(t, code, "GetOpenAPISpecJSON")
assert.NotContains(t, code, "openAPISpecJSON")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Client struct {
// The endpoint of the server conforming to this interface, with scheme,
// https://api.deepmap.com for example. This can contain a path relative
// to the server, such as https://api.deepmap.com/dev-test, and all the
// paths in the swagger spec will be appended to the server.
// paths in the OpenAPI spec will be appended to the server.
Server string

// Doer for performing requests, typically a *http.Client with any
Expand Down

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

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

Loading