openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[python-flask] Support regeneration without overwriting implementation

Open OJFord opened this issue 3 years ago • 6 comments

PR checklist

  • [x] Read the contribution guidelines.
  • [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • [x] Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @arun-nalla (2019/11) @spacether (2019/11)

Presently, running generate overwrites any existing implementation of the generated stubs. This fixes it by running an implementation function from another file instead of simply returning 'do some magic'; the user can ignore the openapi_server/impl directory and put the stub implementations there, allowing them to re-run generate as required without blowing them away.

OJFord avatar Apr 20 '22 16:04 OJFord

I tried to regenerate the samples but got the following errors:

[pool-1-thread-8] Generating python-aiohttp (outputs to /Users/williamcheng/Code/openapi-generator4/samples/server/petstore/python-aiohttp-srclayout)…
[pool-1-thread-4] Generating python-blueplanet (outputs to /Users/williamcheng/Code/openapi-generator4/samples/server/petstore/python-blueplanet)…
[pool-1-thread-1] Generating python-legacy (outputs to /Users/williamcheng/Code/openapi-generator4/samples/client/petstore/python-asyncio)…
[pool-1-thread-5] Generating python-fastapi (outputs to /Users/williamcheng/Code/openapi-generator4/samples/server/petstore/python-fastapi)…
[pool-1-thread-6] Generating python (outputs to /Users/williamcheng/Code/openapi-generator4/samples/openapi3/client/extensions/x-auth-id-alias/python)…
################################################################################
# Thanks for using OpenAPI Generator.                                          #
# Please consider donation to help us maintain this project 🙏                 #
# https://opencollective.com/openapi_generator/donate                          #
################################################################################
[pool-1-thread-6] Finished generating python…
[pool-1-thread-2] Generation failed for python-aiohttp: (RuntimeException) Could not generate supporting file 'SupportingFile[templateFile='__init__impl.mustache', folder='openapi_server/impl', destinationFilename='__init__.py', canOverwrite=true]'
java.lang.RuntimeException: Could not generate supporting file 'SupportingFile[templateFile='__init__impl.mustache', folder='openapi_server/impl', destinationFilename='__init__.py', canOverwrite=true]'
	at org.openapitools.codegen.DefaultGenerator.generateSupportingFiles(DefaultGenerator.java:719)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:899)
	at org.openapitools.codegen.cmd.GenerateBatch$GenerationRunner.run(GenerateBatch.java:232)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.openapitools.codegen.templating.TemplateNotFoundException: __init__impl.mustache
	at org.openapitools.codegen.TemplateManager.getFullTemplateFile(TemplateManager.java:54)
	at org.openapitools.codegen.TemplateManager.getFullTemplateContents(TemplateManager.java:72)
	at org.openapitools.codegen.templating.MustacheEngineAdapter.compileTemplate(MustacheEngineAdapter.java:63)
	at org.openapitools.codegen.TemplateManager.write(TemplateManager.java:163)
	at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1038)
	at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1025)
	at org.openapitools.codegen.DefaultGenerator.generateSupportingFiles(DefaultGenerator.java:711)
	... 5 more

Can you please take a look when you've time?

wing328 avatar May 04 '22 17:05 wing328

I've tried again from a different machine, but I still get this - this is on master, not this branch:

$ ./mvnw clean package
[...]

[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR]   DefaultGeneratorTest.supportCustomTemplateEngine:313 » Runtime Could not generate api file for 'Ping'
[ERROR]   StringHelpersTest.startsWithSectionalTest:47->evaluate:28 » Handlebars inline@486b7642:1:4: java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.util.HashMap$Node[] java.util.HashMap.table accessible: module java.base does not "opens java.util" to unnamed module @4d41cee
    inline@486b7642:1:4
[ERROR]   StringHelpersTest.startsWithTest:61->evaluate:28 » Handlebars inline@43bd77be:1:2: java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.util.HashMap$Node[] java.util.HashMap.table accessible: module java.base does not "opens java.util" to unnamed module @4d41cee
    inline@43bd77be:1:2
[ERROR]   StringHelpersTest.startsWithYesOverrideTest:81->evaluate:28 » Handlebars inline@689e2599:1:2: java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.util.HashMap$Node[] java.util.HashMap.table accessible: module java.base does not "opens java.util" to unnamed module @4d41cee
    inline@689e2599:1:2
[INFO]
[ERROR] Tests run: 1628, Failures: 4, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for openapi-generator-project 6.0.0-SNAPSHOT:
[INFO]
[INFO] openapi-generator-project .......................... SUCCESS [  0.550 s]
[INFO] openapi-generator-core ............................. SUCCESS [  5.222 s]
[INFO] openapi-generator (core library) ................... FAILURE [ 46.962 s]
[INFO] openapi-generator (executable) ..................... SKIPPED
[INFO] openapi-generator (maven-plugin) ................... SKIPPED
[INFO] openapi-generator-gradle-plugin (maven wrapper) .... SKIPPED
[INFO] openapi-generator-online ........................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  53.027 s
[INFO] Finished at: 2022-05-10T21:37:57+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M6:test (default-test) on project openapi-generator: There are test failures.
[ERROR]
[ERROR] Please refer to /home/ojf/code/openapi-generator/modules/openapi-generator/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :openapi-generator

