question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"A Parser can not resolve classes" for modular json schema structure

See original GitHub issue

Describe the bug

The parser cannot resolve all classes for a modular json schema structure

Exception: A Parser can not resolve classes: [class: Person references: {'pet.JsonPet'}].

To Reproduce

Example schema:

/schema
  person.json
  /definitions
    pet.json
{
  "$id": "person.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Person",
  "type": "object",
  "properties": {
    "first_name": {
      "type": "string",
      "description": "The person's first name."
    },
    "last_name": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years.",
      "type": "integer",
      "minimum": 0
    },
    "pets": {
      "type": "array",
      "items": [
        {
          "$ref": "definitions/pet.json#Pet"
        }
      ]
    },
    "comment": {
      "type": "null"
    }
  },
  "required": [
      "first_name",
      "last_name"
  ],
}
{
  "$id": "pet.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Pet",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  }
}

Used commandline:

$ datamodel-codegen --input schema --input-file-type jsonschema --output test.py

Expected behavior I expected similar output as described in https://pydantic-docs.helpmanual.io/datamodel_code_generator

Version:

  • OS: macOS Catalina 10.15.4 (19E287)
  • Python version: Python 3.8
  • datamodel-code-generator version: datamodel-codegen --version 0.6.2

Additional context Maybe I’m structuring the refs incorrectly, I would love to know how to change them to make this work. Thanks in advance!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
themmescommented, Nov 21, 2020

Are you kidding? I listed this yesterday, please don’t feel rushed by my comments. You’re doing an amazing job on this library! 👍

1reaction
themmescommented, Nov 21, 2020

The problem was a mistake in my example, the reference should be "$ref": "definitions/pet.json#/Pet" and then it works perfectly!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support generating models from json-schemas defined in ...
This would help with generating models from json-schema (or . ... "A Parser can not resolve classes" for modular json schema structure #267....
Read more >
Understanding JSON Schema
JSON Schema is a powerful tool for validating the structure of JSON data. However, learning to use it by reading its.
Read more >
Convert a JSON schema to a python class
I would love an answer to this question that had a code generator (compiler) for the classes, rather than the current approaches, which...
Read more >
jsonschema
JSON schema validator, which is designed to be fast and simple to use. JSON Schema versions through draft-07 are fully supported. Contributing & ......
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
Property names must be double-quoted strings. You cannot use single-quotes around properties, like 'foo'. JSON.parse ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found