Skip to content
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

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

Open
jdart opened this issue Apr 10, 2020 · 1 comment
Open

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

jdart opened this issue Apr 10, 2020 · 1 comment

Comments

@jdart
Copy link

jdart commented Apr 10, 2020

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...

@dordal
Copy link

dordal commented Oct 13, 2020

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants