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

[Python] Generated model for component with "allOf" has no attributes #453

Closed
ghost opened this issue Jul 4, 2018 · 3 comments
Closed

Comments

@ghost
Copy link

ghost commented Jul 4, 2018

Description

Given below is a snippet of the input YAML file I am using. I have several components in the original file that are all have the same structure as "AddEmailContactRequest". All of these components' classes are generated without any attributes, although the YAML file is read correctly.
Here is the relevant snippet of the generated code for "AddEmailContactRequest":

`class AddEmailContactRequest(object):
    openapi_types = {
    }
    attribute_map = {
    }

    def __init__(self):
        self.discriminator = None`

While what it should generate is the below code (I manually fixed it because, well, I need to keep working :) ):

`class AddEmailContactRequest(object):
    openapi_types = {
        'type': 'str',
        'attributes': 'EmailContact'
    }
    attribute_map = {
        'type': 'type',
        'attributes': 'attributes'
    }

    def __init__(self, type=None, attributes=None):  # noqa: E501
        self._type = None
        self._attributes = None
        self.discriminator = None

        self.type = type
        self.attributes = attributes`

NOTE - I am also getting several warnings as follows during generation:
[main] WARN o.o.codegen.DefaultCodegen - Unknown type found in the schema: object

openapi-generator version

I'm building from branch 4.0.x, but have encountered this problem with tag v3.0.3 as well.

OpenAPI declaration file content or url
openapi: "3.0.1"
servers:
  - url: 'http://www.myserver.com/api'
info:
  version: '1.0'
  title: MYSERVER


paths:
  '/email-contacts':
    post:
      summary: addEmailContact
      tags:
        - sometag
      operationId: addEmailContact
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:              
                    $ref: '#/components/schemas/AddEmailContactResponse'
      security: []
      x-unitTests: []
      x-operation-settings:
        CollectParameters: false
        AllowDynamicQueryParameters: false
        AllowDynamicFormParameters: false
        IsMultiContentStreaming: false
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                data:            
                  $ref: '#/components/schemas/AddEmailContactRequest'
        required: true


components:
  schemas:
    AddEmailContactRequest:
      allOf:
        - $ref: '#/components/schemas/JsonAPIPostRequestWrapper'
      title: addEmailContactRequest
      type: object
      properties:
        attributes:
          $ref: '#/components/schemas/EmailContact'
      required:
        - attributes
        
    AddEmailContactResponse:
      title: addEmailContactResponse
      type: object
      properties:
        type:
          description: ''
          type: string
        id:
          description: ''
          type: string
        attributes:
          $ref: '#/components/schemas/EmailContact'
      required:
        - type
        - id
        - attributes
   
    EmailContact:
      title: EmailContact
      description: ''
      type: object
      properties:
        name:
          description: ''
          type: string
        address:
          description: ''
          type: string
      required:
        - name
        - address

    JsonAPIPostRequestWrapper:
      title: JsonAPIPostRequestWrapper
      type: object
      properties:
        type:
          description: ''
          type: string
      required:
        - type
Command line used for generation

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i path_to_yaml_file_as_seen_above.yaml -g python -o /outputdir -v

Steps to reproduce
  1. Clone the project
  2. Checkout branch 4.0.x
  3. Build with "mvn clean install"
  4. Run the generation command as shown in previous section with given YAML file
  5. Under the "models" directory, go to add_email_contact_request.py --> The attributes will be missing.
Related issues/PRs

I think this might be related to an issue I saw for JAVA generation -
#340

Suggest a fix/enhancement

I wish I knew.. I would fix it myself :)

@wing328
Copy link
Member

wing328 commented Nov 7, 2018

@poli-granot I've submitted #1360 with better support for allOf, oneOf, etc.

Please give it a try and let me if you've any feedback.

@tobuh
Copy link

tobuh commented Nov 30, 2018

I just wanted to add another issue and noticed there is already one open. Maybe you might find my input useful:

Description

Generating python client code with yaml listed below will result in model of "another_object" containing only "anotherproperty" instead of additionally "name" from "test_object".

openapi-generator version

3.3.1
3.3.3
openapi-generator-cli-3.3.4-20181130.143646-28.jar
openapi-generator-cli-3.4.0-20181108.104734-7.jar
openapi-generator-cli-4.0.0-20181129.104836-46.jar

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: Test    
  version: 0.1.0
  title: "Test API"

paths:
  /objects:
    get:
      summary: "Get all available objects"
      operationId: getObjects
      responses:
        200:
          description: "List of available objects"
          content:
            application/json:
              schema:
                type: "array"
                items:
                  allOf:
                  - $ref: '#/components/schemas/AnotherObject'
        default:
          description: "standard error"

components:
  schemas:
    TestObject:
      type: "object"
      properties:
        name:
          type: "string"
          example: "Testname"
          
    AnotherObject:
      allOf:
        - $ref: '#/components/schemas/TestObject'
        - type: "object"
          properties:
            anotherproperty:
              type: "string"
Command line used for generation

java -jar openapi-generator-cli.jar generate -i testapispec.yaml -g python -o out/

Steps to reproduce

Use yaml and command line above. See out/openapi_client/models/another_object.py

Suggest a fix/enhancement

"$refs" should be followed

@spacether
Copy link
Contributor

spacether commented Oct 22, 2022

Per openapi json schema, each schema must be validated against the same payload.
So when SchemaA is empty except for allOf including SchemaB
What that means is:

  • Schema A allows in any type
  • If the ingested payload is an object, then all property values muse be of any json schema type (additionalProperties unset)
  • schema A defines no properties
  • the allOf schema (SchemaB) must also be validated ...

Because of this independent schema validation, the python (in openapi-generator v6.2.0) generator does not hoist properties from composed schemas higher. If hoisting is done, then there can be collisions of property types and whether or not the properties are required, if they are enum etc.

If you use the python client in v6.2.0, the AddEmailContactRequest class will contain the properties:

  • attributes

And the properties in JsonAPIPostRequestWrapper will be validated in that class.
And instance of AddEmailContactRequest will subclass both AddEmailContactRequest + JsonAPIPostRequestWrapper and will have all keys acessible and any required properties will be accessible with dot notation some_inst.some_required_prop

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

No branches or pull requests

3 participants