Skip to content

[BUG][PYTHON] Failed to lookup discriminator value using AllOf and discriminator #18495

@zishunwei

Description

@zishunwei

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?
    expected output: Calling for the DefaultApi.get_user_info() passes
    actual output:
ValueError: Base failed to lookup discriminator value from {"_typeName": "string", "_value": "some string"}. Discriminator property name: _typeName, mapping: {"string": "PrimitiveString", "Info": "Info"}
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Although I have a mapping "string": "PrimitiveString", but still failed to lookup discriminator value from {"_typeName": "string", "_value": "some string"}.

The root cause is there is a bug to get the object_type in in the from_dict of base model, it should be "string" instead of "PrimitiveString", causing the failure of mapping.

This is from_dict of base model generated:

    @classmethod
    def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[PrimitiveString, Info]]:
        """Create an instance of Base from a dict"""
        # look up the object type based on discriminator mapping
        object_type = cls.get_discriminator_value(obj)
        if object_type ==  'string':
            return import_module("openapi_client.models.primitive_string").PrimitiveString.from_dict(obj)
        if object_type ==  'Info':
            return import_module("openapi_client.models.info").Info.from_dict(obj)

        raise ValueError("Base failed to lookup discriminator value from " +
                            json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
                            ", mapping: " + json.dumps(cls.__discriminator_value_class_map))
openapi-generator version

Latest master code 25/04/2024

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: ""
  version: ""
paths:
  /user/getInfo:
    get:
      operationId: getUserInfo
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Info'
          description: |
            OK
      security:
        - Session: []
      x-accepts: application/json
components:
  schemas:
    Info:
      allOf:
        - $ref: '#/components/schemas/Base'
      example:
        val:
          _typeName: _typeName
      properties:
        val:
          $ref: '#/components/schemas/Base'
      required:
        - val
      type: object
    Base:
      discriminator:
        mapping:
          string: '#/components/schemas/PrimitiveString'
        propertyName: _typeName
      properties:
        _typeName:
          type: string
      required:
        - _typeName
      type: object
    PrimitiveString:
      allOf:
        - $ref: '#/components/schemas/Base'
      properties:
        _value:
          type: string
      required:
        - _value
      type: object
Generation Details

Enable REFACTOR_ALLOF_WITH_PROPERTIES_ONLY=true

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i disc.yaml -g python -o disc --openapi-normalizer REFACTOR_ALLOF_WITH_PROPERTIES_ONLY=true
Steps to reproduce
  1. run a server to response like:
    {
    "_typeName": "Info",
    "val": {
    "_typeName": "string",
    "_value": "some string"
    }
    }

  2. call get_user_info() API

Related issues/PRs
Suggest a fix

When generating the discriminator model, should have some fix to make the mapping available.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions