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

Commit d41a95e

Browse files
authored
Generators, adjusts storage of exclusiveMaximum/exclusiveMinimum (#316)
* Adjusts storage of max/min/exclMax/Min * Fixes java tests * Updates python templates * Fiexes anytype template schemas for python * Move java fixes, add org.codehaus.janino back in * Samples regen * Updates python templates to use isSimpleX * Samples updated * Improves exclusive max and min handling in python templates * Fixes handling uniqueItems in python * Adds sample schema with exclusive min and max * Samples updated * Fixes java doc * Adds java tests * Sample regen
1 parent dd92159 commit d41a95e

File tree

35 files changed

+573
-79
lines changed

35 files changed

+573
-79
lines changed

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@
438438
<artifactId>logback-core</artifactId>
439439
<version>${logback.version}</version>
440440
</dependency>
441+
<dependency>
442+
<groupId>org.codehaus.janino</groupId>
443+
<artifactId>janino</artifactId>
444+
<version>3.1.11</version>
445+
</dependency>
441446
</dependencies>
442447
<properties>
443448
<!-- RELEASE_VERSION -->

samples/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/uniqueitems_false_validation.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@
1010
from __future__ import annotations
1111
from unit_test_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]
1212

13-
UniqueitemsFalseValidation: typing_extensions.TypeAlias = schemas.AnyTypeSchema
13+
14+
15+
@dataclasses.dataclass(frozen=True)
16+
class UniqueitemsFalseValidation(
17+
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
18+
):
19+
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
20+
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
21+
22+
Do not edit the class manually.
23+
"""
24+
# any type
25+
unique_items: bool = False
26+

samples/client/3_1_0_unit_test/python/src/unit_test_api/components/schema/uniqueitems_false_validation.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@
1010
from __future__ import annotations
1111
from unit_test_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]
1212

13-
UniqueitemsFalseValidation: typing_extensions.TypeAlias = schemas.AnyTypeSchema
13+
14+
15+
@dataclasses.dataclass(frozen=True)
16+
class UniqueitemsFalseValidation(
17+
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
18+
):
19+
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
20+
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
21+
22+
Do not edit the class manually.
23+
"""
24+
# any type
25+
unique_items: bool = False
26+

samples/client/3_1_0_unit_test/python/src/unit_test_api/components/schema/uniqueitems_false_with_an_array_of_items.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ class UniqueitemsFalseWithAnArrayOfItems(
3131
_0,
3232
_1,
3333
)
34+
unique_items: bool = False
3435

samples/client/petstore/java/.openapi-generator/FILES

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ docs/components/schemas/NullableShape.md
103103
docs/components/schemas/NullableString.md
104104
docs/components/schemas/NumberOnly.md
105105
docs/components/schemas/NumberSchema.md
106+
docs/components/schemas/NumberWithExclusiveMinMax.md
106107
docs/components/schemas/NumberWithValidations.md
107108
docs/components/schemas/ObjWithRequiredProps.md
108109
docs/components/schemas/ObjWithRequiredPropsBase.md
@@ -406,6 +407,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/NullableShape
406407
src/main/java/org/openapijsonschematools/client/components/schemas/NullableString.java
407408
src/main/java/org/openapijsonschematools/client/components/schemas/NumberOnly.java
408409
src/main/java/org/openapijsonschematools/client/components/schemas/NumberSchema.java
410+
src/main/java/org/openapijsonschematools/client/components/schemas/NumberWithExclusiveMinMax.java
409411
src/main/java/org/openapijsonschematools/client/components/schemas/NumberWithValidations.java
410412
src/main/java/org/openapijsonschematools/client/components/schemas/ObjWithRequiredProps.java
411413
src/main/java/org/openapijsonschematools/client/components/schemas/ObjWithRequiredPropsBase.java

