Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit b28b544

Browse files
authored
v2 use modern python packaging (#115)
* Adds src folder above package in java code * Moves package code into the new src subfolder, petstore regenerated * Removes setup.cfg * setup.py changed to pyproject.toml * Reverts python java file change * Removes setup files, fixes installation instructions * Regenerated samples * Fixes java test
1 parent 3731ee8 commit b28b544

File tree

2,076 files changed

+4032
-4156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,076 files changed

+4032
-4156
lines changed

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ protected Map<String, Schema> getModelNameToSchemaCache() {
476476

477477
@Override
478478
public String packageName() {
479+
// used to generate imports
479480
return packageName;
480481
}
481482

@@ -4063,7 +4064,7 @@ public String responseFilename(String templateName, String jsonPath) {
40634064
// #/paths/somePath/get/responses/200 -> length 6
40644065
String pathModuleName = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]));
40654066
String httpVerb = pathPieces[3];
4066-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + pathPieces[5] + File.separatorChar + writtenFilename;
4067+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + pathPieces[5] + File.separatorChar + writtenFilename;
40674068
}
40684069
return null;
40694070
}
@@ -4080,7 +4081,7 @@ public String requestBodyFilename(String templateName, String jsonPath) {
40804081
// #/paths/somePath/get/requestBody -> length 5
40814082
String pathModuleName = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]));
40824083
String httpVerb = pathPieces[3];
4083-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + writtenFilename;
4084+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + writtenFilename;
40844085
}
40854086
return null;
40864087
}
@@ -4098,7 +4099,7 @@ public String parameterFilename(String templateName, String jsonPath) {
40984099
String pathModuleName = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]));
40994100
String httpVerb = pathPieces[3];
41004101
String i = pathPieces[5];
4101-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + writtenFilename;
4102+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + writtenFilename;
41024103
}
41034104
return null;
41044105
}
@@ -4121,7 +4122,7 @@ public String headerFilename(String templateName, String jsonPath) {
41214122
String httpVerb = pathPieces[3];
41224123
String code = pathPieces[5];
41234124
String headerModule = toHeaderFilename(pathPieces[7]);
4124-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + writtenFilename;
4125+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + writtenFilename;
41254126
}
41264127
return null;
41274128
}
@@ -4177,36 +4178,36 @@ public String schemaFilename(String templateName, String jsonPath) {
41774178
if (pathPieces.length == 7) {
41784179
// #/paths/somePath/get/parameters/1/schema -> length 7
41794180
String i = pathPieces[5];
4180-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(pathPieces[6]).getSnakeCaseName() + suffix;
4181+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(pathPieces[6]).getSnakeCaseName() + suffix;
41814182
} else if (pathPieces.length == 8) {
41824183
// #/paths/somePath/get/requestBody/content/application-json/schema -> length 8
41834184
String contentType = ModelUtils.decodeSlashes(pathPieces[6]);
4184-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
4185+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
41854186
} else if (pathPieces.length == 9) {
41864187
String parametersOrResponses = pathPieces[4];
41874188
if (parametersOrResponses.equals("parameters")) {
41884189
// #/paths/somePath/get/parameters/1/content/application-json/schema -> length 9
41894190
String i = pathPieces[5];
41904191
String contentType = ModelUtils.decodeSlashes(pathPieces[7]);
4191-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
4192+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
41924193
} else if (parametersOrResponses.equals("responses")) {
41934194
String contentOrHeaders = pathPieces[6];
41944195
String code = pathPieces[5];
41954196
if (contentOrHeaders.equals("headers")) {
41964197
// #/paths/somePath/get/responses/200/headers/someHeader/schema -> length 9
41974198
String headerModule = toHeaderFilename(pathPieces[7]);
4198-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(pathPieces[8]).getSnakeCaseName() + suffix;
4199+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(pathPieces[8]).getSnakeCaseName() + suffix;
41994200
}
42004201
// #/paths/somePath/get/responses/200/content/application-json/schema -> length 9
42014202
String contentType = ModelUtils.decodeSlashes(pathPieces[7]);
4202-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
4203+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
42034204
}
42044205
} else if (pathPieces.length == 11) {
42054206
// #/paths/somePath/get/responses/200/headers/someHeader/content/application-json/schema -> length 11
42064207
String code = pathPieces[5];
42074208
String headerModule = toHeaderFilename(pathPieces[7]);
42084209
String contentType = ModelUtils.decodeSlashes(pathPieces[9]);
4209-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
4210+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
42104211
}
42114212
}
42124213
return null;
@@ -5555,12 +5556,12 @@ private List<CodegenSchema> getComposedProperties(List<Schema> xOfCollection, St
55555556

55565557
@Override
55575558
public String requestBodyFileFolder(String componentName) {
5558-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "components" + File.separatorChar + "request_bodies" + toRequestBodyFilename(componentName);
5559+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "components" + File.separatorChar + "request_bodies" + toRequestBodyFilename(componentName);
55595560
}
55605561

55615562
@Override
55625563
public String responseFileFolder(String componentName) {
5563-
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "components" + File.separatorChar + "responses" + File.separatorChar + toResponseModuleName(componentName);
5564+
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "components" + File.separatorChar + "responses" + File.separatorChar + toResponseModuleName(componentName);
55645565
}
55655566

55665567
@Override

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,6 @@ public void processOpts() {
359359
generateSourceCodeOnly = Boolean.valueOf(additionalProperties.get(CodegenConstants.SOURCECODEONLY_GENERATION).toString());
360360
}
361361

362-
if (generateSourceCodeOnly) {
363-
// tests in test
364-
testFolder = packagePath() + File.separatorChar + testFolder;
365-
// api docs in <package>/docs/apis/tags/
366-
apiDocPath = packagePath() + File.separatorChar + apiDocPath;
367-
// model docs in <package>/docs/models/
368-
modelDocPath = packagePath() + File.separatorChar + modelDocPath;
369-
}
370362
// make api and model doc path available in templates
371363
additionalProperties.put("apiDocPath", apiDocPath);
372364
additionalProperties.put("modelDocPath", modelDocPath);
@@ -410,13 +402,12 @@ public void processOpts() {
410402
supportingFiles.add(new SupportingFile("tox." + templateExtension, "", "tox.ini"));
411403
supportingFiles.add(new SupportingFile("test-requirements." + templateExtension, "", "test-requirements.txt"));
412404
supportingFiles.add(new SupportingFile("requirements." + templateExtension, "", "requirements.txt"));
413-
supportingFiles.add(new SupportingFile("setup_cfg." + templateExtension, "", "setup.cfg"));
414405

415406
supportingFiles.add(new SupportingFile("git_push.sh." + templateExtension, "", "git_push.sh"));
416407
supportingFiles.add(new SupportingFile("gitignore." + templateExtension, "", ".gitignore"));
417408
supportingFiles.add(new SupportingFile("travis." + templateExtension, "", ".travis.yml"));
418409
supportingFiles.add(new SupportingFile("gitlab-ci." + templateExtension, "", ".gitlab-ci.yml"));
419-
supportingFiles.add(new SupportingFile("setup." + templateExtension, "", "setup.py"));
410+
supportingFiles.add(new SupportingFile("pyproject." + templateExtension, "", "pyproject.toml"));
420411
}
421412
supportingFiles.add(new SupportingFile("configuration." + templateExtension, packagePath(), "configuration.py"));
422413
supportingFiles.add(new SupportingFile("__init__package." + templateExtension, packagePath(), "__init__.py"));
@@ -1865,8 +1856,9 @@ public void setPackageUrl(String packageUrl) {
18651856
}
18661857

18671858
public String packagePath() {
1868-
return packageName.replace('.', File.separatorChar);
1869-
}
1859+
// src is needed for modern packaging per
1860+
// https://packaging.python.org/en/latest/tutorials/packaging-projects/
1861+
return "src" + File.separatorChar + packageName.replace('.', File.separatorChar); }
18701862

