From e3200c1187b9cfbe35f0a2283907967ece4a6260 Mon Sep 17 00:00:00 2001 From: lseguin Date: Wed, 13 Nov 2024 16:13:34 +0100 Subject: [PATCH] bugfix/generate_correct_setup_py_when_no_apis --- .../src/main/resources/python/setup.mustache | 9 +------ .../python/PythonClientCodegenTest.java | 25 +++++++++++++++++++ .../src/test/resources/3_0/no_apis.yaml | 16 ++++++++++++ .../setup.py | 3 +-- samples/client/echo_api/python/setup.py | 3 +-- .../client/petstore/python-aiohttp/setup.py | 3 +-- .../openapi3/client/petstore/python/setup.py | 3 +-- 7 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/no_apis.yaml diff --git a/modules/openapi-generator/src/main/resources/python/setup.mustache b/modules/openapi-generator/src/main/resources/python/setup.mustache index 12e052372e2c..cb33a3001dd8 100644 --- a/modules/openapi-generator/src/main/resources/python/setup.mustache +++ b/modules/openapi-generator/src/main/resources/python/setup.mustache @@ -13,9 +13,6 @@ from setuptools import setup, find_packages # noqa: H301 NAME = "{{{projectName}}}" VERSION = "{{packageVersion}}" PYTHON_REQUIRES = ">= 3.8" -{{#apiInfo}} -{{#apis}} -{{#-last}} REQUIRES = [ "urllib3 >= 1.25.3, < 3.0.0", "python-dateutil >= 2.8.2", @@ -33,7 +30,6 @@ REQUIRES = [ "pydantic >= 2", "typing-extensions >= 4.7.1", ] - setup( name=NAME, version=VERSION, @@ -51,7 +47,4 @@ setup( {{appDescription}} """, # noqa: E501 package_data={"{{{packageName}}}": ["py.typed"]}, -) -{{/-last}} -{{/apis}} -{{/apiInfo}} +) \ No newline at end of file diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java index 3510bb3e894c..e41800606fb5 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java @@ -28,6 +28,8 @@ import org.openapitools.codegen.*; import org.openapitools.codegen.languages.PythonClientCodegen; import org.openapitools.codegen.languages.features.CXFServerFeatures; + +import static org.junit.jupiter.api.Assertions.assertNull; import static org.openapitools.codegen.TestUtils.assertFileContains; import static org.openapitools.codegen.TestUtils.assertFileExists; import org.openapitools.codegen.TestUtils; @@ -541,4 +543,27 @@ public void testEnumPropertyWithQuotes() { Assert.assertEquals(codegen.toEnumValue("1.0", "float"), "1.0"); Assert.assertEquals(codegen.toEnumValue("1", "int"), "1"); } + + @Test + public void testHandleNoApis() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/no_apis.yaml"); + final DefaultGenerator defaultGenerator = new DefaultGenerator(); + final ClientOptInput clientOptInput = new ClientOptInput(); + clientOptInput.openAPI(openAPI); + PythonClientCodegen pythonClientCodegen = new PythonClientCodegen(); + pythonClientCodegen.setOutputDir(output.getAbsolutePath()); + clientOptInput.config(pythonClientCodegen); + defaultGenerator.opts(clientOptInput); + + Map files = defaultGenerator.generate().stream().collect(Collectors.toMap(File::getPath, Function.identity())); + + File apiFile = files.get(Paths.get(output.getAbsolutePath(), "openapi_client", "api", "hello_example_api.py").toString()); + assertNull(apiFile); + + File setupFile = files.get(Paths.get(output.getAbsolutePath(), "setup.py").toString()); + assertNotNull(setupFile); + assertFileContains(setupFile.toPath(), "setup("); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/no_apis.yaml b/modules/openapi-generator/src/test/resources/3_0/no_apis.yaml new file mode 100644 index 000000000000..68961bbbc7b9 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/no_apis.yaml @@ -0,0 +1,16 @@ +openapi: 3.0.3 +info: + title: Example Hello API + description: '' + version: v1 +servers: + - url: http://localhost + description: Global Endpoint +paths: {} +components: + schemas: + HelloResponse: + type: object + properties: + message: + type: string \ No newline at end of file diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/setup.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/setup.py index 64d7cd1c9db5..c4206f267f73 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/setup.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/setup.py @@ -30,7 +30,6 @@ "pydantic >= 2", "typing-extensions >= 4.7.1", ] - setup( name=NAME, version=VERSION, @@ -48,4 +47,4 @@ Echo Server API """, # noqa: E501 package_data={"openapi_client": ["py.typed"]}, -) +) \ No newline at end of file diff --git a/samples/client/echo_api/python/setup.py b/samples/client/echo_api/python/setup.py index 64d7cd1c9db5..c4206f267f73 100644 --- a/samples/client/echo_api/python/setup.py +++ b/samples/client/echo_api/python/setup.py @@ -30,7 +30,6 @@ "pydantic >= 2", "typing-extensions >= 4.7.1", ] - setup( name=NAME, version=VERSION, @@ -48,4 +47,4 @@ Echo Server API """, # noqa: E501 package_data={"openapi_client": ["py.typed"]}, -) +) \ No newline at end of file diff --git a/samples/openapi3/client/petstore/python-aiohttp/setup.py b/samples/openapi3/client/petstore/python-aiohttp/setup.py index 6cb0af14e066..15c18c15acba 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/setup.py +++ b/samples/openapi3/client/petstore/python-aiohttp/setup.py @@ -33,7 +33,6 @@ "pydantic >= 2", "typing-extensions >= 4.7.1", ] - setup( name=NAME, version=VERSION, @@ -51,4 +50,4 @@ This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ """, # noqa: E501 package_data={"petstore_api": ["py.typed"]}, -) +) \ No newline at end of file diff --git a/samples/openapi3/client/petstore/python/setup.py b/samples/openapi3/client/petstore/python/setup.py index 742fd7c6281d..561f27978b32 100755 --- a/samples/openapi3/client/petstore/python/setup.py +++ b/samples/openapi3/client/petstore/python/setup.py @@ -31,7 +31,6 @@ "pydantic >= 2", "typing-extensions >= 4.7.1", ] - setup( name=NAME, version=VERSION, @@ -49,4 +48,4 @@ This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ """, # noqa: E501 package_data={"petstore_api": ["py.typed"]}, -) +) \ No newline at end of file