quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

"required": [ ... ] does not have any effect when inside oneOf/anyOf

Open Ark-kun opened this issue 5 years ago • 3 comments

allOf works fine, though.

Ark-kun avatar Oct 31 '18 06:10 Ark-kun

Could you give an example, please?

schani avatar Oct 31 '18 13:10 schani

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".

Ark-kun avatar Oct 31 '18 21:10 Ark-kun

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;
}

cmawhorter avatar Jan 29 '24 21:01 cmawhorter