modelina icon indicating copy to clipboard operation
modelina copied to clipboard

Weird behavior with self referencing models not create correct types

Open jonaslagoni opened this issue 2 years ago • 1 comments

This is part of a larger schema, but managed to narrow it down to the following small example:

{
  "allOf": [
    {
      "$ref": "#/definitions/json-schema-draft-07-schema"
    },
    {
      "properties": {
        "items": {
          "anyOf": [
            {
              "$ref": "#"
            },
            {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#"
              }
            }
          ],
          "default": {}
        }
      }
    }
  ],
  "definitions": {
    "json-schema-draft-07-schema": {
      "title": "Core schema meta-schema",
      "definitions": {
        "schemaArray": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/json-schema-draft-07-schema"
          }
        }
      },
      "type": ["object", "boolean"],
      "properties": {
        "additionalItems": {
          "$ref": "#/definitions/json-schema-draft-07-schema"
        },
        "items": {
          "anyOf": [
            {
              "$ref": "#/definitions/json-schema-draft-07-schema"
            },
            {
              "$ref": "#/definitions/json-schema-draft-07-schema/definitions/schemaArray"
            }
          ],
          "default": true
        }
      },
      "default": true
    }
  }
}

Which generates the following Python code for the root object, notice the type for _items:

from CoreSchemaMetaMinusSchemaObject import CoreSchemaMetaMinusSchemaObject
from typing import List, Any, Dict
class RootObject: 
  def __init__(self, input: Dict):
    if 'additional_items' in input:
      self._additional_items: CoreSchemaMetaMinusSchemaObject | bool = input['additional_items']
    if 'items' in input:
      self._items:  | List[] = input['items']
    if 'additional_properties' in input:
      self._additional_properties: dict[str, Any] = input['additional_properties']

  @property
  def additional_items(self) -> CoreSchemaMetaMinusSchemaObject | bool:
    return self._additional_items
  @additional_items.setter
  def additional_items(self, additional_items: CoreSchemaMetaMinusSchemaObject | bool):
    self._additional_items = additional_items

  @property
  def items(self) ->  | List[]:
    return self._items
  @items.setter
  def items(self, items:  | List[]):
    self._items = items

  @property
  def additional_properties(self) -> dict[str, Any]:
    return self._additional_properties
  @additional_properties.setter
  def additional_properties(self, additional_properties: dict[str, Any]):
    self._additional_properties = additional_properties

Everything works fine if we remove additionalItems from json-schema-draft-07-schema...

{
  "allOf": [
    {
      "$ref": "#/definitions/json-schema-draft-07-schema"
    },
    {
      "properties": {
        "items": {
          "anyOf": [
            {
              "$ref": "#"
            },
            {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#"
              }
            }
          ],
          "default": {}
        }
      }
    }
  ],
  "definitions": {
    "json-schema-draft-07-schema": {
      "title": "Core schema meta-schema",
      "definitions": {
        "schemaArray": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/json-schema-draft-07-schema"
          }
        }
      },
      "type": ["object", "boolean"],
      "properties": {
        "items": {
          "anyOf": [
            {
              "$ref": "#/definitions/json-schema-draft-07-schema"
            },
            {
              "$ref": "#/definitions/json-schema-draft-07-schema/definitions/schemaArray"
            }
          ],
          "default": true
        }
      },
      "default": true
    }
  }
}
from CoreSchemaMetaMinusSchemaObject import CoreSchemaMetaMinusSchemaObject
from typing import Any, List, Dict
class RootObject: 
  def __init__(self, input: Dict):
    if 'items' in input:
      self._items: CoreSchemaMetaMinusSchemaObject | bool | List[CoreSchemaMetaMinusSchemaObject | bool] = input['items']
    if 'additional_properties' in input:
      self._additional_properties: dict[str, Any] = input['additional_properties']

  @property
  def items(self) -> CoreSchemaMetaMinusSchemaObject | bool | List[CoreSchemaMetaMinusSchemaObject | bool]:
    return self._items
  @items.setter
  def items(self, items: CoreSchemaMetaMinusSchemaObject | bool | List[CoreSchemaMetaMinusSchemaObject | bool]):
    self._items = items

  @property
  def additional_properties(self) -> dict[str, Any]:
    return self._additional_properties
  @additional_properties.setter
  def additional_properties(self, additional_properties: dict[str, Any]):
    self._additional_properties = additional_properties

The same problem is across multiple languages, so it has something to do with the interpreter or JSON Schema input processor.

jonaslagoni avatar Mar 15 '24 12:03 jonaslagoni

So...

The underlying problem occurs because of the way we constrain models and their types, basically doing both at the same time.

This means that when we have circular models and they depend on each other, and the type has not been set yet, but we expect it to, we end up with the type having the value ''. I.e. why we end up with self._items: | List[] = input['items'] where the union model that items reference has not been sat when we set the items type.

jonaslagoni avatar Apr 05 '24 11:04 jonaslagoni