Code Monkey home page Code Monkey logo

Comments (2)

jstasiak avatar jstasiak commented on June 27, 2024

I managed to narrow it down to an input like this (openapi.yml):

openapi: 3.0.1

components:
  schemas:
    CreateOrderByEstimateRequest:
      type: object
      properties:
        quantity_trunc:
          $ref: '#/components/schemas/QuantityTrunc'

    QuantityTrunc:
      type: string
      description: Selects to which precision to truncate quantities specific to carbon offsetting.
      example: 't'
      allOf:
        - $ref: '#/components/schemas/MassUnit'

    MassUnit:
      type: string
      enum:
        - g
        - kg
        - t

The crash log:

% poetry run datamodel-codegen --input openapi.yml --input-file-type openapi --output models.py
Traceback (most recent call last):
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/spreadsheet-offset-tool-L93AmRO5-py3.12/lib/python3.12/site-packages/datamodel_code_generator/__main__.py", line 447, in main
    generate(
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/spreadsheet-offset-tool-L93AmRO5-py3.12/lib/python3.12/site-packages/datamodel_code_generator/__init__.py", line 468, in generate
    results = parser.parse()
              ^^^^^^^^^^^^^^
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/spreadsheet-offset-tool-L93AmRO5-py3.12/lib/python3.12/site-packages/datamodel_code_generator/parser/base.py", line 1187, in parse
    _, sorted_data_models, require_update_action_models = sort_data_models(
                                                          ^^^^^^^^^^^^^^^^^
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/spreadsheet-offset-tool-L93AmRO5-py3.12/lib/python3.12/site-packages/datamodel_code_generator/parser/base.py", line 141, in sort_data_models
    return sort_data_models(
           ^^^^^^^^^^^^^^^^^
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/spreadsheet-offset-tool-L93AmRO5-py3.12/lib/python3.12/site-packages/datamodel_code_generator/parser/base.py", line 208, in sort_data_models
    raise Exception(f'A Parser can not resolve classes: {unresolved_classes}.')
Exception: A Parser can not resolve classes: [class: openapi.yml#/components/schemas/CreateOrderByEstimateRequest references: frozenset({'openapi.yml#/components/schemas/QuantityTrunc'})].

I can't reduce the example any further.

  • If I remove the enum block from MassUnit the crash goes away
  • If I remove the CreateOrderByEstimateRequest type the crash goes away
  • If I remove QuantityTrunc and make CreateOrderByEstimateRequest refer to MassUnit (instead of QuantityTrunc) the crash goes away

So it seems like a $ref/anyOf/enum edge case?

from datamodel-code-generator.

jstasiak avatar jstasiak commented on June 27, 2024

Interesting, if I change that allOf instance to oneOf (possible in this case, only one child type anyway), like

openapi: 3.0.1

components:
  schemas:
    CreateOrderByEstimateRequest:
      type: object
      properties:
        quantity_trunc:
          $ref: '#/components/schemas/QuantityTrunc'

    QuantityTrunc:
      type: string
      description: Selects to which precision to truncate quantities specific to carbon offsetting.
      example: 't'
      oneOf:
        - $ref: '#/components/schemas/MassUnit'

    MassUnit:
      type: string
      enum:
        - g
        - kg
        - t

the tool actually generates code:

# generated by datamodel-codegen:
#   filename:  openapi.yml
#   timestamp: 2024-05-23T11:41:39+00:00

from __future__ import annotations

from enum import Enum
from typing import Optional

from pydantic import BaseModel, Field


class MassUnit(Enum):
    g = 'g'
    kg = 'kg'
    t = 't'


class QuantityTrunc(BaseModel):
    __root__: MassUnit = Field(
        ...,
        description='Selects to which precision to truncate quantities specific to carbon offsetting.',
        example='t',
    )


class CreateOrderByEstimateRequest(BaseModel):
    quantity_trunc: Optional[QuantityTrunc] = None

from datamodel-code-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.