Invalid code generated for polymorphic array in Swagger spec
See original GitHub issueDescribe 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:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 forUnion
,List
, etc.Thank you. I have closed this issue. Please feel free to report them if you see any problem.