datamodel-code-generator: Does not inherit all the classes mentioned in `allOf`

Describe the bug It is not extending all the mentioned classes in the allOf attribute of the schema.

To Reproduce I am running this on the directory: https://github.com/AnalyticalGraphicsInc/czml-writer/tree/master/Schema. But it does not inherit all the classes as I mentioned above. For example, one of the schemas is as follows,

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://analyticalgraphicsinc.github.io/czml-writer/Schema/Color.json",
    "title": "Color",
    "description": "A color. The color can optionally vary over time.",
    "allOf": [
        { "$ref": "InterpolatableProperty.json" },
        { "$ref": "DeletableProperty.json" },
        { "$ref": "ValueProperties/RgbaValueProperty.json" },
        { "$ref": "ValueProperties/RgbafValueProperty.json" },
        { "$ref": "ValueProperties/ReferenceValueProperty.json" }
    ],
    "type": [
        "array",
        "object"
    ],
    "items": {
        "$ref": "#"
    },
    "properties": {
        "rgba": {
            "$ref": "Values/RgbaValue.json",
            "description": "The color specified as an array of color components `[Red, Green, Blue, Alpha]` where each component is an integer in the range 0-255."
        },
        "rgbaf": {
            "$ref": "Values/RgbafValue.json",
            "description": "The color specified as an array of color components `[Red, Green, Blue, Alpha]` where each component is a double in the range 0.0-1.0."
        },
        "reference": {
            "$ref": "Values/ReferenceValue.json",
            "description": "The color specified as a reference to another property."
        }
    }
}

It generates the class as follows:

# generated by datamodel-codegen:
#   filename:  Color.json
#   timestamp: 2021-03-29T05:46:25+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field

from . import Cylinder, LabelStyle


class Color(BaseModel):
    """
    A color. The color can optionally vary over time.
    """

    rgba: Optional[Cylinder.RgbaValue] = Field(
        None,
        description='The color specified as an array of color components `[Red, Green, Blue, Alpha]` where each component is an integer in the range 0-255.',
    )
    rgbaf: Optional[Cylinder.RgbafValue] = Field(
        None,
        description='The color specified as an array of color components `[Red, Green, Blue, Alpha]` where each component is a double in the range 0.0-1.0.',
    )
    reference: Optional[LabelStyle.ReferenceValue] = Field(
        None, description='The color specified as a reference to another property.'
    )

Expected behavior It should have inherited the classes DeletableProperty and InterpolatableProperty

Additional context Those classes have been generated and are residing in the same directory. It’s just that they are not being imported and inherited.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 22 (12 by maintainers)

Most upvoted comments

Hi, works like a charm now 💯

Thanks for fixing it 😃

Is the naming to the extra class given by one of these PRs. For example Color1 in this comment. If yes, then in my opinion we can name it like _ClassName instead of ClassName1.

It’s only a suggestion, it’s okay if you think ClassName1 is better, it was just my opinion.

Thanks for the super quick fix @koxudaxi 😃

Sure, just give me a minute, I’ll test it and come back to you.

Hmm, reasonable.

No problem. And thanks again for fixing it, closing the issue!

Thank you for giving me the schema.

I have understood why the error is happened. The parser can’t resolve actual $ref. The files have $id attributes to indicate self url. But, the file is in our local disk. The parser is confused what file should read …

I will fix it.

No problem, take your time 😄

I got this error while using the codegen on an entire directory at once. Download Schema.zip and run it on the Schema directory. The command I am using is,

datamodel-codegen  --use-schema-description --input /path/to/Schema/ --input-file-type jsonschema --output ./generated/

We can only one choice to resolve the problems It’s to ignore inherited __root__ models.

I agree, I think ignoring __root__ models would not matter that much.

I merged a fixed for combined allOf and object. Next, I will implement a feature for combined object and array . a better way is…


class Colors(BaseModel):
   __root__: Unoin[List[Color], Color]

Thanks for your willingness to fix this. I don’t have the context to imagine the amount of work it will require but please let me know if you need any help.

Thank you 😃