Skip to content

Add a setting to override the package version #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Additions

- Allow specifying the generated client's version using `package_version_override` in a config file. (#225 - Thanks @fyhertz!)

## 0.6.1 - 2020-09-26

### Changes
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,24 @@ package_name_override: my_extra_special_package_name

### field_prefix

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

Example:

```yaml
field_prefix: attr_
```

### package_version_override

Specify the package version of the generated client. If unset, the client will use the version of the OpenAPI spec.

Example:

```yaml
package_version_override: 1.2.3
```

[changelog.md]: CHANGELOG.md
[poetry]: https://python-poetry.org/
3 changes: 2 additions & 1 deletion openapi_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Project:
TEMPLATE_FILTERS = {"snakecase": utils.snake_case, "kebabcase": utils.kebab_case}
project_name_override: Optional[str] = None
package_name_override: Optional[str] = None
package_version_override: Optional[str] = None

def __init__(self, *, openapi: GeneratorData) -> None:
self.openapi: GeneratorData = openapi
Expand All @@ -42,7 +43,7 @@ def __init__(self, *, openapi: GeneratorData) -> None:
self.package_description: str = utils.remove_string_escapes(
f"A client library for accessing {self.openapi.title}"
)
self.version: str = openapi.version
self.version: str = self.package_version_override or openapi.version

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

Expand Down
2 changes: 2 additions & 0 deletions openapi_python_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Config(BaseModel):
class_overrides: Optional[Dict[str, ClassOverride]]
project_name_override: Optional[str]
package_name_override: Optional[str]
package_version_override: Optional[str]
field_prefix: Optional[str]

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

Project.project_name_override = self.project_name_override
Project.package_name_override = self.package_name_override
Project.package_version_override = self.package_version_override

if self.field_prefix is not None:
utils.FIELD_PREFIX = self.field_prefix
Expand Down
9 changes: 7 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ def test_class_overrides(self):
assert reference.class_overrides["Class1"] == reference.Reference(**override1)
assert reference.class_overrides["Class2"] == reference.Reference(**override2)

def test_project_and_package_name_overrides(self):
config = Config(project_name_override="project-name", package_name_override="package_name")
def test_project_and_package_name_and_package_version_overrides(self):
config = Config(
project_name_override="project-name",
package_name_override="package_name",
package_version_override="package_version",
)
config.load_config()

from openapi_python_client import Project

assert Project.project_name_override == "project-name"
assert Project.package_name_override == "package_name"
assert Project.package_version_override == "package_version"

def test_field_prefix(self):
Config(field_prefix="blah").load_config()
Expand Down