swagger-api / swagger-api/swagger-codegen

Incorrect client code deserialization of composed models (using allOf) [python]

Open
#10,833 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I generated python client code using v3.0.23, generated models seem to describe the allOf inheritance schemas correctly, but when running GET requests on server, the de-serializer is treating the sub-schemas as the parent schema and therefore returning a base-model with only the base attributes
for example if I have childObject that inherits properties from parentObject (using allOf and a discriminator property)
response from GET parses the childObject from the response body into the parentObject and ignores all extra attributes of childObject (path is expected to receive objects of type parentObject)

When running post requests the client works fine with child models

Swagger-codegen version

3.0.23

Swagger declaration file content or url
Object1:
   type: object
   properties:
        prop1:
            type: string
        prop2:
            $ref: '#/components/schemas/ParentObject
            
           
ParentObject:
   type: object
   required:
        - objectType
        - baseProperty
   properties:
      objectType:
           type: string
      baseProperty:
           type: string
   discriminator:
        propertyName:  objectType
        
        
ChildObject1:
     allOf:
         -     $ref: '#/components/schemas/ParentObject
         -     type: object
                properties:
                     extraProp1:
                         type: string
ChildObject2:
     allOf:
         -     $ref: '#/components/schemas/ParentObject
         -     type: object
                properties:
                     extraProp2:
                         type: string

Command line used for generation

java -jar generate -i -l python -o

Steps to reproduce
  • generate code using similar YAML,
  • run GET request for path that uses ParentObject schema for responses,
  • Server should return object of type ChildObject1 or ChildObject2 as a response with appropriate objectType property (same as schema name)

expected: client code parses response into ChildObject1 or 2 according to the discriminator property (objectType)
observed: client code de-serializes objects in response into ParentObject

Related issues/PRs
Suggest a fix/enhancement

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated Python client's deserialization path and the supplied OpenAPI YAML, focusing on ParentObject, ChildObject1, ChildObject2, allOf, and the objectType discriminator. Generate the client with the documented Java command, reproduce a GET response containing a child object, and verify that discriminator-based deserialization preserves the child model and its extra properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.