jacksmith15 / statham-schema

Statham is a Python Model Parsing Library for JSON Schema.
https://statham-schema.readthedocs.io/en/latest/
MIT License
39 stars 10 forks source link

Untitled schemas defined in an array are given title Items #29

Closed jacksmith15 closed 4 years ago

jacksmith15 commented 4 years ago

Background

Auto-naming of schemas means that all schemas declared in an array are given the name Items. This naturally leads to conflicts when declaring the classes.

Steps to reproduce

Generate models for the following schema:

title: Autoname
type: object
description: Test schema for checking auto-naming logic for anonymous schemas.
required:
- list_of_strings
- list_of_integers
properties:
  list_of_integers:
    items:
      properties:
        integer_property:
          type: integer
      type: object
    type: array
  list_of_strings:
    items:
      properties:
        string_property:
          type: string
      type: object
    type: array

Expected outcome

Three models are generated, one for the outer schema, and one for each schema with the lists.

Actual outcome

Only two schemas are generated. The schema for the integer wrapping object is overwritten.

Full output

from typing import ClassVar, List, Union

from attr import attrs, attrib
from statham import converters as con, validators as val
from statham.validators import NotPassed

@attrs(kw_only=True)
class Items:
    """items"""

    _required: ClassVar[List[str]] = []

    string_property: Union[str, NotPassed] = attrib(
        validator=[
            val.instance_of(str),
        ],
        default=NotPassed(),
    )

@attrs(kw_only=True)
class Autoname:
    """Test schema for checking auto-naming logic for anonymous schemas."""

    _required: ClassVar[List[str]] = ['list_of_strings', 'list_of_integers']

    list_of_strings: List[Union[Items, NotPassed]] = attrib(
        validator=[
            val.instance_of(list),
        ],
        converter=con.map_instantiate(Items),  # type: ignore
    )
    list_of_integers: List[Union[Items, NotPassed]] = attrib(
        validator=[
            val.instance_of(list),
        ],
        converter=con.map_instantiate(Items),  # type: ignore
    )