Special-case nullable schema
This commit is contained in:
parent
50881d18fa
commit
221c18e60a
1 changed files with 35 additions and 0 deletions
|
@ -128,6 +128,37 @@ func noAdditionalProps(schema *jsonschema.Schema) bool {
|
||||||
return schema.AdditionalProperties != nil && schema.AdditionalProperties.IsFalse()
|
return schema.AdditionalProperties != nil && schema.AdditionalProperties.IsFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unwrapNullableSchema unwraps a schema in the form:
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// "oneOf": {
|
||||||
|
// { "type": "null" },
|
||||||
|
// <sub-schema>
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
func unwrapNullableSchema(schema *jsonschema.Schema) (*jsonschema.Schema, bool) {
|
||||||
|
for _, choices := range [][]jsonschema.Schema{schema.AnyOf, schema.OneOf} {
|
||||||
|
if len(choices) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
nullIndex := -1
|
||||||
|
for i, choice := range choices {
|
||||||
|
if len(choice.Type) == 1 && choice.Type[0] == jsonschema.TypeNull {
|
||||||
|
nullIndex = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nullIndex < 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
otherIndex := (nullIndex + 1) % 2
|
||||||
|
return &choices[otherIndex], true
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
func generateSchemaType(schema *jsonschema.Schema, root *jsonschema.Schema, required bool) jen.Code {
|
func generateSchemaType(schema *jsonschema.Schema, root *jsonschema.Schema, required bool) jen.Code {
|
||||||
if schema == nil {
|
if schema == nil {
|
||||||
return jen.Interface()
|
return jen.Interface()
|
||||||
|
@ -143,6 +174,10 @@ func generateSchemaType(schema *jsonschema.Schema, root *jsonschema.Schema, requ
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if subschema, ok := unwrapNullableSchema(schema); ok {
|
||||||
|
return jen.Op("*").Add(generateSchemaType(subschema, root, true))
|
||||||
|
}
|
||||||
|
|
||||||
switch schemaType(schema) {
|
switch schemaType(schema) {
|
||||||
case jsonschema.TypeNull:
|
case jsonschema.TypeNull:
|
||||||
return jen.Struct()
|
return jen.Struct()
|
||||||
|
|
Loading…
Reference in a new issue