swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

PHP Client doesn't seem to support anyOf with discriminator

Open jdart opened this issue 6 years ago • 1 comments

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:

https://github.com/swagger-api/swagger-codegen/issues/4622 https://github.com/swagger-api/swagger-codegen/pull/3475 https://github.com/swagger-api/swagger-codegen/issues/4226 https://github.com/swagger-api/swagger-codegen/issues/4346 https://github.com/swagger-api/swagger-codegen/issues/3904

Suggest a fix/enhancement

Not sure...

jdart avatar Apr 10 '20 19:04 jdart

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.

dordal avatar Oct 13 '20 04:10 dordal