0

I'm trying to create a pydantic model of a json schema I'm using with oneOf. The basic schema is as follows:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "type": "object",
    "title": "v2_test",
    "additionalProperties": true,
    "oneOf": [
        {
            "type": "object",
            "properties": {
                "field_1": {
                    "enum": [
                        "response_1"
                    ]
                }
            },
            "additionalProperties": true,
            "oneOf": [
                {
                    "type": "object",
                    "properties": {
                        "field_2": {
                            "enum": [
                                "response_a"
                            ]
                        }
                    },
                    "additionalProperties": true,
                    "required": [
                        "field_2"
                    ]
                }
            ],
            "required": [
                "field_1"
            ]
        },
        {
            "type": "object",
            "properties": {
                "field_1": {
                    "enum": [
                        "response_2"
                    ]
                }
            },
            "additionalProperties": true,
            "oneOf": [
                {
                    "type": "object",
                    "properties": {
                        "field_2": {
                            "enum": [
                                "response_b"
                            ]
                        }
                    },
                    "additionalProperties": true,
                    "required": [
                        "field_2"
                    ]
                },
                {
                    "type": "object",
                    "properties": {
                        "field_2": {
                            "enum": [
                                "response_c"
                            ]
                        }
                    },
                    "additionalProperties": true,
                    "required": [
                        "field_2"
                    ]
                }
            ],
            "required": [
                "field_1"
            ]
        }
    ]
}

However, I get this model with this code datamodel-codegen --input schema.json --input-file-type jsonschema --output model.py:

# generated by datamodel-codegen:
#   filename:  v2_differnet_names_schema.json
#   timestamp: 2022-08-21T09:12:26+00:00

from __future__ import annotations

from enum import Enum
from typing import Union

from pydantic import BaseModel, Extra, Field


class Field2(Enum):
    response_a = 'response_a'


class V2TestItem(BaseModel):
    class Config:
        extra = Extra.allow

    field_2: Field2


class Field21(Enum):
    response_b = 'response_b'


class V2TestItem1(BaseModel):
    class Config:
        extra = Extra.allow

    field_2: Field21


class Field22(Enum):
    response_c = 'response_c'


class V2TestItem2(BaseModel):
    class Config:
        extra = Extra.allow

    field_2: Field22


class V2Test(BaseModel):
    class Config:
        extra = Extra.allow

    __root__: Union[V2TestItem, Union[V2TestItem1, V2TestItem2]] = Field(
        ..., title='v2_test'
    )

As you can see, there is no field 1 with response 1 or response 2.

Thank you for your help!

Cole
  • 116
  • 7
  • Cole ur jsonshema doesn't seem to be correct. Could you tell what cases you handling with the schema so we can confirm if its correct or else create a new one. – Origin Sep 01 '22 at 07:51
  • You may be better off raising an issue on the Pydanitc Github repo than asking here for specific problems like this. – Relequestual Sep 01 '22 at 08:47

0 Answers0