samples/client/petstore/java/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ allowed input and output types.
234234
| [NullableString.NullableString1](docs/components/schemas/NullableString.md#nullablestring1) | |
235235
| [NumberSchema.NumberSchema1](docs/components/schemas/NumberSchema.md#numberschema1) | |
236236
| [NumberOnly.NumberOnly1](docs/components/schemas/NumberOnly.md#numberonly1) | |
237+
| [NumberWithExclusiveMinMax.NumberWithExclusiveMinMax1](docs/components/schemas/NumberWithExclusiveMinMax.md#numberwithexclusiveminmax1) | |
237238
| [NumberWithValidations.NumberWithValidations1](docs/components/schemas/NumberWithValidations.md#numberwithvalidations1) | |
238239
| [ObjWithRequiredProps.ObjWithRequiredProps1](docs/components/schemas/ObjWithRequiredProps.md#objwithrequiredprops1) | |
239240
| [ObjWithRequiredPropsBase.ObjWithRequiredPropsBase1](docs/components/schemas/ObjWithRequiredPropsBase.md#objwithrequiredpropsbase1) | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# NumberWithExclusiveMinMax
2+
org.openapijsonschematools.client.components.schemas.NumberWithExclusiveMinMax.java
3+
public class NumberWithExclusiveMinMax
4+
5+
A class that contains necessary nested schema classes, and classes to store validated list and map payloads
6+
7+
## Nested Class Summary
8+
| Modifier and Type | Class and Description |
9+
| ----------------- | ---------------------- |
10+
| static class | [NumberWithExclusiveMinMax.NumberWithExclusiveMinMax1](#numberwithexclusiveminmax1)<br> schema class |
11+
12+
## NumberWithExclusiveMinMax1
13+
public static class NumberWithExclusiveMinMax1<br>
14+
extends JsonSchema
15+
16+
A schema class that validates payloads
17+
### Field Summary
18+
| Modifier and Type | Field and Description |
19+
| ----------------- | ---------------------- |
20+
| static LinkedHashMap<String, KeywordValidator> |keywordToValidator<br/>new LinkedHashMap<>(Map.ofEntries(<br/>&nbsp;&nbsp;&nbsp;&nbsp;new KeywordEntry("type", new TypeValidator(Set.of(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer.class,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Long.class,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Float.class,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Double.class<br/>&nbsp;&nbsp;&nbsp;&nbsp;))),<br/>&nbsp;&nbsp;&nbsp;&nbsp;new KeywordEntry("exclusiveMaximum", new ExclusiveMaximumValidator(12)),<br>&nbsp;&nbsp;&nbsp;&nbsp;new KeywordEntry("exclusiveMinimum", new ExclusiveMinimumValidator(10))<br>)); |
21+
22+
### Method Summary
23+
| Modifier and Type | Method and Description |
24+
| ----------------- | ---------------------- |
25+
| static Number | validate(Number arg, SchemaConfiguration configuration) |
26+
27+
[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.openapijsonschematools.client.components.schemas;
2+
import java.util.LinkedHashMap;
3+
import java.util.Map;
4+
import java.util.Set;
5+
import org.openapijsonschematools.client.configurations.SchemaConfiguration;
6+
import org.openapijsonschematools.client.exceptions.ValidationException;
7+
import org.openapijsonschematools.client.schemas.validation.ExclusiveMaximumValidator;
8+
import org.openapijsonschematools.client.schemas.validation.ExclusiveMinimumValidator;
9+
import org.openapijsonschematools.client.schemas.validation.JsonSchema;
10+
import org.openapijsonschematools.client.schemas.validation.KeywordEntry;
11+
import org.openapijsonschematools.client.schemas.validation.KeywordValidator;
12+
import org.openapijsonschematools.client.schemas.validation.TypeValidator;
13+
14+
public class NumberWithExclusiveMinMax {
15+
// nest classes so all schemas and input/output classes can be public
16+
17+
18+
public static class NumberWithExclusiveMinMax1 extends JsonSchema {
19+
/*
20+
NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
21+
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
22+
23+
Do not edit the class manually.
24+
*/
25+
public static final LinkedHashMap<String, KeywordValidator> keywordToValidator = new LinkedHashMap<>(Map.ofEntries(
26+
new KeywordEntry("type", new TypeValidator(Set.of(
27+
Integer.class,
28+
Long.class,
29+
Float.class,
30+
Double.class
31+
))),
32+
new KeywordEntry("exclusiveMaximum", new ExclusiveMaximumValidator(12)),
33+
new KeywordEntry("exclusiveMinimum", new ExclusiveMinimumValidator(10))
34+
));
35+
public static int validate(int arg, SchemaConfiguration configuration) throws ValidationException {
36+
return JsonSchema.validateInt(NumberWithExclusiveMinMax1.class, arg, configuration);
37+
}
38+
39+
public static long validate(long arg, SchemaConfiguration configuration) throws ValidationException {
40+
return JsonSchema.validateLong(NumberWithExclusiveMinMax1.class, arg, configuration);
41+
}
42+
43+
public static float validate(float arg, SchemaConfiguration configuration) throws ValidationException {
44+
return JsonSchema.validateFloat(NumberWithExclusiveMinMax1.class, arg, configuration);
45+
}
46+
47+
public static double validate(double arg, SchemaConfiguration configuration) throws ValidationException {
48+
return JsonSchema.validateDouble(NumberWithExclusiveMinMax1.class, arg, configuration);
49+
}
50+
}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.openapijsonschematools.client.components.schemas;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags;
6+
import org.openapijsonschematools.client.configurations.SchemaConfiguration;
7+
import org.openapijsonschematools.client.exceptions.ValidationException;
8+
import org.openapijsonschematools.client.schemas.validation.JsonSchema;
9+
import org.openapijsonschematools.client.schemas.MapMaker;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.AbstractMap;
14+
15+
public class NumberWithExclusiveMinMaxTest {
16+
static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
17+
}

samples/client/petstore/python/.openapi-generator/FILES

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ docs/components/schema/nullable_shape.md
136136
docs/components/schema/nullable_string.md
137137
docs/components/schema/number.md
138138
docs/components/schema/number_only.md
139+
docs/components/schema/number_with_exclusive_min_max.md
139140
docs/components/schema/number_with_validations.md
140141
docs/components/schema/obj_with_required_props.md
141142
docs/components/schema/obj_with_required_props_base.md
@@ -646,6 +647,7 @@ src/petstore_api/components/schema/nullable_shape.py
646647
src/petstore_api/components/schema/nullable_string.py
647648
src/petstore_api/components/schema/number.py
648649
src/petstore_api/components/schema/number_only.py
650+
src/petstore_api/components/schema/number_with_exclusive_min_max.py
649651
src/petstore_api/components/schema/number_with_validations.py
650652
src/petstore_api/components/schema/obj_with_required_props.py
651653
src/petstore_api/components/schema/obj_with_required_props_base.py

samples/client/petstore/python/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ Class | Description
323323
[NullableString](docs/components/schema/nullable_string.md) |
324324
[Number](docs/components/schema/number.md) |
325325
[NumberOnly](docs/components/schema/number_only.md) |
326+
[NumberWithExclusiveMinMax](docs/components/schema/number_with_exclusive_min_max.md) |
326327
[NumberWithValidations](docs/components/schema/number_with_validations.md) |
327328
[ObjWithRequiredProps](docs/components/schema/obj_with_required_props.md) |
328329
[ObjWithRequiredPropsBase](docs/components/schema/obj_with_required_props_base.md) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# NumberWithExclusiveMinMax
2+
petstore_api.components.schema.number_with_exclusive_min_max
3+
```
4+
type: schemas.Schema
5+
```
6+
7+
## validate method
8+
Input Type | Return Type | Notes
9+
------------ | ------------- | -------------
10+
float, int | float, int |
11+
12+
[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)

samples/client/petstore/python/src/petstore_api/components/schema/composed_none.py

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ class ComposedNone(
2929
type(None),
3030
})
3131
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# coding: utf-8
2+
3+
"""
4+
OpenAPI Petstore
5+
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: " \ # noqa: E501
6+
The version of the OpenAPI document: 1.0.0
7+
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
8+
"""
9+
10+
from __future__ import annotations
11+
from petstore_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]
12+
13+
14+
15+
@dataclasses.dataclass(frozen=True)
16+
class NumberWithExclusiveMinMax(
17+
schemas.NumberSchema
18+
):
19+
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
20+
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
21+
22+
Do not edit the class manually.
23+
"""
24+
types: typing.FrozenSet[typing.Type] = frozenset({
25+
float,
26+
int,
27+
})
28+
exclusive_maximum: typing.Union[int, float] = 12
29+
exclusive_minimum: typing.Union[int, float] = 10

samples/client/petstore/python/src/petstore_api/components/schemas/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
from petstore_api.components.schema.nullable_string import NullableString
9090
from petstore_api.components.schema.number import Number
9191
from petstore_api.components.schema.number_only import NumberOnly
92+
from petstore_api.components.schema.number_with_exclusive_min_max import NumberWithExclusiveMinMax
9293
from petstore_api.components.schema.number_with_validations import NumberWithValidations
9394
from petstore_api.components.schema.obj_with_required_props import ObjWithRequiredProps
9495
from petstore_api.components.schema.obj_with_required_props_base import ObjWithRequiredPropsBase
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# coding: utf-8
2+
3+
"""
4+
OpenAPI Petstore
5+
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: " \ # noqa: E501
6+
The version of the OpenAPI document: 1.0.0
7+
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
8+
"""
9+
10+
import unittest
11+
12+
import petstore_api
13+
from petstore_api.components.schema.number_with_exclusive_min_max import NumberWithExclusiveMinMax
14+
from petstore_api.configurations import schema_configuration
15+
16+
17+
class TestNumberWithExclusiveMinMax(unittest.TestCase):
18+
"""NumberWithExclusiveMinMax unit test stubs"""
19+
configuration = schema_configuration.SchemaConfiguration(
20+
disabled_json_schema_keywords={'format'}
21+
)
22+
23+
24+
if __name__ == '__main__':
25+
unittest.main()

src/main/java/org/openapijsonschematools/codegen/common/ModelUtils.java

+18-27
Original file line numberDiff line numberDiff line change
@@ -1503,17 +1503,20 @@ public static void syncValidationProperties(Schema schema, CodegenSchema target)
15031503
BigDecimal multipleOf = schema.getMultipleOf();
15041504
BigDecimal minimum = schema.getMinimum();
15051505
BigDecimal maximum = schema.getMaximum();
1506-
Boolean exclusiveMinimum = schema.getExclusiveMinimum();
1507-
if (minimum == null && schema.getExclusiveMinimumValue() != null) {
1508-
minimum = schema.getExclusiveMinimumValue();
1509-
exclusiveMinimum = Boolean.TRUE;
1506+
// 3.1.0 use case
1507+
BigDecimal exclusiveMinimum = schema.getExclusiveMinimumValue();
1508+
// 3.1.0 use case
1509+
BigDecimal exclusiveMaximum = schema.getExclusiveMaximumValue();
1510+
// 3.0.0-3.0.3 use case
1511+
if (Boolean.TRUE.equals(schema.getExclusiveMinimum())) {
1512+
exclusiveMinimum = minimum;
1513+
minimum = null;
15101514
}
1511-
Boolean exclusiveMaximum = schema.getExclusiveMaximum();
1512-
if (maximum == null && schema.getExclusiveMaximumValue() != null) {
1513-
maximum = schema.getExclusiveMaximumValue();
1514-
exclusiveMaximum = Boolean.TRUE;
1515+
// 3.0.0-3.0.3 use case
1516+
if (Boolean.TRUE.equals(schema.getExclusiveMaximum())) {
1517+
exclusiveMaximum = maximum;
1518+
maximum = null;
15151519
}
1516-
15171520
if (isArraySchema(schema)) {
15181521
setArrayValidations(minItems, maxItems, uniqueItems, target);
15191522
} else if (isTypeObjectSchema(schema)) {
@@ -1551,24 +1554,12 @@ private static void setStringValidations(Integer minLength, Integer maxLength, S
15511554
if (maxLength != null) target.maxLength = maxLength;
15521555
}
15531556

1554-
private static void setNumericValidations(Schema schema, BigDecimal multipleOf, BigDecimal minimum, BigDecimal maximum, Boolean exclusiveMinimum, Boolean exclusiveMaximum, CodegenSchema target) {
1555-
if (multipleOf != null) target.multipleOf = multipleOf;
1556-
if (minimum != null) {
1557-
if (isIntegerSchema(schema)) {
1558-
target.minimum = String.valueOf(minimum.longValue());
1559-
} else {
1560-
target.minimum = String.valueOf(minimum);
1561-
}
1562-
if (exclusiveMinimum != null) target.exclusiveMinimum = exclusiveMinimum;
1563-
}
1564-
if (maximum != null) {
1565-
if (isIntegerSchema(schema)) {
1566-
target.maximum = String.valueOf(maximum.longValue());
1567-
} else {
1568-
target.maximum = String.valueOf(maximum);
1569-
}
1570-
if (exclusiveMaximum != null) target.exclusiveMaximum = exclusiveMaximum;
1571-
}
1557+
private static void setNumericValidations(Schema schema, BigDecimal multipleOf, BigDecimal minimum, BigDecimal maximum, BigDecimal exclusiveMinimum, BigDecimal exclusiveMaximum, CodegenSchema target) {
1558+
target.multipleOf = multipleOf;
1559+
target.minimum = minimum;
1560+
target.maximum = maximum;
1561+
target.exclusiveMinimum = exclusiveMinimum;
1562+
target.exclusiveMaximum = exclusiveMaximum;
15721563
}
15731564

15741565
private static ObjectMapper getRightMapper(String data) {

0 commit comments

Comments
 (0)