Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,10 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
readOnlyFields.add(cp.name);
}

if (cp.vendorExtensions.containsKey("x-python-not-string-enum-values")) {
moduleImports.add(PYDANTIC, "field_validator");
}

String typing = pydantic.generatePythonType(cp);
cp.vendorExtensions.put(X_PY_TYPING, typing);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,20 @@ public void postProcessParameter(CodegenParameter parameter) {
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);

if (property.getComposedSchemas() != null
&& property.getComposedSchemas().getNot() != null) {
CodegenProperty notProperty = property.getComposedSchemas().getNot();
if (notProperty.isEnum
&& notProperty.isString
&& notProperty.allowableValues != null
&& notProperty.allowableValues.get("values") != null) {
property.vendorExtensions.put(
"x-python-not-string-enum-values",
notProperty.allowableValues.get("values"));
}
}

if (hasOneOf(model) || hasAnyOf(model)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
{{/isContainer}}
return value
{{/isEnum}}
{{#vendorExtensions.x-python-not-string-enum-values.size}}
@field_validator('{{{name}}}')
def {{{name}}}_validate_not_enum(cls, value):
"""Validates values excluded by not"""
{{^required}}
if value is None:
return value

{{/required}}
{{#required}}
{{#isNullable}}
if value is None:
return value

{{/isNullable}}
{{/required}}
if value in set([{{#vendorExtensions.x-python-not-string-enum-values}}{{#lambda.pythonStringLiteral}}{{{.}}}{{/lambda.pythonStringLiteral}}{{^-last}}, {{/-last}}{{/vendorExtensions.x-python-not-string-enum-values}}]):
raise ValueError("must not be one of excluded enum values")
return value
{{/vendorExtensions.x-python-not-string-enum-values.size}}
{{/vars}}

{{#vendorExtensions.x-py-preprocesses-input-names}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,39 @@ public void testInitialConfigValues() throws Exception {
Assert.assertNull(codegen.additionalProperties().get(CodegenConstants.SOURCE_FOLDER));
}

@Test
public void testOneOfNotEnumValidation() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("python")
.setInputSpec("src/test/resources/3_1/python/oneof-not-enum.yaml")
.setOutputDir(output.getAbsolutePath());

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);

Path other = Paths.get(
output.getAbsolutePath(),
"openapi_client",
"models",
"other.py");

String otherContent = Files.readString(other);

Assert.assertTrue(otherContent.contains(
"if value in set([\"known\", \"reserved\"]):"));
Assert.assertTrue(otherContent.contains(
"raise ValueError(\"must not be one of excluded enum values\")"));
Assert.assertTrue(otherContent.contains("@field_validator('kind')"));

Assert.assertEquals(
otherContent.split("def kind_validate_not_enum", -1).length - 1,
1);
}

@Test
public void testSettersForConfigValues() throws Exception {
final PythonClientCodegen codegen = new PythonClientCodegen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2975,3 +2975,30 @@ components:
type: string
recursive:
type: boolean

Known:
type: object
required:
- kind
properties:
kind:
type: string
enum:
- known

Other:
type: object
required:
- kind
properties:
kind:
type: string
not:
enum:
- known
- reserved

TaggedUnion:
oneOf:
- $ref: '#/components/schemas/Known'
- $ref: '#/components/schemas/Other'
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: 3.1.0

info:
title: oneOf not enum repro
version: 1.0.0

paths: {}

components:
schemas:
Known:
type: object
required:
- kind
properties:
kind:
type: string
enum:
- known

Other:
type: object
required:
- kind
properties:
kind:
type: string
not:
enum:
- known
- reserved

TaggedUnion:
oneOf:
- $ref: '#/components/schemas/Known'
- $ref: '#/components/schemas/Other'
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ docs/Info.md
docs/InnerDictWithProperty.md
docs/InputAllOf.md
docs/IntOrString.md
docs/Known.md
docs/ListClass.md
docs/MapOfArrayOfModel.md
docs/MapTest.md
Expand All @@ -82,6 +83,7 @@ docs/ObjectToTestAdditionalProperties.md
docs/ObjectWithDeprecatedFields.md
docs/OneOfEnumString.md
docs/Order.md
docs/Other.md
docs/OuterComposite.md
docs/OuterEnum.md
docs/OuterEnumDefaultValue.md
Expand All @@ -108,6 +110,7 @@ docs/SpecialModelName.md
docs/SpecialName.md
docs/StoreApi.md
docs/Tag.md
docs/TaggedUnion.md
docs/Task.md
docs/TaskActivity.md
docs/TestEnum.md
Expand Down Expand Up @@ -199,6 +202,7 @@ petstore_api/models/info.py
petstore_api/models/inner_dict_with_property.py
petstore_api/models/input_all_of.py
petstore_api/models/int_or_string.py
petstore_api/models/known.py
petstore_api/models/list_class.py
petstore_api/models/map_of_array_of_model.py
petstore_api/models/map_test.py
Expand All @@ -216,6 +220,7 @@ petstore_api/models/object_to_test_additional_properties.py
petstore_api/models/object_with_deprecated_fields.py
petstore_api/models/one_of_enum_string.py
petstore_api/models/order.py
petstore_api/models/other.py
petstore_api/models/outer_composite.py
petstore_api/models/outer_enum.py
petstore_api/models/outer_enum_default_value.py
Expand All @@ -240,6 +245,7 @@ petstore_api/models/special_character_enum.py
petstore_api/models/special_model_name.py
petstore_api/models/special_name.py
petstore_api/models/tag.py
petstore_api/models/tagged_union.py
petstore_api/models/task.py
petstore_api/models/task_activity.py
petstore_api/models/test_enum.py
Expand Down
3 changes: 3 additions & 0 deletions samples/openapi3/client/petstore/python-aiohttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Class | Method | HTTP request | Description
- [InnerDictWithProperty](docs/InnerDictWithProperty.md)
- [InputAllOf](docs/InputAllOf.md)
- [IntOrString](docs/IntOrString.md)
- [Known](docs/Known.md)
- [ListClass](docs/ListClass.md)
- [MapOfArrayOfModel](docs/MapOfArrayOfModel.md)
- [MapTest](docs/MapTest.md)
Expand All @@ -253,6 +254,7 @@ Class | Method | HTTP request | Description
- [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
- [OneOfEnumString](docs/OneOfEnumString.md)
- [Order](docs/Order.md)
- [Other](docs/Other.md)
- [OuterComposite](docs/OuterComposite.md)
- [OuterEnum](docs/OuterEnum.md)
- [OuterEnumDefaultValue](docs/OuterEnumDefaultValue.md)
Expand All @@ -277,6 +279,7 @@ Class | Method | HTTP request | Description
- [SpecialModelName](docs/SpecialModelName.md)
- [SpecialName](docs/SpecialName.md)
- [Tag](docs/Tag.md)
- [TaggedUnion](docs/TaggedUnion.md)
- [Task](docs/Task.md)
- [TaskActivity](docs/TaskActivity.md)
- [TestEnum](docs/TestEnum.md)
Expand Down
29 changes: 29 additions & 0 deletions samples/openapi3/client/petstore/python-aiohttp/docs/Known.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Known


## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**kind** | **str** | |

## Example

```python
from petstore_api.models.known import Known

# TODO update the JSON string below
json = "{}"
# create an instance of Known from a JSON string
known_instance = Known.from_json(json)
# print the JSON string representation of the object
print(Known.to_json())

# convert the object into a dict
known_dict = known_instance.to_dict()
# create an instance of Known from a dict
known_from_dict = Known.from_dict(known_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


29 changes: 29 additions & 0 deletions samples/openapi3/client/petstore/python-aiohttp/docs/Other.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Other


## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**kind** | **str** | |

## Example

```python
from petstore_api.models.other import Other

# TODO update the JSON string below
json = "{}"
# create an instance of Other from a JSON string
other_instance = Other.from_json(json)
# print the JSON string representation of the object
print(Other.to_json())

# convert the object into a dict
other_dict = other_instance.to_dict()
# create an instance of Other from a dict
other_from_dict = Other.from_dict(other_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TaggedUnion


## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**kind** | **str** | |

## Example

```python
from petstore_api.models.tagged_union import TaggedUnion

# TODO update the JSON string below
json = "{}"
# create an instance of TaggedUnion from a JSON string
tagged_union_instance = TaggedUnion.from_json(json)
# print the JSON string representation of the object
print(TaggedUnion.to_json())

# convert the object into a dict
tagged_union_dict = tagged_union_instance.to_dict()
# create an instance of TaggedUnion from a dict
tagged_union_from_dict = TaggedUnion.from_dict(tagged_union_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"InnerDictWithProperty",
"InputAllOf",
"IntOrString",
"Known",
"ListClass",
"MapOfArrayOfModel",
"MapTest",
Expand All @@ -110,6 +111,7 @@
"ObjectWithDeprecatedFields",
"OneOfEnumString",
"Order",
"Other",
"OuterComposite",
"OuterEnum",
"OuterEnumDefaultValue",
Expand All @@ -134,6 +136,7 @@
"SpecialModelName",
"SpecialName",
"Tag",
"TaggedUnion",
"Task",
"TaskActivity",
"TestEnum",
Expand Down Expand Up @@ -233,6 +236,7 @@
from petstore_api.models.inner_dict_with_property import InnerDictWithProperty as InnerDictWithProperty
from petstore_api.models.input_all_of import InputAllOf as InputAllOf
from petstore_api.models.int_or_string import IntOrString as IntOrString
from petstore_api.models.known import Known as Known
from petstore_api.models.list_class import ListClass as ListClass
from petstore_api.models.map_of_array_of_model import MapOfArrayOfModel as MapOfArrayOfModel
from petstore_api.models.map_test import MapTest as MapTest
Expand All @@ -250,6 +254,7 @@
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields as ObjectWithDeprecatedFields
from petstore_api.models.one_of_enum_string import OneOfEnumString as OneOfEnumString
from petstore_api.models.order import Order as Order
from petstore_api.models.other import Other as Other
from petstore_api.models.outer_composite import OuterComposite as OuterComposite
from petstore_api.models.outer_enum import OuterEnum as OuterEnum
from petstore_api.models.outer_enum_default_value import OuterEnumDefaultValue as OuterEnumDefaultValue
Expand All @@ -274,6 +279,7 @@
from petstore_api.models.special_model_name import SpecialModelName as SpecialModelName
from petstore_api.models.special_name import SpecialName as SpecialName
from petstore_api.models.tag import Tag as Tag
from petstore_api.models.tagged_union import TaggedUnion as TaggedUnion
from petstore_api.models.task import Task as Task
from petstore_api.models.task_activity import TaskActivity as TaskActivity
from petstore_api.models.test_enum import TestEnum as TestEnum
Expand Down
Loading
Loading