Repository navigation
Description
Considering the following schema:
FooProperties:
type: object
properties:
uuid:
type: string
format: uuid
readOnly: true
firstname:
type: string
lastname:
type: string
Foo:
allOf:
- $ref: "#/components/schemas/FooProperties"
- type: object
required:
- uuid
- firstname
- lastname
NewFoo:
allOf:
- $ref: "#/components/schemas/FooProperties"
- type: object
required:
- firstnameAccording to the specs, also confirmed in the https://editor.swagger.io/, and swagger-api/swagger-editor#1212, NewFoo should have firstname as required, and Foo should have all properties as required.
The validation works as expected.
However, the resulting objects in Go are:
type Foo struct {
// Embedded struct due to allOf(#/components/schemas/FooProperties)
FooProperties `yaml:",inline"`
// Embedded fields due to inline allOf schema
}
// FooProperties defines model for FooProperties.
type FooProperties struct {
Firstname *string `json:"firstname,omitempty"`
Lastname *string `json:"lastname,omitempty"`
Uuid *string `json:"uuid,omitempty"`
}
// NewFoo defines model for NewFoo.
type NewFoo struct {
// Embedded struct due to allOf(#/components/schemas/FooProperties)
FooProperties `yaml:",inline"`
// Embedded fields due to inline allOf schema
}Because of the struct embedding, the properties are all pointers instead of values (like when using "required" directly in objects).
Now, because the validation works as expected, this might not be an issue at all. I wonder however if in this case, if the generated code should embed FooProperties as is, or if a new struct should be generated per "merge".
This has been discussed, and sometimes fixed, in several other projects:
- Can optional properties be made required when "allOf" and "discriminator" are used? OAI/OpenAPI-Specification#1870
- Required properties on an extended schema are not handled expectedly cyclosproject/ng-openapi-gen#108
- Required properties in the models with inheritance sylvainlaurent/swagger-validator-maven-plugin#19
- Making fields required during use of allOf acacode/swagger-typescript-api#275
- Failure to correctly validate required in allOf python-openapi/openapi-schema-validator#15
Activity
[-]Required inheritance missing using allOf[/-][+]Required inheritance and generated objects when using allOf[/+]florentchauveau commentedon Apr 20, 2022
Maybe we can add a "merge strategy" option? Either embed, or create a new struct with values (according to the required fields).
Swiftwork commentedon Apr 21, 2022
Thought I would share my temporary and convoluted solution using JavaScript to "transpile" a schema by flattening allOf and dereferencing objects with a custom
x-derefattribute. I don't suggest you use this method but it might be hint as to how you can have both embedded and dereferenced types.Example:
Rewrite allOf support
Rewrite allOf support
Rewrite allOf support