Skip to content

Commit ef04aa3

Browse files
dbantyfyhertz
andauthored
Add a setting to override the package version (#232)
* Add a setting to override the package version A generate package version actually depends on the version of the OpenAPI spec and on the version of the generator. Upgrading the generator version may break backward compatibility of the client. This gives the user the possibility to bump its client version to reflect changes in the generated code even though the spec has not changed. Co-authored-by: Simon Guigui <[email protected]> Co-authored-by: Dylan Anthony <[email protected]>
1 parent 9beb727 commit ef04aa3

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
### Additions
1818

19+
- Allow specifying the generated client's version using `package_version_override` in a config file. (#225 - Thanks @fyhertz!)
20+
1921
## 0.6.1 - 2020-09-26
2022

2123
### Changes

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,24 @@ package_name_override: my_extra_special_package_name
114114

115115
### field_prefix
116116

117-
When generating properties, the `name` attribute of the OpenAPI schema will be used. When the `name` is not a valid
118-
Python identifier (e.g. begins with a number) this string will be prepended. Defaults to "field_".
117+
When generating properties, the `name` attribute of the OpenAPI schema will be used. When the `name` is not a valid
118+
Python identifier (e.g. begins with a number) this string will be prepended. Defaults to "field\_".
119119

120120
Example:
121121

122122
```yaml
123123
field_prefix: attr_
124124
```
125125

126+
### package_version_override
127+
128+
Specify the package version of the generated client. If unset, the client will use the version of the OpenAPI spec.
129+
130+
Example:
131+
132+
```yaml
133+
package_version_override: 1.2.3
134+
```
135+
126136
[changelog.md]: CHANGELOG.md
127137
[poetry]: https://python-poetry.org/

openapi_python_client/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Project:
2929
TEMPLATE_FILTERS = {"snakecase": utils.snake_case, "kebabcase": utils.kebab_case}
3030
project_name_override: Optional[str] = None
3131
package_name_override: Optional[str] = None
32+
package_version_override: Optional[str] = None
3233

3334
def __init__(self, *, openapi: GeneratorData) -> None:
3435
self.openapi: GeneratorData = openapi
@@ -42,7 +43,7 @@ def __init__(self, *, openapi: GeneratorData) -> None:
4243
self.package_description: str = utils.remove_string_escapes(
4344
f"A client library for accessing {self.openapi.title}"
4445
)
45-
self.version: str = openapi.version
46+
self.version: str = self.package_version_override or openapi.version
4647

4748
self.env.filters.update(self.TEMPLATE_FILTERS)
4849

openapi_python_client/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Config(BaseModel):
1414
class_overrides: Optional[Dict[str, ClassOverride]]
1515
project_name_override: Optional[str]
1616
package_name_override: Optional[str]
17+
package_version_override: Optional[str]
1718
field_prefix: Optional[str]
1819

1920
def load_config(self) -> None:
@@ -29,6 +30,7 @@ def load_config(self) -> None:
2930

3031
Project.project_name_override = self.project_name_override
3132
Project.package_name_override = self.package_name_override
33+
Project.package_version_override = self.package_version_override
3234

3335
if self.field_prefix is not None:
3436
utils.FIELD_PREFIX = self.field_prefix

tests/test_config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ def test_class_overrides(self):
2525
assert reference.class_overrides["Class1"] == reference.Reference(**override1)
2626
assert reference.class_overrides["Class2"] == reference.Reference(**override2)
2727

28-
def test_project_and_package_name_overrides(self):
29-
config = Config(project_name_override="project-name", package_name_override="package_name")
28+
def test_project_and_package_name_and_package_version_overrides(self):
29+
config = Config(
30+
project_name_override="project-name",
31+
package_name_override="package_name",
32+
package_version_override="package_version",
33+
)
3034
config.load_config()
3135

3236
from openapi_python_client import Project
3337

3438
assert Project.project_name_override == "project-name"
3539
assert Project.package_name_override == "package_name"
40+
assert Project.package_version_override == "package_version"
3641

3742
def test_field_prefix(self):
3843
Config(field_prefix="blah").load_config()

0 commit comments

Comments
 (0)