clement boutaric
clement boutaric

Reputation: 11

datamodel-code-generator can't handle JSON Compound Schema Document

I am trying to turn a JSON Schema into a pydantic model using datamodel-code-generator. The Schema is a Compound Schema Document, meaning that $id is used in a subschema. It leads to issue related to yaml.scanner.

I am using : datamodel-code-generator 0.21.4 pydantic 2.3.0 python 3.11.14

I tried to use datamodel on the following schema, which can be found there :(https://json-schema.org/understanding-json-schema/structuring.html#retrieval-uri)

{
  "$id": "https://example.com/schemas/customer",
  "$schema": "https://json-schema.org/draft/2020-12/schema",

  "type": "object",
  "properties": {
    "first_name": { "type": "string" },
    "last_name": { "type": "string" },
    "shipping_address": { "$ref": "/schemas/address" },
    "billing_address": { "$ref": "/schemas/address" }
  },
  "required": ["first_name", "last_name", "shipping_address", "billing_address"],

  "$defs": {
    "address": {
      "$id": "/schemas/address",
      "$schema": "http://json-schema.org/draft-07/schema#",

      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city": { "type": "string" },
        "state": { "$ref": "#/definitions/state" }
      },
      "required": ["street_address", "city", "state"],

      "definitions": {
        "state": { "enum": ["CA", "NY", "... etc ..."] }
      }
    }
  }
}```

I ran on the CLI datamodel-codegen --input schema.json --input-file-type jsonschema --output model.py.

I expected the generation of a pydantic model.

I got the following error message :

yaml.scanner.ScannerError: mapping values are not allowed in this context
  in "<unicode string>", line 11, column 25

A similar issue occured here : text However the solutions found don't seem to apply to my case.

Anyone knows what's happening ? I would appreciate any help.

Upvotes: 1

Views: 1133

Answers (1)

ncoghlan
ncoghlan

Reputation: 41586

The OP did file an issue for this as recommended in the question comments: https://github.com/koxudaxi/datamodel-code-generator/issues/1541

According to the GitHub history on the issue, it was resolved in the 0.23.0 release of data-model-generator

Upvotes: 0

Related Questions