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

$refs to object properties break the parser

Open mfulgo opened this issue 3 years ago • 1 comments
trafficstars

Describe the bug The parser raises an error if any models contain references to object properties, rather than the top-level objects.

To Reproduce

Example schema:

openapi: "3.0.3"
info:
  title: Example
  version: 1.0.0

paths: {}

components:
  schemas:
    Id:
      type: string
    Parent:
      type: object
      properties:
        id:
          $ref: "#/components/schemas/Id"
        name:
          type: string
    Child:
      type: object
      properties:
        id:
          $ref: "#/components/schemas/Id"
        parent_id:
          $ref: "#/components/schemas/Parent/properties/id"
        name:
          type: string

This will fail because parent_id points to a property, rather than a schema object.

Used commandline:

$ datamodel-codegen --input ./example.yaml --target-python-version 3.9 --validation

Expected behavior I see two options:

  1. Generate two models, with parent_id in Child duplicating the id field in Parent. (Probably best for standard types.)
  2. Generate three models, Parent, Parent.Id, Child. (Probably best if the referenced field is an object.)

Version:

  • OS: iOS
  • Python version: 3.9
  • datamodel-code-generator version: 0.11.20

mfulgo avatar Apr 06 '22 16:04 mfulgo

@mfulgo Thank you for creating the issue. But, It's difficult to implement from the current code. I didn't expect the way to refer to an object's property 😅 I will think how to fix the problem.

koxudaxi avatar Apr 16 '22 17:04 koxudaxi

@mfulgo
I'm sorry for my too late reply. I have released the fixed version 0.16.0 Thank you very much!!

koxudaxi avatar Jan 16 '23 14:01 koxudaxi