Describe the bug
When a schema in one file references another schema in another file using the $ref keyword, and the referenced schema uses the oneOf keyword in the OpenAPI specification, the model generated does not utilize the imports from the referenced schema.
For example. the schema SupportedCommands in commands.yaml has a $ref to the AddUserCommand schema in user.yaml. The AddUserCommand schema is using the oneOf
keyword. The generated commands.py
imports user
as expected but doesn't use the import when referencing the user models
This bug only occurs when the models are generated using an input directory of OpenAPI schemas instead of a single openapi.yaml. It also only appears to be happening when I use the -collapse-root-models
flag. When I generate without that flag, the imports are referenced correctly.
Generated models with unused imports
To Reproduce
user.yaml
and commands.yaml
using the schemas belowExample user.yaml schema:
openapi: 3.0.0
components:
schemas:
JsonRpcError:
type: object
properties:
error:
type: string
User:
type: object
properties:
userId:
type: string
AddUserCommand:
oneOf:
- $ref: "#/components/schemas/User"
- $ref: "#/components/schemas/JsonRpcError"
Example commands.yaml schema:
openapi: 3.0.0
components:
schemas:
SupportedCommands:
$ref: "user.yaml#/components/schemas/AddUserCommand"
Used commandline:
$ datamodel-codegen --input schemas --output models --collapse-root-models
Expected behavior
The user models referenced in commands.py should be correctly prefixed with the imported module name. For example, instead of User
/ JsonRpcError
, it should be user.User
/ user.JsonRpcError
, like shown below:
# generated by datamodel-codegen:
# filename: commands.yaml
# timestamp: 2023-07-31T22:40:48+00:00
from __future__ import annotations
from pydantic import BaseModel
from . import user
class SupportedCommands(BaseModel):
__root__: Union[user.User, user.JsonRpcError]
Version:
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too