Skip to content
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

msgspec output of a directory input where a schema only contains a root model is missing imports #1607

Closed
indrat opened this issue Oct 6, 2023 · 3 comments · Fixed by #1611

Comments

@indrat
Copy link
Contributor

indrat commented Oct 6, 2023

Describe the bug
Similar to #1495 when using a directory input with a annotated constrainted RootModel where the schema ends up generating solely RootModels in the generated python, the module is missing the required imports.

To Reproduce

$ tree schemas
schemas
├── common.yml
└── test.yml

$ datamodel-codegen --input schemas --input-file-type jsonschema --output src --disable-timestamp --enable-version-header --output-model-type msgspec.Struct

$ tree src
src
├── __init__.py
├── common.py
└── test.py

# Note the missing `from msgspec import Meta`
$ cat common.py
# generated by datamodel-codegen:
#   filename:  test.yml
#   version:   0.22.0

from __future__ import annotations

from typing import Any

from typing_extensions import Annotated

Model = Any


Ulid = Annotated[str, Meta(pattern='[0-9ABCDEFGHJKMNPQRSTVWXYZ]{26,26}')]

Example schema:

  • schemas/common.yml
---
$schema: https://json-schema.org/draft/2020-12/schema
$id: common.yml
definitions:
  ulid:
    type: string
    pattern: '[0-9ABCDEFGHJKMNPQRSTVWXYZ]{26,26}'
  • schemas/test.yml
---
$schema: https://json-schema.org/draft/2020-12/schema
$id: test.yml
title: test
required:
  - uid
properties:
  uid:
    description: ulid of this object
    $ref: ./common.yml#/definitions/ulid

Used commandline:

$ datamodel-codegen --input schemas --input-file-type jsonschema --output src --disable-timestamp --enable-version-header --output-model-type msgspec.Struct

Expected behavior
Generated modules that only root models that are annotated with Meta constraints import the required classes.

eg:

from msgspec import Meta

Version:

  • OS: Linux, debian 12.
  • Python version: 3.11
  • datamodel-code-generator version: 0.22.0
@indrat
Copy link
Contributor Author

indrat commented Oct 6, 2023

Investigating this it would appear that it maybe because the root_model.RootModel has no concept of adding to the imports so when a python module is generated that only contains root models the requisite imports aren't added?

Although there is obviously some interaction with the msgspec.Struct model as the root model has been able to "acquire" the Meta constraint however I've no idea how that happens or how to affect the import list for a root model.

@koxudaxi
Copy link
Owner

koxudaxi commented Oct 6, 2023

Investigating this it would appear that it maybe because the root_model.RootModel has no concept of adding to the imports so when a python module is generated that only contains root models the requisite imports aren't added?

Which codes are you talking about specifically?
I would like to see if I can add it.

@indrat
Copy link
Contributor Author

indrat commented Oct 6, 2023

The MR #1609 doesn't currently fix this unfortunately. Running the following on the test data added in that MR is still missing the Meta import, note the lack of --collapse-root-models

› datamodel-codegen --input tests/data/jsonschema/duplicate_field_constraints \
    --input-file-type jsonschema \
    --output tests/data/expected/main/duplicate_field_constraints_msgspec \
    --disable-timestamp --enable-version-header \
    --output-model-type msgspec.Struct \
    --target-python 3.9

› cat tests/data/expected/main/duplicate_field_constraints_msgspec/common.py
# generated by datamodel-codegen:
#   filename:  common.yml
#   version:   0.0.0

from __future__ import annotations

from typing import Annotated, Any

Model = Any


Ulid = Annotated[str, Meta(pattern='[0-9ABCDEFGHJKMNPQRSTVWXYZ]{26,26}')]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants