Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 of 6 tasks
zishunwei opened this issue Apr 25, 2024 · 0 comments
Closed
5 of 6 tasks

Comments

@zishunwei
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant