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
Exception: A Parser can not resolve classes when using $ref
if references are nested
#300
Comments
@sonali686 OK, I understand why I just decided on how to implement it. I will support it. please wait... |
@sonali686 |
Hi! I have the similar problem for the following case: {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "test.json",
"description": "test",
"type": "object",
"required": [
"test_id",
"test_ip",
"result"
],
"properties": {
"test_id": {
"type": "string",
"description": "test ID"
},
"test_ip": {
"$ref": "base_test.json#/definitions/first"
}
}
} base_test.json: {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "base_test.json",
"description": "test",
"type": "object",
"definitions": {
"first": {
"$ref": "#/definitions/second"
},
"second": {
"type": "string"
}
}
}
Used commandline:
Error:
The issue can be solved by editing ref link:
But i don't sure it's the right solution Version:
|
Hi @MikeZaharov, # generated by datamodel-codegen:
# filename: test.json
# timestamp: 2021-01-24T16:08:55+00:00
from __future__ import annotations
from pydantic import BaseModel, Field
class Second(BaseModel):
__root__: str
class First(BaseModel):
__root__: Second
class Model(BaseModel):
test_id: str = Field(..., description='test ID')
test_ip: First |
@koxudaxi |
@koxudaxi I came across one more scenario where library fails to resolve classes while using $ref.
Is it possible to support this scenario as well? Is it possible to resolve any reference if it is a valid json pointer? Currently I am getting same exception as mentioned in my original post. |
@sonali686 I have fixed the code with my hands instantly. I put generated models. # generated by datamodel-codegen:
# filename: person.json
# timestamp: 2019-07-26T00:00:00+00:00
from __future__ import annotations
from enum import Enum
from typing import Any, Optional, Union
from pydantic import BaseModel, Field
class CatBreed(BaseModel):
__root__: Any
class DogBreed(BaseModel):
__root__: Any
class Pets(BaseModel):
__root__: Any
class PetType(Enum):
Cat = 'Cat'
class PetType1(Enum):
Dog = 'Dog'
class C1(BaseModel):
hunts: Optional[bool] = None
age: Optional[str] = None
class C2(BaseModel):
hunts: Optional[bool] = None
age: Optional[str] = None
class D1(BaseModel):
bark: Optional[bool] = None
age: Optional[str] = None
class D2(BaseModel):
hunts: Optional[bool] = None
age: Optional[str] = None
class Cat(BaseModel):
pet_type: PetType
breed: Optional[Union[C1, C2]] = Field(None, title='breed')
class Dog(BaseModel):
pet_type: PetType1
breed: Union[D1, D2] = Field(..., title='breed')
class Person(BaseModel):
name: Optional[str] = Field(None, title='name')
pet: Optional[Union[Cat, Dog]] = Field(None, title='pet') |
Yes I expected these models. Thanks for providing the solution so fast. 👍 |
@sonali686 I have released a new version as |
Describe the bug
Parser can not resolve classes when using
$ref
if the path is nested even though the reference is a valid json pointer.To Reproduce
Example schema:
Used commandline:
Expected behavior
I was expecting following things which is working when I do not encapsulate "Cat" and "Dogs" inside "Pet".
Version:
Additional context
I tried to pull the code and run this example but I am getting the same error. I have few large schemas which are encapsulating references for readability. After editing the schema according to datamodel-code-generator, it works wonderfully 👍 . Thanks for this module.
The text was updated successfully, but these errors were encountered: