Skip to content

Commit 3cf49e0

Browse files
committed
feat: add support for OpenAPI Overlay
In the past, users wishing to override specific configuration, for instance taking advantage of extensions such as `x-go-type` would need to modify the OpenAPI specification they are using. In a lot of cases, this OpenAPI specification would be produced by a different team to the consumers (or even a different company) and so asking them to make changes like this were unreasonable. This would lead to the API consumers needing to vendor the specification from the producer (which is our recommendation anyway) and then make any number of local changes to the specification to make it generate code that looks reasonable. However, in the case that a consumer would update their specification, they would likely end up with a number of merge conflicts. With these changes, it is now possible to make changes to the input OpenAPI specification _without needing to modify it directly_. This takes advantage of the OpenAPI Overlay specification[0], which is a stable specification, and is ready for wider implementation. This uses Speakeasy's implementation, which is noted as an "alpha" implementation. We introduce a new function, `LoadSwaggerWithOverlay`, which performs the wrangling of the Overlay on top of the specification we're pointing to, and make sure we wire in the ability to configure the path, as well as whether we want to relax the "strict" validation the Overlay library provides. Closes oapi-codegen#1553. [0]: https://github.com/OAI/Overlay-Specification
1 parent fa048ac commit 3cf49e0

File tree

18 files changed

+892
-6
lines changed

18 files changed

+892
-6
lines changed

cmd/oapi-codegen/oapi-codegen.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,17 @@ func main() {
281281
return
282282
}
283283

284-
swagger, err := util.LoadSwagger(flag.Arg(0))
284+
overlayOpts := util.LoadSwaggerWithOverlayOpts{
285+
Path: opts.OutputOptions.Overlay.Path,
286+
// default to strict, but can be overridden
287+
Strict: true,
288+
}
289+
290+
if opts.OutputOptions.Overlay.Strict != nil {
291+
overlayOpts.Strict = *opts.OutputOptions.Overlay.Strict
292+
}
293+
294+
swagger, err := util.LoadSwaggerWithOverlay(flag.Arg(0), overlayOpts)
285295
if err != nil {
286296
errExit("error loading swagger spec in %s\n: %s\n", flag.Arg(0), err)
287297
}

configuration-schema.json

+18
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,24 @@
196196
"ToCamelCaseWithDigits",
197197
"ToCamelCaseWithInitialisms"
198198
]
199+
},
200+
"overlay": {
201+
"type": "object",
202+
"description": "Overlay defines configuration for the OpenAPI Overlay (https://github.com/OAI/Overlay-Specification) to manipulate the OpenAPI specification before generation. This allows modifying the specification without needing to apply changes directly to it, making it easier to keep it up-to-date.",
203+
"properties": {
204+
"path": {
205+
"description": "The path to the Overlay file",
206+
"type": "string"
207+
},
208+
"strict": {
209+
"type": "boolean",
210+
"description": "Strict defines whether the Overlay should be applied in a strict way, highlighting any actions that will not take any effect. This can, however, lead to more work when testing new actions in an Overlay, so can be turned off with this setting.",
211+
"default": true
212+
}
213+
},
214+
"required": [
215+
"path"
216+
]
199217
}
200218
}
201219
},

examples/authenticated-api/stdhttp/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
require (
1717
github.com/davecgh/go-spew v1.1.1 // indirect
1818
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
19+
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
1920
github.com/go-openapi/jsonpointer v0.21.0 // indirect
2021
github.com/go-openapi/swag v0.23.0 // indirect
2122
github.com/goccy/go-json v0.10.2 // indirect
@@ -32,6 +33,8 @@ require (
3233
github.com/perimeterx/marshmallow v1.1.5 // indirect
3334
github.com/pkg/errors v0.9.1 // indirect
3435
github.com/pmezard/go-difflib v1.0.0 // indirect
36+
github.com/speakeasy-api/openapi-overlay v0.9.0 // indirect
37+
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
3538
golang.org/x/crypto v0.22.0 // indirect
3639
golang.org/x/mod v0.17.0 // indirect
3740
golang.org/x/text v0.15.0 // indirect

examples/authenticated-api/stdhttp/go.sum

+104
Large diffs are not rendered by default.

examples/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/cloudwego/iasm v0.2.0 // indirect
4242
github.com/davecgh/go-spew v1.1.1 // indirect
4343
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
44+
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
4445
github.com/fatih/structs v1.1.0 // indirect
4546
github.com/flosch/pongo2/v4 v4.0.2 // indirect
4647
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
@@ -90,6 +91,7 @@ require (
9091
github.com/russross/blackfriday/v2 v2.1.0 // indirect
9192
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
9293
github.com/sirupsen/logrus v1.8.1 // indirect
94+
github.com/speakeasy-api/openapi-overlay v0.9.0 // indirect
9395
github.com/tdewolff/minify/v2 v2.12.9 // indirect
9496
github.com/tdewolff/parse/v2 v2.6.8 // indirect
9597
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
@@ -100,6 +102,7 @@ require (
100102
github.com/valyala/tcplisten v1.0.0 // indirect
101103
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
102104
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
105+
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
103106
github.com/yosssi/ace v0.0.5 // indirect
104107
golang.org/x/arch v0.8.0 // indirect
105108
golang.org/x/crypto v0.23.0 // indirect

examples/go.sum

+90-2
Large diffs are not rendered by default.

examples/minimal-server/stdhttp/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ replace github.com/oapi-codegen/oapi-codegen/v2 => ../../../
77
require github.com/oapi-codegen/oapi-codegen/v2 v2.0.0-00010101000000-000000000000
88

99
require (
10+
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
1011
github.com/getkin/kin-openapi v0.127.0 // indirect
1112
github.com/go-openapi/jsonpointer v0.21.0 // indirect
1213
github.com/go-openapi/swag v0.23.0 // indirect
@@ -15,6 +16,8 @@ require (
1516
github.com/mailru/easyjson v0.7.7 // indirect
1617
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
1718
github.com/perimeterx/marshmallow v1.1.5 // indirect
19+
github.com/speakeasy-api/openapi-overlay v0.9.0 // indirect
20+
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
1821
golang.org/x/mod v0.17.0 // indirect
1922
golang.org/x/text v0.15.0 // indirect
2023
golang.org/x/tools v0.21.0 // indirect

0 commit comments

Comments
 (0)