I don't really ever work with or no much about java/maven/etc. so it's not really clear to me what's wrong? I assume it's something missing from/wrong with my environment.

(Running generate-samples.sh without that errors Error: Unable to access jarfile /home/ojf/code/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar - I assume that should be created by a successful mvnw package?)

OJFord avatar May 10 '22 20:05 OJFord

Oh, maven wasn't installed. Just a bizarre error for it. After installing, ./bin/generate-samples.sh bin/configs/python-flask.yaml worked fine without running the mvnw clean package first.

OJFord avatar May 10 '22 20:05 OJFord

The docs generator produces a diff, but none of which is related to my changes (all other languages even).

OJFord avatar May 10 '22 20:05 OJFord

The tests failed:

AttributeError: module 'openapi_server.impl' has no attribute 'get_inventory'
_____________________ TestUserController.test_logout_user ______________________
self = <openapi_server.test.test_user_controller.TestUserController testMethod=test_logout_user>
    def test_logout_user(self):
        """Test case for logout_user
    
        Logs out current logged in user session
        """
        headers = {
        }
        response = self.client.open(
            '/v2/user/logout',
            method='GET',
            headers=headers)
        self.assert200(response,
>                      'Response body is : ' + response.data.decode('utf-8'))
openapi_server/test/test_user_controller.py:132: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.tox/py3/lib/python3.6/site-packages/flask_testing/utils.py:336: in assert200
    self.assertStatus(response, 200, message)
.tox/py3/lib/python3.6/site-packages/flask_testing/utils.py:324: in assertStatus
    self.assertEqual(response.status_code, status_code, message)
E   AssertionError: 500 != 200 : Response body is : {
E     "detail": "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.",
E     "status": 500,
E     "title": "Internal Server Error",
E     "type": "about:blank"
E   }
------------------------------ Captured log call -------------------------------
WARNING  connexion.operations.openapi3:openapi.py:270 this operation accepts multiple content types, using application/json
WARNING  connexion.operations.openapi3:openapi.py:270 this operation accepts multiple content types, using application/json
WARNING  connexion.operations.openapi3:openapi.py:270 this operation accepts multiple content types, using application/json
WARNING  connexion.operations.openapi3:openapi.py:270 this operation accepts multiple content types, using application/json
WARNING  connexion.operations.openapi3:openapi.py:270 this operation accepts multiple content types, using application/json
WARNING  connexion.operations.openapi3:openapi.py:270 this operation accepts multiple content types, using application/json
WARNING  connexion.operations.openapi3:openapi.py:270 this operation accepts multiple content types, using applica

Ref: https://app.travis-ci.com/github/OpenAPITools/openapi-generator/builds/250793217

Can you please take a look when you've time?

wing328 avatar May 18 '22 08:05 wing328

Travis CI failed the following:

  File "/home/travis/build/OpenAPITools/openapi-generator/samples/server/petstore/python-flask/.tox/py3/lib/python3.6/site-packages/connexion/decorators/parameter.py", line 120, in wrapper
    return function(**kwargs)
  File "/home/travis/build/OpenAPITools/openapi-generator/samples/server/petstore/python-flask/openapi_server/controllers/user_controller.py", line 93, in get_user_by_name
    return impl.get_user_by_name(
AttributeError: module 'openapi_server.impl' has no attribute 'get_user_by_name'
______________________ TestUserController.test_login_user ______________________
self = <openapi_server.test.test_user_controller.TestUserController testMethod=test_login_user>
    def test_login_user(self):
        """Test case for login_user
    
        Logs user into the system
        """
        query_string = [('username', 'username_example'),
                        ('password', 'password_example')]
        headers = {
            'Accept': 'application/json',
        }
        response = self.client.open(
            '/v2/user/login',
            method='GET',
            headers=headers,
            query_string=query_string)
        self.assert200(response,
>                      'Response body is : ' + response.data.decode('utf-8'))
openapi_server/test/test_user_controller.py:118: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.tox/py3/lib/python3.6/site-packages/flask_testing/utils.py:336: in assert200
    self.assertStatus(response, 200, message)
.tox/py3/lib/python3.6/site-packages/flask_testing/utils.py:324: in assertStatus
    self.assertEqual(response.status_code, status_code, message)
E   AssertionError: 500 != 200 : Response body is : {
E     "detail": "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.",
E     "status": 500,
E     "title": "Internal Server Error",
E     "type":

ref: https://app.travis-ci.com/github/OpenAPITools/openapi-generator/builds/255699561

can you please take a look when you've time?

wing328 avatar Sep 19 '22 02:09 wing328