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

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

Open
np-kyokyo opened this issue Apr 9, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@np-kyokyo
Copy link

np-kyokyo commented Apr 9, 2024

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: cpython-x86_64@3.11.6
symlink support: true
uv enabled: true
~/g/datamode-codegen-a (master) [SIGINT]> rye run datamodel-codegen --version
0.25.5
@np-kyokyo
Copy link
Author

This seems to related to #1579

@koxudaxi koxudaxi added the bug Something isn't working label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants