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

[yaml] The generator does not generate Python code from models in external files referenced by $ref.

Open np-kyokyo opened this issue 1 year ago • 1 comments

Describe the bug

Models in external files referenced by $ref are not being output to the Python file. It seems only the items under index.yml#/components are being output. It might be by design, but since the openapi-generator outputs them, it would be nice if this could also output those models.

To Reproduce

Example schema:

index.yaml


openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    $ref: "./apis/pets/index.yaml" 
components:
  schemas:
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

apis/pets/index.yaml


get:
  summary: List all pets
  operationId: listPets
  tags:
    - pets
  responses:
    '200':
      description: A paged array of pets
      content:
        application/json:
          schema:
            $ref: "model.yaml#/Pets"
    default:
      description: unexpected error
      content:
        application/json:
          schema:
            $ref: "../../model.yaml#/components/schemas/Error"

apis/pets/model.yaml


Pet:
  required:
    - id
    - name
  properties:
    id:
      type: integer
      format: int64
    name:
      type: string
    tag:
      type: string
Pets:
  type: array
  items:
    $ref: "#/Pet"

Used commandline:

$ rye run datamodel-codegen --input index.yaml --output model.py

result:

# generated by datamodel-codegen:
#   filename:  index.yaml
#   timestamp: 2024-04-09T05:17:06+00:00

from __future__ import annotations

from pydantic import BaseModel


class Error(BaseModel):
    code: int
    message: str

Expected behavior

Models are properly output to model.py.

Version:

~/g/datamode-codegen-a (master) [1]> rye --version
rye 0.32.0
commit: 0.32.0 (e1b4f2a29 2024-03-29)
platform: macos (aarch64)
self-python: [email protected]
symlink support: true
uv enabled: true
~/g/datamode-codegen-a (master) [SIGINT]> rye run datamodel-codegen --version
0.25.5

np-kyokyo avatar Apr 09 '24 05:04 np-kyokyo

This seems to related to #1579

np-kyokyo avatar Apr 11 '24 12:04 np-kyokyo