datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Broken Import Statement: inheriting schemas named with "dot" notation (ie: `$ref: '#/components/schemas/v0.animal`)

Open andreffs18 opened this issue 1 year ago • 1 comments

Describe the bug

Broken imports when defining schema names with "dot" notation.

openapi: 3.0.0
info:
  title: Example for import errors
  version: 1.0.0

paths:
  {}

components:
  schemas:
    v0.animal:
      title: Animal
      description: An representation of an Animal
      type: object
      required:
        - name
      properties:
        name:
          description: Animal name
          type: string
        properties:
          $ref: "#/components/schemas/v0.properties"

    v0.properties:
      title: Properties
      description: Generic Animal Properties
      type: object
      additionalProperties: true

    v0.animal.dog:
      title: Dog
      description: Dog
      allOf:
        - $ref: '#/components/schemas/v0.animal'
        - type: object
          properties:
            color_of_the_fur:
              description: Color of the dog's fur
              type: string
       (...)

This would generate a "Dog" class with the following imports

# generated by datamodel-codegen:
#   filename:  animals.yaml

from __future__ import annotations

from typing import Optional

from pydantic import Field

from . import properties
from .Animal import Animal


class Dog(Animal):
    """
    Dog
    """

    color_of_the_fur: Optional[str] = Field(None, description="Color of the dog's fur")
    properties: Optional[properties.DogProperties] = Field(
        None, description='Which type of bark a dog can have'
    )

from .Animal import Animal is a broken import

To Reproduce

This repo is able to replicate this issue https://gitlab.com/andreffs18/datacodegen-experiments/-/tree/main

Just copy and paste the following into a terminal and you should be able to see the problem.

mkdir test
cd test/
git clone [email protected]:andreffs18/datacodegen-experiments.git
cd datacodegen-experiments/
make setup

Then to generate the models just run the shortcut make generate which runs the command below.

Used commandline:

$ datamodel-codegen --input schemas/animals.yaml --input-file-type openapi --output models/ --disable-timestamp --use-schema-description

Expected behavior A clear and concise description of what you expected to happen.

Version:

  • OS: Apple M2 Pro, MacOS 13.6
  • Python version: Python 3.9.6
  • datamodel-code-generator version: 0.25.8

andreffs18 avatar Jul 20 '24 12:07 andreffs18

Related to this one https://github.com/koxudaxi/datamodel-code-generator/issues/1600 with the difference of having more than just an extra "." on the import path.

andreffs18 avatar Jul 20 '24 12:07 andreffs18

This issue should now be fixed thanks to https://github.com/koxudaxi/datamodel-code-generator/pull/2343. @andreffs18 could you confirm?

ilovelinux avatar Oct 31 '25 14:10 ilovelinux