You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+85
Original file line number
Diff line number
Diff line change
@@ -1947,6 +1947,91 @@ If you don't want to do this, an alternate option is to [bundle your multiple Op
1947
1947
1948
1948
Check out [the import-mapping example](examples/import-mapping/) for the full code.
1949
1949
1950
+
## Modifying the input OpenAPI Specification
1951
+
1952
+
Prior to `oapi-codegen` v2.4.0, 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.
1953
+
1954
+
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.
1955
+
1956
+
This would lead to the API consumers needing to vendor the specification from the producer (which is [our recommendation anyway](#https-paths)) and then make any number of local changes to the specification to make it generate code that looks reasonable.
1957
+
1958
+
However, in the case that a consumer would update their specification, they would likely end up with a number of merge conflicts.
1959
+
1960
+
Now, as of `oapi-codegen` v2.4.0, it is now possible to make changes to the input OpenAPI specification _without needing to modify it directly_.
1961
+
1962
+
This takes advantage of the [OpenAPI Overlay specification](https://github.com/OAI/Overlay-Specification), which is a stable specification.
1963
+
1964
+
> [!CAUTION]
1965
+
> Beware! Here (may) be dragons.
1966
+
>
1967
+
> The Overlay specification requires the use of JSON Path, which some users may find difficult to write and/or maintain.
1968
+
>
1969
+
> We still heavily recommend using Overlay functionality, but would like users to be aware of this.
1970
+
>
1971
+
> There is a [proposed modification to the specification](https://github.com/OAI/Overlay-Specification/pull/32) which would relax the need for JSON Path as the targeting mechanism.
1972
+
1973
+
For instance, let's say that we have the following OpenAPI specification, which provides insight into an internal endpoint that we should not be generating any code for (denoted by `x-internal`):
1974
+
1975
+
```yaml
1976
+
openapi: "3.0.0"
1977
+
info:
1978
+
version: 1.0.0
1979
+
title: "Example to indicate how to use the OpenAPI Overlay specification (https://github.com/OAI/Overlay-Specification)"
1980
+
paths:
1981
+
/ping:
1982
+
get:
1983
+
responses:
1984
+
'200':
1985
+
description: pet response
1986
+
content:
1987
+
application/json:
1988
+
schema:
1989
+
$ref: '#/components/schemas/Pong'
1990
+
delete:
1991
+
x-internal: true
1992
+
responses:
1993
+
'202':
1994
+
content: {}
1995
+
```
1996
+
1997
+
If we were to run `oapi-codegen` with out-of-the-box functionality, this would then lead to the DELETE endpoint being generated, which we don't want.
1998
+
1999
+
Instead, we can define the following `overlay.yaml`:
2000
+
2001
+
2002
+
```yaml
2003
+
overlay: 1.0.0
2004
+
info:
2005
+
title: Overlay
2006
+
version: 0.0.0
2007
+
actions:
2008
+
- target: $.paths.*[?(@.x-internal)]
2009
+
description: Remove internal endpoints (noted by x-internal)
2010
+
remove: true
2011
+
- target: $.paths.*.*[?(@.x-internal)]
2012
+
description: Remove internal endpoints (noted by x-internal)
This then completely removes the DELETE endpoint _before_ we even start to parse the specification in `oapi-codegen`, so it's as if your specification was provided without that endpoint.
2032
+
2033
+
Check out [the overlay example](examples/overlay/) for the full code, and some more complex examples.
2034
+
1950
2035
## Generating Nullable types
1951
2036
1952
2037
It's possible that you want to be able to determine whether a field isn't sent, is sent as `null` or has a value.
0 commit comments