Skip to content

Commit a567872

Browse files
committed
Merge branch 'feature/python-buildystem' of https://github.com/tughril/openapi-generator into tughril-feature/python-buildystem
2 parents a79d60b + ec7220f commit a567872

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

docs/generators/python.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1919

2020
| Option | Description | Values | Default |
2121
| ------ | ----------- | ------ | ------- |
22+
|buildSystem|Build system to use in pyproject.toml (setuptools, hatchling).| |setuptools|
2223
|dateFormat|date format for query parameters| |%Y-%m-%d|
2324
|datetimeFormat|datetime format for query parameters| |%Y-%m-%dT%H:%M:%S%z|
2425
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class PythonClientCodegen extends AbstractPythonCodegen implements Codege
4949
public static final String SET_ENSURE_ASCII_TO_FALSE = "setEnsureAsciiToFalse";
5050
public static final String POETRY1_FALLBACK = "poetry1";
5151
public static final String LAZY_IMPORTS = "lazyImports";
52+
public static final String BUILD_SYSTEM = "buildSystem";
5253

5354
@Setter protected String packageUrl;
5455
protected String apiDocPath = "docs/";
@@ -153,6 +154,7 @@ public PythonClientCodegen() {
153154
cliOptions.add(new CliOption(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC).defaultValue("false"));
154155
cliOptions.add(new CliOption(POETRY1_FALLBACK, "Fallback to formatting pyproject.toml to Poetry 1.x format."));
155156
cliOptions.add(new CliOption(LAZY_IMPORTS, "Enable lazy imports.").defaultValue(Boolean.FALSE.toString()));
157+
cliOptions.add(new CliOption(BUILD_SYSTEM, "Build system to use in pyproject.toml (setuptools, hatchling).").defaultValue("setuptools"));
156158

157159
supportedLibraries.put("urllib3", "urllib3-based client");
158160
supportedLibraries.put("asyncio", "asyncio-based client");
@@ -271,6 +273,13 @@ public void processOpts() {
271273
additionalProperties.put(LAZY_IMPORTS, Boolean.valueOf(additionalProperties.get(LAZY_IMPORTS).toString()));
272274
}
273275

276+
if (additionalProperties.containsKey(BUILD_SYSTEM)) {
277+
String buildSystem = (String) additionalProperties.get(BUILD_SYSTEM);
278+
if ("hatchling".equals(buildSystem)) {
279+
additionalProperties.put("hatchling", true);
280+
}
281+
}
282+
274283
String modelPath = packagePath() + File.separatorChar + modelPackage.replace('.', File.separatorChar);
275284
String apiPath = packagePath() + File.separatorChar + apiPackage.replace('.', File.separatorChar);
276285

modules/openapi-generator/src/main/resources/python/pyproject.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ mypy = ">= 1.5"
110110

111111

112112
[build-system]
113+
{{#hatchling}}
114+
requires = ["hatchling"]
115+
build-backend = "hatchling.build"
116+
{{/hatchling}}
117+
{{^hatchling}}
113118
requires = ["setuptools"]
114119
build-backend = "setuptools.build_meta"
120+
{{/hatchling}}
115121

116122
[tool.pylint.'MESSAGES CONTROL']
117123
extension-pkg-whitelist = "pydantic"

modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,47 @@ public void testUuidWithPatternImportsFieldValidator() throws IOException {
670670
assertFileContains(p, "from pydantic import BaseModel, ConfigDict, field_validator");
671671
}
672672

673+
@Test(description = "Verify default buildSystem uses setuptools")
674+
public void testDefaultBuildSystemSetuptools() throws IOException {
675+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
676+
output.deleteOnExit();
677+
678+
final CodegenConfigurator configurator = new CodegenConfigurator()
679+
.setGeneratorName("python")
680+
.setInputSpec("src/test/resources/bugs/issue_21619.yaml")
681+
.setOutputDir(output.getAbsolutePath());
682+
683+
DefaultGenerator generator = new DefaultGenerator();
684+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
685+
files.forEach(File::deleteOnExit);
686+
687+
Path pyprojectPath = Paths.get(output.getAbsolutePath(), "pyproject.toml");
688+
TestUtils.assertFileExists(pyprojectPath);
689+
TestUtils.assertFileContains(pyprojectPath, "requires = [\"setuptools\"]");
690+
TestUtils.assertFileContains(pyprojectPath, "build-backend = \"setuptools.build_meta\"");
691+
}
692+
693+
@Test(description = "Verify buildSystem=hatchling uses hatchling")
694+
public void testBuildSystemHatchling() throws IOException {
695+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
696+
output.deleteOnExit();
697+
698+
final CodegenConfigurator configurator = new CodegenConfigurator()
699+
.setGeneratorName("python")
700+
.setInputSpec("src/test/resources/bugs/issue_21619.yaml")
701+
.setOutputDir(output.getAbsolutePath())
702+
.addAdditionalProperty("buildSystem", "hatchling");
703+
704+
DefaultGenerator generator = new DefaultGenerator();
705+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
706+
files.forEach(File::deleteOnExit);
707+
708+
Path pyprojectPath = Paths.get(output.getAbsolutePath(), "pyproject.toml");
709+
TestUtils.assertFileExists(pyprojectPath);
710+
TestUtils.assertFileContains(pyprojectPath, "requires = [\"hatchling\"]");
711+
TestUtils.assertFileContains(pyprojectPath, "build-backend = \"hatchling.build\"");
712+
}
713+
673714
@Test(description = "Verify non-poetry1 mode uses object notation for license")
674715
public void testNonPoetry1LicenseFormat() throws IOException {
675716
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

0 commit comments

Comments
 (0)