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

Incorrect submodule import string when generating from nested directory structure

Open chu-k opened this issue 2 years ago • 0 comments

Describe the bug

Generating models from a nested directory structure with relative references leads to invalid imports. For the structure shown below, the generated class for B has invalid relative import. It's almost perfect besides that issue, the exported directory structure mirrors my schema directory correctly:

from __future__ import annotations

from typing import Optional

from ..foo.barA import A
# should be >> from ..foo.bar.A import A


class B(A):
    name: Optional[str] = None

To Reproduce

A nested schema directory structure as:

schema/foo/bar/A.yml

---
title: A
type: object
properties:
  something:
    type: string

schema/baz/B.yml


---
title: B
type: object
allOf:
- $ref: "../foo/bar/A.yml#"
properties:
  name:
    type: string


Used commandline:

$ datamodel-codegen --input aschema/ --input-file-type jsonschema --output tmp

Expected behavior

The relative import in the generated python file should correctly reference the nested submodule


from ..foo.bar.A import A

Version:

  • OS: "Debian GNU/Linux 11 (bullseye)"
  • Python version: Python 3.8.6
  • datamodel-code-generator version: 0.21.5

chu-k avatar Sep 12 '23 17:09 chu-k