18711863
/**
18721864
* Generate Python package name from String `packageName`

modules/openapi-json-schema-generator/src/main/resources/python/README.handlebars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ import {{{packageName}}}
129129
Install via [Setuptools](http://pypi.python.org/pypi/setuptools).
130130

131131
```sh
132-
python setup.py install --user
132+
python -m pip install . --user
133133
```
134-
(or `sudo python setup.py install` to install the package for all users)
134+
(or `python -m pip install .` to install the package for all users)
135135

136136
Then import the package:
137137
```python
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
2+
3+
[build-system]
4+
requires = [
5+
{{> required_libraries quoted=true }}
6+
]
7+
build-backend = "setuptools.build_meta"
8+
9+
[project]
10+
name = "{{{projectName}}}"
11+
version = "{{packageVersion}}"
12+
{{#with apiInfo}}
13+
authors = [
14+
{ name="{{#if infoName}}{{infoName}}{{else}}OpenAPI JSON Schema Generator community{{/if}}"{{#if infoEmail}}, email="{{infoEmail}}"{{/if}} },
15+
]
16+
description = "{{appName}}"
17+
readme = "README.md"
18+
requires-python = "{{{generatorLanguageVersion}}}"
19+
classifiers = [
20+
"Programming Language :: Python :: 3",
21+
{{#if licenseInfo}}"License :: {{licenseInfo}}",{{/if}}
22+
"Operating System :: OS Independent",
23+
"Topic :: Software Development :: Code Generators"
24+
]
25+
26+
{{#or packageUrl infoUrl}}
27+
[project.urls]
28+
{{#if packageUrl}}"Homepage" = "{{{packageUrl}}}"{{/if}}
29+
{{#if infoUrl}}"Bug Tracker" = "{{{infoUrl}}}"{{/if}}
30+
{{/or}}
31+
{{/with}}

modules/openapi-json-schema-generator/src/main/resources/python/required_libraries.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{{#if quoted}}"{{/if}}pycryptodome >= 3.9.0{{#if quoted}}",{{/if}}
99
{{/if}}
1010
{{#if quoted}}"{{/if}}python-dateutil ~= 2.7.0{{#if quoted}}",{{/if}}
11-
{{#if quoted}}"{{/if}}setuptools >= 21.0.0{{#if quoted}}",{{/if}}
11+
{{#if quoted}}"{{/if}}setuptools >= 61.0{{#if quoted}}",{{/if}}
1212
{{#if tornado}}
1313
{{#if quoted}}"{{/if}}tornado >= 4.2{{#if quoted}}",{{/if}}
1414
{{/if}}

modules/openapi-json-schema-generator/src/main/resources/python/setup.handlebars

Lines changed: 0 additions & 38 deletions
This file was deleted.

modules/openapi-json-schema-generator/src/main/resources/python/setup_cfg.handlebars

Lines changed: 0 additions & 13 deletions
This file was deleted.

modules/openapi-json-schema-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,10 @@ public void testProcessUserDefinedTemplatesWithConfig() throws IOException {
731731
// Assert.assertTrue(new File(output, "sampleConfig.json").exists());
732732

733733
// Generator should report api_client.py as a generated file
734-
TestUtils.ensureContainsFile(files, output, "io/something/api_client.py");
734+
TestUtils.ensureContainsFile(files, output, "src/io/something/api_client.py");
735735

736736
// Generated file should exist on the filesystem after generation
737-
File apiClient = new File(output, "io/something/api_client.py");
737+
File apiClient = new File(output, "src/io/something/api_client.py");
738738
Assert.assertTrue(apiClient.exists());
739739

740740
// Generated file should contain our custom packageName

0 commit comments

Comments
 (0)