Skip to content

oneOf/anyOf support (including discriminator)#471

Merged
deepmap-marcinr merged 13 commits intooapi-codegen:masterfrom
ispringtech:union
May 31, 2022
Merged

oneOf/anyOf support (including discriminator)#471
deepmap-marcinr merged 13 commits intooapi-codegen:masterfrom
ispringtech:union

Conversation

@Warboss-rus
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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
Copy Markdown

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

@jonasrmichel
Copy link
Copy Markdown

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
Copy Markdown

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
Comment thread pkg/codegen/schema.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
Copy Markdown
Contributor

Impressive change, thank you.

@deepmap-marcinr deepmap-marcinr merged commit 313aa80 into oapi-codegen: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>
adrianpk added a commit to foorester/oapi-codegen that referenced this pull request May 31, 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.

6 participants