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] Missing model inheritance in generated client when schema has both oneOf and properties #1206

Open
kqr opened this issue Oct 9, 2018 · 6 comments

Comments

@kqr
Copy link

kqr commented Oct 9, 2018

Description

When a schema contains both properties and oneOf, I expect openapi-generator to create multiple PHP classes with inheritance. In the example schema below, I expect

  • One PHP class called Fruit, which has a protected member color with getters and setters.
  • One PHP class called Apple, which inherits from Fruit and extends it with protected member kind, and, crucially, whose serialisation contains both fields color and kind.
  • One PHP class called Banana, which also inherits from Fruit but extends it with protected member count instead.

This does not happen – instead the Fruit superclass is generated without any protected members at all (i.e. the properties are ignored as soon as oneOf is part of the schema) and the "child" classes do not inherit from the superclass.

openapi-generator version

3.3.0

OpenAPI declaration file content or url

Reasonably minimal example to reproduce the problem. The interesting bits are under #/components/schemas, so you can ignore the rest.

{ "openapi": "3.0.1",
  "info": { "title": "fruity", "version": "0.0.1" },
  "paths": { "/": { "get": { "responses": { "200": {
    "description": "desc",
    "content": { "application/json": {
      "schema": { "$ref": "#/components/schemas/fruit" }}}}}}}},
  "components": { "schemas": {
    "fruit": {
      "title": "fruit",
      "type": "object",
      "properties": { "color": { "type": "string" }},
      "oneOf": [
          {"$ref": "#/components/schemas/apple"},
          {"$ref": "#/components/schemas/banana"}
      ]
    },
    "apple": {
      "title": "apple",
      "type": "object",
      "properties": { "kind": { "type": "string" } }
    },
    "banana": {
      "title": "banana",
      "type": "object",
      "properties": { "count": { "type": "number" } }
    }
  }}
}
Command line used for generation

openapi-generator generate -g php -i testschema.json -o out

Related issues/PRs

I couldn't find any related specifically to using the closed oneOf way to model inheritance. The common way to model inheritance with OpenAPI seems to be to use allOf in the child classes instead.

Suggest a fix/enhancement

The actual fixes to the generated code are really small, but I don't really know where to put them!

@kqr
Copy link
Author

kqr commented Oct 26, 2018

Actually, this happens also when there is an allOf specification present, even in 3.3.1.

@wing328
Copy link
Member

wing328 commented Nov 1, 2018

@kqr I've filed #1360 to better support allOf, anyOf, oneOf. I wonder if you can give it a try to see if it works better for your spec.

@wing328
Copy link
Member

wing328 commented Dec 10, 2018

#1360 has been merged into master. Please give it another try and let us know if it's still an issue with allOf. Fro oneOf & anyOf, we will need to update the PHP templates to support these. Please let us know if you've time to contribute the enhancement.

Closing this issue for the time being.

@darren-potter
Copy link

I am having this problem with version 4.0.1 of openapi-generator on mac OS 10.14.5.
Spec (snippet):

    200UserAuthenticatedResponse:
      description: Successful user login response
      headers:
        X-Request-Id:
          $ref: "#/components/headers/X-Request-Id"
      content:
        application/json:
          schema:
            allOf:
              - $ref: "#/components/schemas/ApiResponseCommonValues"
              - type: object
                required:
                  - details
                properties:
                  details:
                    $ref: "#/components/schemas/LoginAuthenticatedResponse"

Generates the following PHP:

switch($statusCode) {
                case 200:
                    if ('ApiResponseCommonValues' === '\SplFileObject') {
                        $content = $responseBody; //stream goes to serializer
                    } else {
                        $content = $responseBody->getContents();
                    }

                    return [
                        ObjectSerializer::deserialize($content, 'ApiResponseCommonValues', []),
                        $response->getStatusCode(),
                        $response->getHeaders()
                    ];

It's attempting to deserialize the response into the single class ApiResponseCommonValues (it also doing this badly but I will raise this separately). The next effect is that I don't have access the properties relating to LoginAuthenticatedResponse.

@ksvirkou-hubspot
Copy link
Contributor

Hi everyone
We have the same issue
generator version is 5.1.0

@ddanielroche
Copy link

Hi everyone
We have the same issue
generator version is 6.6.0

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

5 participants