Skip to content

PHP Client doesn't seem to support anyOf with discriminator #10168

Open

Description

@jdart
Description

I have the below spec and the generated php client doesn't seem to handle the combination of anyOf and discriminator. I see a property DISCRIMINATOR in the generated models but they're all null.

Swagger-codegen version

3.0.18, not sure if it's a regression

Swagger declaration file content or url
openapi: 3.0.0
info:
  title: Foo
  version: '1.0'
paths:
  /api/v1/object:
    get:
      operationId: get-object
      responses:
        '200':
          description: example
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExampleResponse'
components:
  schemas:
    ObjectFoo:
      type: object
      required:
        - value
        - type
      properties:
        value:
          type: integer
        type:
          type: string
    ObjectBar:
      type: object
      required:
        - type
      properties:
        value:
          type: boolean
        type:
          type: string
    ExampleResponse:
      properties:
        objects:
          type: array
          items:
            anyOf:
              - $ref: '#/components/schemas/ObjectFoo'
              - $ref: '#/components/schemas/ObjectBar'
            discriminator:
              propertyName: type
Command line used for generation

I used brew to install codegen, command below:

swagger-codegen generate -i ./swagger-smol.yml -l php --config ./swagger-codegen/php-config.json -o ~/workspace/foo-client --git-user-id=foo --git-repo-id=foo

Steps to reproduce

Populate swagger-smol.yml with the above snippet and run the command above.

Related issues/PRs

I didn't find anything exactly like this but there are similar issues:

#4622
#3475
#4226
#4346
#3904

Suggest a fix/enhancement

Not sure...

Activity

  1. dordal commented on Oct 13, 2020

    @dordal

    I can confirm this is still an issue as of codegen 3.0.22, and appears to affect more than just PHP.

    dordal@Atlantis-MBP Downloads % swagger-codegen version
    3.0.22
    

    Running the above file with one of the debug flags on....

    dordal@Atlantis-MBP Downloads % swagger-codegen generate -lphp -i distest.yaml -o dis-test -DdebugSwagger 
    
          <snip>
    
          "ExampleResponse" : {
            "properties" : {
              "objects" : {
                "type" : "array",
                "items" : {
                 "discriminator" : {
                    "propertyName" : "type"
                  },
                  "anyOf" : [ {
                    "$ref" : "#/components/schemas/ObjectFoo"
                  }, {
                    "$ref" : "#/components/schemas/ObjectBar"
                  } ]
                }
              }
            }
    
    

    So you can see that swagger is parsing the discriminator, and it's even included in the schema for the AnyOfExampleResponseObjectsItems model:

    dordal@Atlantis-MBP Downloads % swagger-codegen generate -lphp -i distest.yaml -o dis-test -DdebugModels 
    
      <snip>
    
      "importPath" : "Swagger\\Client\\Model.AnyOfExampleResponseObjectsItems",
      "model" : {
        "vendorExtensions" : { },
        "interfaces" : [ ],
        "name" : "AnyOfExampleResponseObjectsItems",
        "classname" : "AnyOfExampleResponseObjectsItems",
        "classVarName" : "any_of_example_response_objects_items",
        "classFilename" : "AnyOfExampleResponseObjectsItems",
        "vars" : [ ],
        "requiredVars" : [ ],
        "optionalVars" : [ ],
        "readOnlyVars" : [ ],
        "readWriteVars" : [ ],
        "allVars" : [ ],
        "parentVars" : [ ],
        "mandatory" : [ ],
        "allMandatory" : [ ],
        "imports" : [ ],
        "emptyVars" : false,
        "isComposedModel" : true,
        "hasVars" : false,
        "hasOnlyReadOnly" : false,
        "hasMoreModels" : false,
        "hasEnums" : false,
        "hasRequired" : false,
        "hasOptional" : false,
        "hasChildren" : false,
        "isPrimitiveType" : false,
        "hasHeaders" : false,
        "isString" : false,
        "isNumeric" : false,
        "isInteger" : false,
        "isLong" : false,
        "isNumber" : false,
        "isFloat" : false,
        "isDouble" : false,
        "isByteArray" : false,
        "isBoolean" : false,
        "isDate" : false,
        "isDateTime" : false,
        "isUuid" : false,
        "isDefault" : false,
        "isMapContainer" : false,
        "isListContainer" : false,
        "isMultipart" : false,
        "isResponseBinary" : false,
        "isResponseFile" : false,
        "isBinary" : false,
        "isFile" : false,
        "isEnum" : false,
        "isArrayModel" : false,
        "isAlias" : false,
        "isObject" : false,
        "hasInnerObject" : false,
        "isContainer" : false,
        "isNotContainer" : false,
        "isReadOnly" : false,
        "isCollectionFormatMulti" : false,
        "hasMore" : false
      }
    }, {
      "schema" : {
        "properties" : {
          "objects" : {
            "type" : "array",
            "items" : {
              "discriminator" : {
                "propertyName" : "type"
              },
              "anyOf" : [ {
                "$ref" : "#/components/schemas/ObjectFoo"
              }, {
                "$ref" : "#/components/schemas/ObjectBar"
              } ]
            }
          }
        }
      },
    

    But the discriminator is still set to null in the generated PHP code:

        const DISCRIMINATOR = null;
    

    Looking at the mustache template ( https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/php/model_generic.mustache ) it appears the {{discriminator}} variable isn't getting set, which is resulting in a null value.

    I was able to reproduce this same issue with python, which appears to use a similar mustache template with a {{discriminator}} variable. Very probably affects all languages.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions