Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behavior with self referencing models not create correct types #1903

Closed
jonaslagoni opened this issue Mar 15, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@jonaslagoni
Copy link
Member

jonaslagoni commented Mar 15, 2024

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
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant