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

Const field not being detected when JSON schema $defs are used to generate code in pydantic_V2 #1951

Open
KasuniWijeratne opened this issue May 9, 2024 · 0 comments

Comments

@KasuniWijeratne
Copy link

Describe the bug
When we use schema $def tag and define the subschemas separately if the first field in the object has a const value, the value is not detected but the object's name appears in the literal of the generated code.

To Reproduce
Create a schema such as given below.
Then generate the pydantic_v2 model using a command as follows

Example schema:

{
  "title": "fruits",
  "type": "object",
  "properties": {
    "families": {
      "type": "object",
      "discriminator": { "propertyName": "colour" },
      "oneOf": [{ "$ref": "#/$defs/apple" }, 
      { "$ref": "#/$defs/orange" }]
    }
  },
  "$defs": {
    "apple": {
      "type": "object",
      "properties": {
        "flavor": {
          "const": "sweet"
        }
      }
    },
    "orange": {
      "type": "object",
      "properties": {
        "flavor": {
          "const": "sour"
        }
      }
    }
  }
}

Used command line:

$ datamodel-codegen  --input schema.json --input-file-type jsonschema --output datamodel.py --output-model-type pydantic_v2.BaseModel

Expected behaviour
Expected

class Fruits(BaseModel):
    flavour: Literal['sweet'] = Field(
        ...
    )

What I got

class Fruits(BaseModel):
    flavour: Literal['apple'] = Field(
        ...
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant