Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oneOf/anyOf support (including discriminator) #471

Merged
merged 13 commits into from May 31, 2022

Conversation

Warboss-rus
Copy link
Contributor

@Warboss-rus Warboss-rus commented Oct 26, 2021

Implemented using json.RawMessage to delay parsing to concrete type until one of the getter (As...) methods is called. If the schema has a discriminator and its mapping, then ValueByDiscriminator method can be used to get an empty interface that holds one of the union types. Also, when using setters (From... methods), discriminator value will be filled automatically, according to mapping.
This implementation only supports JSON, so will not work in parameters (body only). Also reverts to generating empty interface if "type" or other special properties are specified (for example, if only required properties differ in each variant) fixed that behavior, will now work properly with fixed properties
Tests for this feature are added to internal/test/components.

@Warboss-rus
Copy link
Contributor Author

Looks like the travis build is failing because lestrrat-go/jwx requires go 1.15, while the travis.yml specifies 1.13. So we have to either downgrade that package to v1.1.2 or bump the required version of go

@jonasrmichel
Copy link

This is great work @Warboss-rus, thank you!

Any plans to support arrays whose contents are oneOf / anyOf?

For example...

{
   "type":"array",
   "items":{
      "oneOf":[
         {
            "$ref":"#/components/schemas/TypeA"
         },
         {
            "$ref":"#/components/schemas/TypeB"
         },
         {
            "$ref":"#/components/schemas/TypeC"
         }
      ]
   }
}

@Warboss-rus
Copy link
Contributor Author

Thanks, added a test for this case, will fix it this week

…g new type for array items with union or additional properties, added some new tests for union, fixed README
@Warboss-rus
Copy link
Contributor Author

I've reworked the code to support oneOf\anyOf along fixed properties, fixed type generation for array items that have additional properties or union parts (thanks @jonasrmichel), added some tests and updated information in README

@jonasrmichel
Copy link

Brilliant work @Warboss-rus, thank you!

@deepmap-marcinr @carmo-evan @Karitham -- It'd be wonderful if these contributions could be merged for the next release. 🙏

@tamj0rd2
Copy link

Nice! Is there a release schedule? Really looking forward to being able to use this

@jonasrmichel
Copy link

Nudge.

@deepmap-marcinr these changes look good to me. They include new functionality, tests, and necessary documentation updates.

What's required for merge approval? It'd be great to see these features in the next minor release.

# Conflicts:
#	go.sum
#	pkg/codegen/templates/templates.gen.go
@reshef-axiom
Copy link

Would appreciate if these changes are merged in the next release.

ilya.bogdanov added 2 commits April 20, 2022 13:25
# Conflicts:
#	internal/test/components/components.gen.go
ilya.bogdanov and others added 5 commits May 3, 2022 12:02
# Conflicts:
#	internal/test/components/components.gen.go
#	pkg/codegen/schema.go
# Conflicts:
#	internal/test/components/components.gen.go
#	pkg/codegen/schema.go
@deepmap-marcinr
Copy link
Contributor

Impressive change, thank you.

@deepmap-marcinr deepmap-marcinr merged commit 313aa80 into deepmap:master May 31, 2022
@Warboss-rus Warboss-rus deleted the union branch June 1, 2022 05:14
adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
* Implemented oneOf/anyOf support

* Fix travis build by regenerating templates.gen.go

* go mod tidy after master merge

* Reworked code to support fixed properties with oneOf, fixed generating new type for array items with union or additional properties, added some new tests for union, fixed README

* Regenerate tests after merge

* Fixed type generation for combinations of oneOf and additionalProperties and allOf

* Regenerate components.gen.go after merge

* Regenerate components.gen.go after merge

Co-authored-by: ilya.bogdanov <ilya.bogdanov@ispringsolutions.com>
Co-authored-by: ilya.bogdanov <ilya.bogdanov@ispring.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants