Comments (4)
I think this issue is related to #1620
from oapi-codegen.
I found a temporary workaround by using a user template. In the strict-interface
template, check if the response's OpenAPI schema is oneOf when the contentType is JSON.
curl https://raw.githubusercontent.com/oapi-codegen/oapi-codegen/v2.3.0/pkg/codegen/templates/strict/strict-interface.tmpl -o strict-interface.tmpl
patch strict-interface.tmpl -o strict-interface.patched.tmpl < strict-interface.patch
74a75,81
> {{if eq .NameTagOrContentType "JSON" -}}
> {{if or (ne 0 (len .Schema.OAPISchema.OneOf)) (ne 0 (len .Schema.OAPISchema.AnyOf)) (ne 0 (len .Schema.OAPISchema.AllOf)) -}}
> func (response {{$receiverTypeName}}) MarshalJSON() ([]byte, error) {
> return response.union.MarshalJSON()
> }
> {{end -}}
> {{end -}}
config.yaml
# yaml-language-server: $schema=https://raw.githubusercontent.com/deepmap/oapi-codegen/HEAD/configuration-schema.json
package: "api"
generate:
echo-server: true
models: true
strict-server: true
embedded-spec: false
output: api.gen.go
output-options:
nullable-type: true
initialism-overrides: true
user-templates:
strict/strict-interface.tmpl: ./strict-interface.patched.tmpl
from oapi-codegen.
This issue is also discussed here #970. It comes with a PR #1231 but I'm not sure if it should be merged due to my final comment in the issue
from oapi-codegen.
Also discussed under #970 (comment)
I seems that there're two ways to fix this bug.
from oapi-codegen.
Related Issues (20)
- feat: generate union types like Speakeasy does
- feat: allow not generating union types' `As...` or `From...` HOT 2
- Gin has no error types HOT 2
- Generating *interface{} in additionalProperties HOT 2
- Import Mappings not referencing correct variables. HOT 5
- Error output for "map key not found" errors missing trailing newline.
- docs(schema): add a deprecation offiically for `circular-reference-limit`
- Invalid code generated HOT 1
- Add ability to Include both `json` and `yaml` tags to the generated structs
- StrictMiddleware: Custom Error Handler has no access to modified context
- undefined: openapi3.CircularReferenceCounter with [email protected] HOT 2
- Support defaulting query parameter values when not supplied, and default is set.
- oapi-codegen is a program, not an importable package HOT 10
- Can't disable all generators HOT 4
- field lost in nested allof oneof
- bug: oepnAPI generate function does not write http.ResponseWriter correctly HOT 1
- CreateBlueprintResponse redeclared in this block HOT 1
- stdhttp: Receive a mux as interface instead of *http.ServeMux HOT 4
- Generate a slice of "valid" values for an enum
- Duplicate type name from automatically named property of schema
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oapi-codegen.