Skip to content
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

Generators, adjusts storage of exclusiveMaximum/exclusiveMinimum [breaking] #316

Merged
merged 15 commits into from
Dec 5, 2023
Merged
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.11</version>
</dependency>
</dependencies>
<properties>
<!-- RELEASE_VERSION -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@
from __future__ import annotations
from unit_test_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]

UniqueitemsFalseValidation: typing_extensions.TypeAlias = schemas.AnyTypeSchema


@dataclasses.dataclass(frozen=True)
class UniqueitemsFalseValidation(
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
):
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

Do not edit the class manually.
"""
# any type
unique_items: bool = False

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@
from __future__ import annotations
from unit_test_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]

UniqueitemsFalseValidation: typing_extensions.TypeAlias = schemas.AnyTypeSchema


@dataclasses.dataclass(frozen=True)
class UniqueitemsFalseValidation(
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
):
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

Do not edit the class manually.
"""
# any type
unique_items: bool = False

Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ class UniqueitemsFalseWithAnArrayOfItems(
_0,
_1,
)
unique_items: bool = False

2 changes: 2 additions & 0 deletions samples/client/petstore/java/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ docs/components/schemas/NullableShape.md
docs/components/schemas/NullableString.md
docs/components/schemas/NumberOnly.md
docs/components/schemas/NumberSchema.md
docs/components/schemas/NumberWithExclusiveMinMax.md
docs/components/schemas/NumberWithValidations.md
docs/components/schemas/ObjWithRequiredProps.md
docs/components/schemas/ObjWithRequiredPropsBase.md
Expand Down Expand Up @@ -406,6 +407,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/NullableShape
src/main/java/org/openapijsonschematools/client/components/schemas/NullableString.java
src/main/java/org/openapijsonschematools/client/components/schemas/NumberOnly.java
src/main/java/org/openapijsonschematools/client/components/schemas/NumberSchema.java
src/main/java/org/openapijsonschematools/client/components/schemas/NumberWithExclusiveMinMax.java
src/main/java/org/openapijsonschematools/client/components/schemas/NumberWithValidations.java
src/main/java/org/openapijsonschematools/client/components/schemas/ObjWithRequiredProps.java
src/main/java/org/openapijsonschematools/client/components/schemas/ObjWithRequiredPropsBase.java
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ allowed input and output types.
| [NullableString.NullableString1](docs/components/schemas/NullableString.md#nullablestring1) | |
| [NumberSchema.NumberSchema1](docs/components/schemas/NumberSchema.md#numberschema1) | |
| [NumberOnly.NumberOnly1](docs/components/schemas/NumberOnly.md#numberonly1) | |
| [NumberWithExclusiveMinMax.NumberWithExclusiveMinMax1](docs/components/schemas/NumberWithExclusiveMinMax.md#numberwithexclusiveminmax1) | |
| [NumberWithValidations.NumberWithValidations1](docs/components/schemas/NumberWithValidations.md#numberwithvalidations1) | |
| [ObjWithRequiredProps.ObjWithRequiredProps1](docs/components/schemas/ObjWithRequiredProps.md#objwithrequiredprops1) | |
| [ObjWithRequiredPropsBase.ObjWithRequiredPropsBase1](docs/components/schemas/ObjWithRequiredPropsBase.md#objwithrequiredpropsbase1) | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# NumberWithExclusiveMinMax
org.openapijsonschematools.client.components.schemas.NumberWithExclusiveMinMax.java
public class NumberWithExclusiveMinMax

A class that contains necessary nested schema classes, and classes to store validated list and map payloads

## Nested Class Summary
| Modifier and Type | Class and Description |
| ----------------- | ---------------------- |
| static class | [NumberWithExclusiveMinMax.NumberWithExclusiveMinMax1](#numberwithexclusiveminmax1)<br> schema class |

## NumberWithExclusiveMinMax1
public static class NumberWithExclusiveMinMax1<br>
extends JsonSchema

A schema class that validates payloads
### Field Summary
| Modifier and Type | Field and Description |
| ----------------- | ---------------------- |
| 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>)); |

### Method Summary
| Modifier and Type | Method and Description |
| ----------------- | ---------------------- |
| static Number | validate(Number arg, SchemaConfiguration configuration) |

[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.openapijsonschematools.client.components.schemas;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.openapijsonschematools.client.configurations.SchemaConfiguration;
import org.openapijsonschematools.client.exceptions.ValidationException;
import org.openapijsonschematools.client.schemas.validation.ExclusiveMaximumValidator;
import org.openapijsonschematools.client.schemas.validation.ExclusiveMinimumValidator;
import org.openapijsonschematools.client.schemas.validation.JsonSchema;
import org.openapijsonschematools.client.schemas.validation.KeywordEntry;
import org.openapijsonschematools.client.schemas.validation.KeywordValidator;
import org.openapijsonschematools.client.schemas.validation.TypeValidator;

public class NumberWithExclusiveMinMax {
// nest classes so all schemas and input/output classes can be public


public static class NumberWithExclusiveMinMax1 extends JsonSchema {
/*
NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

Do not edit the class manually.
*/
public static final LinkedHashMap<String, KeywordValidator> keywordToValidator = new LinkedHashMap<>(Map.ofEntries(
new KeywordEntry("type", new TypeValidator(Set.of(
Integer.class,
Long.class,
Float.class,
Double.class
))),
new KeywordEntry("exclusiveMaximum", new ExclusiveMaximumValidator(12)),
new KeywordEntry("exclusiveMinimum", new ExclusiveMinimumValidator(10))
));
public static int validate(int arg, SchemaConfiguration configuration) throws ValidationException {
return JsonSchema.validateInt(NumberWithExclusiveMinMax1.class, arg, configuration);
}

public static long validate(long arg, SchemaConfiguration configuration) throws ValidationException {
return JsonSchema.validateLong(NumberWithExclusiveMinMax1.class, arg, configuration);
}

public static float validate(float arg, SchemaConfiguration configuration) throws ValidationException {
return JsonSchema.validateFloat(NumberWithExclusiveMinMax1.class, arg, configuration);
}

public static double validate(double arg, SchemaConfiguration configuration) throws ValidationException {
return JsonSchema.validateDouble(NumberWithExclusiveMinMax1.class, arg, configuration);
}
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.openapijsonschematools.client.components.schemas;

import org.junit.Assert;
import org.junit.Test;
import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags;
import org.openapijsonschematools.client.configurations.SchemaConfiguration;
import org.openapijsonschematools.client.exceptions.ValidationException;
import org.openapijsonschematools.client.schemas.validation.JsonSchema;
import org.openapijsonschematools.client.schemas.MapMaker;

import java.util.Arrays;
import java.util.List;
import java.util.AbstractMap;

public class NumberWithExclusiveMinMaxTest {
static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
}
2 changes: 2 additions & 0 deletions samples/client/petstore/python/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ docs/components/schema/nullable_shape.md
docs/components/schema/nullable_string.md
docs/components/schema/number.md
docs/components/schema/number_only.md
docs/components/schema/number_with_exclusive_min_max.md
docs/components/schema/number_with_validations.md
docs/components/schema/obj_with_required_props.md
docs/components/schema/obj_with_required_props_base.md
Expand Down Expand Up @@ -646,6 +647,7 @@ src/petstore_api/components/schema/nullable_shape.py
src/petstore_api/components/schema/nullable_string.py
src/petstore_api/components/schema/number.py
src/petstore_api/components/schema/number_only.py
src/petstore_api/components/schema/number_with_exclusive_min_max.py
src/petstore_api/components/schema/number_with_validations.py
src/petstore_api/components/schema/obj_with_required_props.py
src/petstore_api/components/schema/obj_with_required_props_base.py
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ Class | Description
[NullableString](docs/components/schema/nullable_string.md) |
[Number](docs/components/schema/number.md) |
[NumberOnly](docs/components/schema/number_only.md) |
[NumberWithExclusiveMinMax](docs/components/schema/number_with_exclusive_min_max.md) |
[NumberWithValidations](docs/components/schema/number_with_validations.md) |
[ObjWithRequiredProps](docs/components/schema/obj_with_required_props.md) |
[ObjWithRequiredPropsBase](docs/components/schema/obj_with_required_props_base.md) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# NumberWithExclusiveMinMax
petstore_api.components.schema.number_with_exclusive_min_max
```
type: schemas.Schema
```

## validate method
Input Type | Return Type | Notes
------------ | ------------- | -------------
float, int | float, int |

[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ class ComposedNone(
type(None),
})
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding: utf-8

"""
OpenAPI Petstore
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
The version of the OpenAPI document: 1.0.0
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

from __future__ import annotations
from petstore_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]



@dataclasses.dataclass(frozen=True)
class NumberWithExclusiveMinMax(
schemas.NumberSchema
):
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

Do not edit the class manually.
"""
types: typing.FrozenSet[typing.Type] = frozenset({
float,
int,
})
exclusive_maximum: typing.Union[int, float] = 12
exclusive_minimum: typing.Union[int, float] = 10
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
from petstore_api.components.schema.nullable_string import NullableString
from petstore_api.components.schema.number import Number
from petstore_api.components.schema.number_only import NumberOnly
from petstore_api.components.schema.number_with_exclusive_min_max import NumberWithExclusiveMinMax
from petstore_api.components.schema.number_with_validations import NumberWithValidations
from petstore_api.components.schema.obj_with_required_props import ObjWithRequiredProps
from petstore_api.components.schema.obj_with_required_props_base import ObjWithRequiredPropsBase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# coding: utf-8

"""
OpenAPI Petstore
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
The version of the OpenAPI document: 1.0.0
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

import unittest

import petstore_api
from petstore_api.components.schema.number_with_exclusive_min_max import NumberWithExclusiveMinMax
from petstore_api.configurations import schema_configuration


class TestNumberWithExclusiveMinMax(unittest.TestCase):
"""NumberWithExclusiveMinMax unit test stubs"""
configuration = schema_configuration.SchemaConfiguration(
disabled_json_schema_keywords={'format'}
)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -1503,17 +1503,20 @@ public static void syncValidationProperties(Schema schema, CodegenSchema target)
BigDecimal multipleOf = schema.getMultipleOf();
BigDecimal minimum = schema.getMinimum();
BigDecimal maximum = schema.getMaximum();
Boolean exclusiveMinimum = schema.getExclusiveMinimum();
if (minimum == null && schema.getExclusiveMinimumValue() != null) {
minimum = schema.getExclusiveMinimumValue();
exclusiveMinimum = Boolean.TRUE;
// 3.1.0 use case
BigDecimal exclusiveMinimum = schema.getExclusiveMinimumValue();
// 3.1.0 use case
BigDecimal exclusiveMaximum = schema.getExclusiveMaximumValue();
// 3.0.0-3.0.3 use case
if (Boolean.TRUE.equals(schema.getExclusiveMinimum())) {
exclusiveMinimum = minimum;
minimum = null;
}
Boolean exclusiveMaximum = schema.getExclusiveMaximum();
if (maximum == null && schema.getExclusiveMaximumValue() != null) {
maximum = schema.getExclusiveMaximumValue();
exclusiveMaximum = Boolean.TRUE;
// 3.0.0-3.0.3 use case
if (Boolean.TRUE.equals(schema.getExclusiveMaximum())) {
exclusiveMaximum = maximum;
maximum = null;
}

if (isArraySchema(schema)) {
setArrayValidations(minItems, maxItems, uniqueItems, target);
} else if (isTypeObjectSchema(schema)) {
Expand Down Expand Up @@ -1551,24 +1554,12 @@ private static void setStringValidations(Integer minLength, Integer maxLength, S
if (maxLength != null) target.maxLength = maxLength;
}

private static void setNumericValidations(Schema schema, BigDecimal multipleOf, BigDecimal minimum, BigDecimal maximum, Boolean exclusiveMinimum, Boolean exclusiveMaximum, CodegenSchema target) {
if (multipleOf != null) target.multipleOf = multipleOf;
if (minimum != null) {
if (isIntegerSchema(schema)) {
target.minimum = String.valueOf(minimum.longValue());
} else {
target.minimum = String.valueOf(minimum);
}
if (exclusiveMinimum != null) target.exclusiveMinimum = exclusiveMinimum;
}
if (maximum != null) {
if (isIntegerSchema(schema)) {
target.maximum = String.valueOf(maximum.longValue());
} else {
target.maximum = String.valueOf(maximum);
}
if (exclusiveMaximum != null) target.exclusiveMaximum = exclusiveMaximum;
}
private static void setNumericValidations(Schema schema, BigDecimal multipleOf, BigDecimal minimum, BigDecimal maximum, BigDecimal exclusiveMinimum, BigDecimal exclusiveMaximum, CodegenSchema target) {
target.multipleOf = multipleOf;
target.minimum = minimum;
target.maximum = maximum;
target.exclusiveMinimum = exclusiveMinimum;
target.exclusiveMaximum = exclusiveMaximum;
}

private static ObjectMapper getRightMapper(String data) {
Expand Down
Loading