question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid code generated for polymorphic array in Swagger spec

See original GitHub issue

Describe the bug Invalid code generated for polymorphic array in Swagger spec.

To Reproduce Using the following Swagger YAML:

openapi: 3.0.0
info:
  title: datamodel-code-generator bug example
components:
  schemas:
    Container:
      allOf:
        - type: object
          required:
            - contents
          properties:
            contents:
              type: array
              items:
                anyOf:
                  - $ref: '#/components/schemas/Type1'
                  - $ref: '#/components/schemas/Type2'
    Type1:
      type: object
      properties:
        prop:
          type: string
    Type2:
      type: object
      properties:
        prop:
          type: string

The generated code:

# generated by datamodel-codegen:
#   filename:  swagger-borked.yaml
#   timestamp: 2020-06-10T22:51:26+00:00

from __future__ import annotations

from typing import List, Optional

from pydantic import BaseModel


class Type1(BaseModel):
    prop: Optional[str] = None


class Type2(BaseModel):
    prop: Optional[str] = None


class Container(BaseModel):
    contents: List[Type1, Type2]

The last line is invalid and causes a runtime error.

TypeError: Too many parameters for typing.List; actual 2, expected 1

Expected behavior The generated code should be a list containing a union of the types.

class Container(BaseModel):
    contents: List[Union[Type1, Type2]]

This occurs using the latest version of datamodel-code-generator, 0.5.2.

Thanks for the tool! It’s very handy. I tried to figure out how to fix this myself but found myself going in circles.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
FlorianLudwigcommented, Jun 11, 2020

I believe to properly support this (and all other nested possibilities) some refactoring is needed. Currently only a few cases are handled (see datamodel-code-generator/datamodel_code_generator/model/base.py function: DataModelFieldBase._get_type_hint).

DataModelFieldBase.data_types would need to be converted from a list to a tree, with nodes for Union, List, etc.

0reactions
koxudaxicommented, Jun 11, 2020

Thank you. I have closed this issue. Please feel free to report them if you see any problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can not generate a server with polymorphic type containing ...
Problem statement Error on server generation when the specification contains a polymorphic type that contains an array of the base type.
Read more >
Polymorphism in Swagger produces strange error message ...
The error occurs because you are mixing OpenAPI 2.0 and 3.0 syntax. Your spec is swagger: '2.0' but the following is 3.0 syntax:...
Read more >
OpenAPI Specification - Version 2.0 - Swagger
Version 2.0 specification defines a set of files required to describe an API. These files can then be used by the Swagger-UI project...
Read more >
OpenAPI Specification - Version 3.0.3 - Swagger
The OpenAPI Specification defines a standard interface to RESTful APIs which allows both humans and computers to understand service capabilities without ...
Read more >
Inheritance and Polymorphism - Swagger
OAS 3 This guide is for OpenAPI 3.0. Inheritance and Polymorphism. Model Composition. In your API, you may have model schemas that share...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found