OpenAPITools / OpenAPITools/openapi-generator

[BUG][PHP] Generated value for discriminator mapping does not match expected value

Open
#9,551 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

We have a spec where we're using discriminator mappings for subtypes. The generated code is using the discriminator schema name instead of the discriminator value.

openapi-generator version

We are using the containerized openapi-generate, v5.1.1

OpenAPI declaration file content or url
components:
  schemas:
    AdHocCompanyStakeholderCreateRequest:
      additionalProperties: false
      allOf:
      - $ref: '#/components/schemas/StakeholderCreateRequest'
      properties:
        addresses:
          description: The stakeholder addresses
          items:
            $ref: '#/components/schemas/Address'
          nullable: true
          type: array
        email:
          description: The stakeholder email
          format: email
          nullable: true
          type: string
        name:
          description: The stakeholder name
          type: string
        phoneNumbers:
          description: The stakeholder phone numbers
          items:
            $ref: '#/components/schemas/PhoneNumber'
          nullable: true
          type: array
        taxId:
          description: The stakeholder tax id
          nullable: true
          type: string
      required:
      - name
      type: object
    AdHocIndividualStakeholderCreateRequest:
      additionalProperties: false
      allOf:
      - $ref: '#/components/schemas/StakeholderCreateRequest'
      properties:
        addresses:
          description: The stakeholder addresses
          items:
            $ref: '#/components/schemas/Address'
          nullable: true
          type: array
        companyStakeholderId:
          description: The stakeholder company stakeholder id
          format: uuid
          nullable: true
          type: string
        email:
          description: The stakeholder email
          format: email
          nullable: true
          type: string
        name:
          description: The stakeholder name
          type: string
        phoneNumbers:
          description: The stakeholder phone numbers
          items:
            $ref: '#/components/schemas/PhoneNumber'
          nullable: true
          type: array
        taxId:
          description: The stakeholder tax id
          nullable: true
          type: string
        title:
          description: The stakeholder title
          nullable: true
          type: string
      required:
      - name
      type: object
    StakeholderCreateRequest:
      additionalProperties: false
      discriminator:
        mapping:
          AdHocCompany: '#/components/schemas/AdHocCompanyStakeholderCreateRequest'
          AdHocIndividual: '#/components/schemas/AdHocIndividualStakeholderCreateRequest'
        propertyName: type
      properties:
        type:
          $ref: '#/components/schemas/StakeholderType'
      required:
      - type
      type: object
    StakeholderType:
      enum:
      - AdHocCompany
      - AdHocIndividual
      - Account
      - AccountUser
      type: string
      x-ms-enum:
        modelAsString: false
        name: StakeholderType
info:
  description: Test API
  title: The Test API
  version: 
openapi: 3.0.1
paths:
  /health/alive:
    get:
      operationId: Alive
      responses:
        200:
          content:
            application/json:
              schema:
                type: string
            application/problem+json:
              schema:
                type: string
          description: Service is alive.
      summary: Returns "alive" if the service is available.
      tags:
      - Health

Produces, for example, subtypes containing this constructor:

public function __construct(array $data = null)
{
    parent::__construct($data);

    $this->container['addresses'] = $data['addresses'] ?? null;
    $this->container['company_stakeholder_id'] = $data['company_stakeholder_id'] ?? null;
    $this->container['email'] = $data['email'] ?? null;
    $this->container['name'] = $data['name'] ?? null;
    $this->container['phone_numbers'] = $data['phone_numbers'] ?? null;
    $this->container['tax_id'] = $data['tax_id'] ?? null;
    $this->container['title'] = $data['title'] ?? null;

    // Initialize discriminator property with the model name.
    $this->container['type'] = static::$openAPIModelName;
}

We expect that the container['type'] would be set to a StakeholderType value such as the following, depending on the discriminator value:

$this->container['type'] = StakeholderType::AD_HOC_COMPANY;
Generation Details
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i ./local/OpenAPI/swagger.yaml -g php -o /local/ --additional-properties=legacyDiscriminatorBehavior=false
Steps to reproduce

Running the above generator command with the above swagger should produce the models that replicate this issue.

Related issues/PRs

This is actually related to an issue in the AutoRest repository that has been implemented in AutoRest >=3.2 so their C# generator now produces the expected output:
autorest issue 4107
autorest changelog

I am not sure if this is on a roadmap for the OpenAPI Generator but it is important functionality for supporting polymorphism in data models going forward.

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

Run the documented Docker generation command with the supplied OpenAPI YAML and inspect the generated PHP subtype constructors. Compare the discriminator property's generated value with the mapping values such as AdHocCompany and AdHocIndividual; done means the generated container uses the discriminator value rather than the schema name.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.