quicktype
quicktype copied to clipboard
"required": [ ... ] does not have any effect when inside oneOf/anyOf
allOf works fine, though.
Could you give an example, please?
I'l l give a proper example when I get home.
What happens (in python) is that all properties generate with Optional[str]
types.
This is understandable since oneOf is not fully supported (another issue): With oneOf, the user can pass either of the object types. Quicktype translates that to the code that (optionally) accepts all objects' properties.
So instead of "We can accept either instance of A or instance of B" we have "We can (optionally) accept any subset of properties a1,a2,b1,b2".
to summarize this issue: the properties in any schema that appear within an anyOf are treated as optional. this is likely because of union flattening and after patching that out, i get things rendering correctly. below is an example schema that illustrates the issue. the "name" property is required wherever it is used, but it always renders as name?: null | string
:
{
"description": "Example",
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"anyof_array": {
"items": {
"anyOf": [
{
"properties": {
"name": {
"maxLength": 5000,
"default": null,
"type": [
"string",
"null"
]
}
},
"additionalProperties": false,
"type": "object",
"required": [
"name"
]
},
{
"properties": {
"size": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": []
}
]
},
"type": "array"
},
"anyof": {
"anyOf": [
{
"properties": {
"name": {
"maxLength": 5000,
"default": null,
"type": [
"string",
"null"
]
}
},
"additionalProperties": false,
"type": "object",
"required": [
"name"
]
},
{
"properties": {
"size": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": []
}
]
},
"object": {
"properties": {
"name": {
"maxLength": 5000,
"default": null,
"type": [
"string",
"null"
]
}
},
"additionalProperties": false,
"type": "object",
"required": [
"name"
]
}
}
}
in typescript, quicktype renders that as:
export interface Coordinate {
anyof?: Anyof;
anyof_array?: AnyofArray[];
object?: Object;
[property: string]: any;
}
export interface Anyof {
name?: null | string;
size?: string;
}
export interface AnyofArray {
name?: null | string;
size?: string;
}
export interface Object {
name: null | string;
}
after manually applying #1460 i get things that look more correct:
export interface Example_Example {
anyof?: Example_PurpleAnyof | Example_FluffyAnyof;
anyof_array?: Array<Example_PurpleAnyofArray | Example_FluffyAnyofArray>;
object?: Example_Object;
[property: string]: any;
}
export interface Example_PurpleAnyof {
name: null | string;
}
export interface Example_FluffyAnyof {
size?: string;
}
export interface Example_PurpleAnyofArray {
name: null | string;
}
export interface Example_FluffyAnyofArray {
size?: string;
}
export interface Example_Object {
name: null | string;
}