Skip to content

Commit 313aa80

Browse files
Warboss-rusilya.bogdanovilya.bogdanov
authored
oneOf/anyOf support (including discriminator) (#471)
* 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>
1 parent 60ca161 commit 313aa80

File tree

7 files changed

+1572
-33
lines changed

7 files changed

+1572
-33
lines changed

README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,21 @@ define types for inner fields which themselves support additionalProperties, and
318318
all of them are tested via the `internal/test/components` schemas and tests. Please
319319
look through those tests for more usage examples.
320320

321+
#### oneOf/anyOf/allOf support
322+
323+
- `oneOf` and `anyOf` are implemented using delayed parsing with the help of `json.RawMessage`.
324+
The following schema will result in a type that has methods such as `AsCat`, `AsDog`, `FromCat`, `FromDog`. If the schema also includes a discriminator the generated code will also have methods such as `Discriminator`, `ValueByDiscriminator` and will force discriminator value in `From` methods.
325+
```yaml
326+
schema:
327+
oneOf:
328+
- $ref: '#/components/schemas/Cat'
329+
- $ref: '#/components/schemas/Dog'
330+
```
331+
- `allOf` is supported, by taking the union of all the fields in all the
332+
component schemas. This is the most useful of these operations, and is
333+
commonly used to merge objects with an identifier, as in the
334+
`petstore-expanded` example.
335+
321336
## Generated Client Boilerplate
322337

323338
Once your server is up and running, you probably want to make requests to it. If
@@ -611,23 +626,6 @@ by comma separating them in the form `key1:value1,key2:value2`.
611626
This code is still young, and not complete, since we're filling it in as we
612627
need it. We've not yet implemented several things:
613628

614-
- `oneOf`, `anyOf` are not supported with strong Go typing. This schema:
615-
616-
schema:
617-
oneOf:
618-
- $ref: '#/components/schemas/Cat'
619-
- $ref: '#/components/schemas/Dog'
620-
621-
will result in a Go type of `interface{}`. It will be up to you
622-
to validate whether it conforms to `Cat` and/or `Dog`, depending on the
623-
keyword. It's not clear if we can do anything much better here given the
624-
limits of Go typing.
625-
626-
`allOf` is supported, by taking the union of all the fields in all the
627-
component schemas. This is the most useful of these operations, and is
628-
commonly used to merge objects with an identifier, as in the
629-
`petstore-expanded` example.
630-
631629
- `patternProperties` isn't yet supported and will exit with an error. Pattern
632630
properties were defined in JSONSchema, and the `kin-openapi` Swagger object
633631
knows how to parse them, but they're not part of OpenAPI 3.0, so we've left

0 commit comments

Comments
 (0)