OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python] Client expects any object to be a dict

Open
#16,110 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The Python client expects a property of type object to be a dict, which seems to be compliant with the JSON schema specification.

object: An unordered set of properties mapping a string to an instance, from the JSON "object" value

However, this causes problems in combination with e.g. a Java server, where everything can be an object.
For example, the API might allow an attribute to be either a string, a boolean, or a number, which can't be handled by the Python client.

The provided example generates an error value is not a valid dict (type=type_error.dict). Even if the object is explicitly annotated with anyOf and the corresponding data types, the error is the same.

openapi-generator version

7.0.0-beta

OpenAPI declaration file content or url

openapi.yaml

or with anyOf

...
    properties:
      name:
        type: object
        description: "A string, a number or a boolean"
        anyOf:
        - type: string
        - type: integer
          format: int64
        - type: number
          format: double
        - type: boolean
...
Generation Details

java -jar .\openapi-generator-cli-7.0.0-beta.jar generate -i .\openapi.yaml -g python -o .\python\

Steps to reproduce
  1. Create a virtual environment python -m venv venv
  2. Install the newly generated api client pip install .\python\
  3. Run the following code:
from openapi_client.api.default_api import DefaultApi
from openapi_client.models.pet import Pet

api = DefaultApi()

if __name__=="__main__":
    pet = Pet(id=123, name="Test")
    api.add_pet(pet)
  1. The following error occurs:
pydantic.error_wrappers.ValidationError: 1 validation error for Pet
name
  value is not a valid dict (type=type_error.dict)
Related issues/PRs
Suggest a fix

It seems to me that the client is generated according to the JSON schema specification, however this is useless in the scenario shown above. The old python client python-prior allowed an object to be anything. Personally, I think this should be the desired behavior with the new client as well.

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 linked openapi.yaml and the generated Python models, especially pet.py, then reproduce the validation error by installing the generated client and constructing Pet(name="Test"). Compare the generated handling of object and anyOf properties with the reported python-prior behavior; done means the demonstrated scalar values are accepted by the generated client and the reproduction no longer raises the dict validation error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.