0

I am trying to generate a python client library with openApI3 . For this I have created a openapi.yml file, where I defined my url and schema with request and response.

I am trying with openApI-generator that I found here https://github.com/OpenAPITools/openapi-generator command: openapitools/openapi-generator-cli

This generator, producing a set of directory and files based on schema defined in yml file.

When I test it's auto generated fils, I am getting error

I am adding here my yml file and auto-generated test_file with error below:`

This is my yml file

opanapi.yml

openapi: 3.0.1
info:
  title: Config Service
  version: '2.0'
  description: Project and system config microservice
  contact: {}
servers:
  - url: ''
paths:
  /config/v1/datasources:
    get:
      tags:
        - config/v1
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Datasource'
          description: Success
        '404':
          description: Not Found
      operationId: get_datasources
      summary: GET endpoint
      description: return list of sources
components:
  schemas:
    Datasource:
      description: ''
      type: object
      properties:
        type:
          type: string
          minLength: 1
        properties:
          type: object
          properties:
            _id:
              type: object
              properties:
                type:
                  type: string
                  minLength: 1

This is the auto-generated test file for models.

test_datasource.py

# coding: utf-8

"""
    Config Service

    Project and system config microservice  # noqa: E501

    The version of the OpenAPI document: 2.0
    Generated by: https://openapi-generator.tech
"""


from __future__ import absolute_import

import unittest
import datetime

import tech.client.config
from tech.client.config.models.datasource import Datasource  # noqa: E501
from tech.client.config.rest import ApiException

class TestDatasource(unittest.TestCase):
    """Datasource unit test stubs"""

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def make_instance(self, include_optional):
        """Test Datasource
            include_option is a boolean, when False only required
            params are included, when True both required and
            optional params are included """
        # model = tech.client.config.models.datasource.Datasource()  # noqa: E501
        if include_optional :
            return Datasource(
                type = '0', 
                properties = tech.client.config.models.datasource_properties.Datasource_properties(
                    _id = tech.client.config.models.datasource_properties__id.Datasource_properties__id(
                        type = '0', )
        else :
            return Datasource(
        )

    def testDatasource(self):
        """Test Datasource"""
        inst_req_only = self.make_instance(include_optional=False)
        inst_req_and_optional = self.make_instance(include_optional=True)


if __name__ == '__main__':
    unittest.main()

   When I am test the above file , I am getting the below error.**
**Error:**
 File "test_datasource.py", line 76, in testDatasource
 inst_req_and_optional = self.make_instance(include_optional=True)
 File "test/test_datasource.py", line 41, in make_instance
 _id = tech.client.config.models.datasource_properties__id.Datasource_properties__id(
 AttributeError: module 'tech.client.config.models' has no attribute 'datasource_properties__id'

Note: In models directory datasource_properties__id is not auto-generated.

I searched a lot regarding this, I a not sure why I am getting this issue. Does opeanAPI 3 not support nested-schema/nested objects ?

Any help/lead would be really appreciable. Thanks

Learner
  • 11
  • 1
  • 8
  • Did you try with another generator ? I’ve used https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/4.3.1/openapi-generator-cli-4.3.1.jar Java cli – pippo1980 Dec 24 '20 at 19:36
  • I tried with tht it fails with: module 'openapi_client.models.datasource_properties' has no attribute 'Datasource_properties' actually it has Datasourceproperties – pippo1980 Dec 25 '20 at 09:35
  • In models directory datasource_properties__id.py is generated. – pippo1980 Dec 25 '20 at 09:52
  • https://stackoverflow.com/questions/54803837/openapi-required-property-in-nested-objects-not-working – pippo1980 Dec 25 '20 at 10:03

1 Answers1

0
if include_optional :
        return Datasource(
            type = '0', 
            properties = tech.client.config.models.datasource_properties.Datasource_properties(
                _id = tech.client.config.models.datasource_properties__id.Datasource_properties__id(
                    type = '0', )

there is an error here in your test_datasource.py file

I tried:

openapi: 3.0.1
info:
 title: Config Service
 version: '2.0'
 description: Project and system config microservice
 contact: {}
servers:
 - url: ''
paths:
 /config/v1/datasources:
   get:
     tags:
       - config/v1
     responses:
       '200':
         content:
           application/json:
             schema:
               type: array
               items:
                 $ref: '#/components/schemas/Datasource'
         description: Success
       '404':
         description: Not Found
     operationId: get_datasources
     summary: GET endpoint
     description: return list of sources
components:
 schemas:
   Datasource:
     description: ''
     type: object
     properties:
       Typez:
        $ref: '#/components/schemas/Typez'
         
   Typez:    
     type: object
     properties : 
      pippo:
       type: string
         

using org/openapitools/openapi-generator-cli/4.3.1

and generated test_datasource.py runs fine

with :

openapi: 3.0.1
info:
 title: Config Service
 version: '2.0'
 description: Project and system config microservice
 contact: {}
servers:
 - url: ''
paths:
 /config/v1/datasources:
   get:
     tags:
       - config/v1
     responses:
       '200':
         content:
           application/json:
             schema:
               type: array
               items:
                 $ref: '#/components/schemas/Datasource'
         description: Success
       '404':
         description: Not Found
     operationId: get_datasources
     summary: GET endpoint
     description: return list of sources
components:
 schemas:
   Datasource:
     description: ''
     type: object
     properties:
       typez:
        $ref: '#/components/schemas/typez'
         
   typez:    
     type: object
     properties : 
      pippo:
       type: string
        

generated test_datasourc.py fails with:

ERROR: testDatasource (__main__.TestDatasource)
Test Datasource
----------------------------------------------------------------------
Traceback (most recent call last):
 File "test_datasource.py", line 49, in testDatasource
   inst_req_and_optional = self.make_instance(include_optional=True)
 File "test_datasource.py", line 39, in make_instance
   typez = openapi_client.models.typez.typez(
AttributeError: module 'openapi_client.models.typez' has no attribute 'typez'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

I am not sure about

 $ref: '#/components/schemas/Typez'

but it should be something nested ?!

so about:

searched a lot regarding this, I a not sure why I am getting this issue. Does opeanAPI 3 not support nested-schema/nested objects ?

I would say : depends

hope someone better would answer this ;-)

pippo1980
  • 2,181
  • 3
  • 14
  • 30