diff --git a/docs/generators/java.md b/docs/generators/java.md index e4871eae415..a5980b7356b 100644 --- a/docs/generators/java.md +++ b/docs/generators/java.md @@ -168,7 +168,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl ### Client Modification Feature | Name | Supported | Defined By | | ---- | --------- | ---------- | -|BasePath|✓|ToolingExtension +|BasePath|✗|ToolingExtension |Authorizations|✗|ToolingExtension |UserAgent|✗|ToolingExtension |MockServer|✗|ToolingExtension @@ -216,7 +216,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl | Name | Supported | Defined By | | ---- | --------- | ---------- | |Readme|✓|ToolingExtension -|Servers|✗|OAS3 +|Servers|✓|OAS3 |Security|✗|OAS2,OAS3 |ComponentSchemas|✓|OAS3 |ComponentResponses|✗|OAS3 @@ -233,7 +233,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl | Name | Supported | Defined By | | ---- | --------- | ---------- | |Info|✓|OAS2,OAS3 -|Servers|✗|OAS3 +|Servers|✓|OAS3 |Paths|✗|OAS2,OAS3 |Webhooks|✗|OAS3 |Components|✓|OAS3 diff --git a/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES b/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES index 40c9532cb8c..e0594ef7f8a 100644 --- a/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES +++ b/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES @@ -86,6 +86,7 @@ docs/components/schemas/UniqueitemsValidation.md docs/components/schemas/UriFormat.md docs/components/schemas/UriReferenceFormat.md docs/components/schemas/UriTemplateFormat.md +docs/servers/Server0.md pom.xml src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.java src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAreAllowedByDefault.java @@ -209,6 +210,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/BooleanEnumVa src/main/java/org/openapijsonschematools/client/schemas/validation/BooleanSchemaValidator.java src/main/java/org/openapijsonschematools/client/schemas/validation/BooleanValueMethod.java src/main/java/org/openapijsonschematools/client/schemas/validation/CustomIsoparser.java +src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleEnumValidator.java src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleValueMethod.java src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java @@ -261,6 +263,9 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java +src/main/java/org/openapijsonschematools/client/servers/Server0.java +src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java +src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java src/test/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlagsTest.java src/test/java/org/openapijsonschematools/client/schemas/AnyTypeSchemaTest.java src/test/java/org/openapijsonschematools/client/schemas/ArrayTypeSchemaTest.java diff --git a/samples/client/3_0_3_unit_test/java/README.md b/samples/client/3_0_3_unit_test/java/README.md index 46cccf3a4d0..4dc95d03794 100644 --- a/samples/client/3_0_3_unit_test/java/README.md +++ b/samples/client/3_0_3_unit_test/java/README.md @@ -148,6 +148,11 @@ org.openapijsonschematools.client.components.schemas to validate input payloads output classes. Json schemas allow multiple types for one schema, so a schema's validate method can have allowed input and output types. +## Servers +| server_index | Class | Description | +| ------------ | ----- | ----------- | +| 0 | [Server0](docs/servers/Server0.md) | | + ## Component Schemas | Class | Description | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md index 499c7276d53..27c56b460ea 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md @@ -111,6 +111,7 @@ String validatedPayload = InvalidStringValueForDefault.Bar.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Integer | minLength = 4 | +| @Nullable Object | defaultValue = "bad" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md index b9f13a0b64c..12d3e838941 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md @@ -133,6 +133,7 @@ int validatedPayload = TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing. | ----------------- | ---------------------- | | Set> | type = Set.of(
    Integer.class,
    Long.class,
    Float.class,
    Double.class
)
| | Number | maximum = 3 | +| @Nullable Object | defaultValue = 5.0 | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/3_0_3_unit_test/java/docs/servers/Server0.md b/samples/client/3_0_3_unit_test/java/docs/servers/Server0.md new file mode 100644 index 00000000000..91b84467efa --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/docs/servers/Server0.md @@ -0,0 +1,17 @@ +org.openapijsonschematools.client.servers.Server0 +# Server Server0 +public class Server0 + +A class that stores a server url + +### Constructor Summary +| Constructor and Description | +| --------------------------- | +| Server0()
Creates a server | + +### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | --------------------- | +| String | url = "https://someserver.com/v1" | + +[[Back to top]](#top) [[Back to Servers]](../../README.md#Servers) [[Back to README]](../../README.md) diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java index 213f19c8f1b..013bb5bfbfb 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java @@ -20,6 +20,7 @@ import org.openapijsonschematools.client.schemas.BaseBuilder; import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenList; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; @@ -37,7 +38,7 @@ public class InvalidStringValueForDefault { // nest classes so all schemas and input/output classes can be public - public static class Bar extends JsonSchema implements StringSchemaValidator { + public static class Bar extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { private static @Nullable Bar instance = null; protected Bar() { @@ -46,6 +47,7 @@ protected Bar() { String.class )) .minLength(4) + .defaultValue("bad") ); } @@ -81,6 +83,12 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class InvalidStringValueForDefaultMap extends FrozenMap<@Nullable Object> { diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java index 9b00417733e..aa59ce4ae7a 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java @@ -16,6 +16,7 @@ import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.BaseBuilder; import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; @@ -41,6 +42,7 @@ protected Alpha() { Double.class )) .maximum(3) + .defaultValue(5.0) ); } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java new file mode 100644 index 00000000000..43fe8423969 --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java @@ -0,0 +1,5 @@ +package org.openapijsonschematools.client.schemas.validation; + +public interface DefaultValueMethod { + T defaultValue(); +} \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java index 479de92c1b5..b924c070ae1 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java @@ -42,6 +42,7 @@ public abstract class JsonSchema { public final @Nullable Boolean uniqueItems; public final @Nullable Set<@Nullable Object> enumValues; public final @Nullable Pattern pattern; + public final @Nullable Object defaultValue; private final LinkedHashMap keywordToValidator; protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { @@ -214,6 +215,7 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { new PatternValidator(this.pattern) ); } + this.defaultValue = jsonSchemaInfo.defaultValue; this.keywordToValidator = keywordToValidator; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java index 3685df5d0bb..27a18717d51 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java @@ -128,4 +128,9 @@ public JsonSchemaInfo pattern(Pattern pattern) { this.pattern = pattern; return this; } + public @Nullable Object defaultValue = null; + public JsonSchemaInfo defaultValue(@Nullable Object defaultValue) { + this.defaultValue = defaultValue; + return this; + } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java new file mode 100644 index 00000000000..9e5175e6180 --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java @@ -0,0 +1,9 @@ +package org.openapijsonschematools.client.servers; + +import org.openapijsonschematools.client.servers.ServerWithoutVariables; + +class Server0 extends ServerWithoutVariables { + public Server0() { + super("https://someserver.com/v1"); + } +} diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java new file mode 100644 index 00000000000..acdb6d730ed --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java @@ -0,0 +1,17 @@ +package org.openapijsonschematools.client.servers; + +import java.util.Map; + +public abstract class ServerWithVariables> { + public final String url; + public final T variables; + + protected ServerWithVariables(String url, T variables) { + this.variables = variables; + for (Map.Entry entry: variables.entrySet()) { + url = url.replace("{" + entry.getKey() + "}", entry.getValue()); + } + this.url = url; + } +} + diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java new file mode 100644 index 00000000000..903ac3f4c7d --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java @@ -0,0 +1,10 @@ +package org.openapijsonschematools.client.servers; + +public abstract class ServerWithoutVariables { + public final String url; + + protected ServerWithoutVariables(String url) { + this.url = url; + } +} + diff --git a/samples/client/petstore/java/.openapi-generator/FILES b/samples/client/petstore/java/.openapi-generator/FILES index 43f29ae94f6..f9fe45351ef 100644 --- a/samples/client/petstore/java/.openapi-generator/FILES +++ b/samples/client/petstore/java/.openapi-generator/FILES @@ -301,6 +301,9 @@ docs/paths/userlogin/get/responses/response200/headers/xratelimit/content/applic docs/paths/userusername/get/responses/response200/content/applicationjson/Schema.md docs/paths/userusername/get/responses/response200/content/applicationxml/Schema.md docs/paths/userusername/put/requestbody/content/applicationjson/Schema.md +docs/servers/Server0.md +docs/servers/Server1.md +docs/servers/Server2.md pom.xml src/main/java/org/openapijsonschematools/client/components/headers/int32jsoncontenttypeheader/content/applicationjson/Int32JsonContentTypeHeaderSchema.java src/main/java/org/openapijsonschematools/client/components/headers/numberheader/NumberHeaderSchema.java @@ -609,8 +612,14 @@ src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/ src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response4xx/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response5xx/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/foo/get/responses/responsedefault/content/applicationjson/Schema.java +src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java +src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java +src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/server1/Variables.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/QueryParameters.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/parameters/parameter0/Schema0.java +src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java +src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java +src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/server1/Variables.java src/main/java/org/openapijsonschematools/client/paths/petfindbytags/get/QueryParameters.java src/main/java/org/openapijsonschematools/client/paths/petfindbytags/get/parameters/parameter0/Schema0.java src/main/java/org/openapijsonschematools/client/paths/petpetid/delete/HeaderParameters.java @@ -679,6 +688,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/BooleanEnumVa src/main/java/org/openapijsonschematools/client/schemas/validation/BooleanSchemaValidator.java src/main/java/org/openapijsonschematools/client/schemas/validation/BooleanValueMethod.java src/main/java/org/openapijsonschematools/client/schemas/validation/CustomIsoparser.java +src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleEnumValidator.java src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleValueMethod.java src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java @@ -731,6 +741,13 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java +src/main/java/org/openapijsonschematools/client/servers/Server0.java +src/main/java/org/openapijsonschematools/client/servers/Server1.java +src/main/java/org/openapijsonschematools/client/servers/Server2.java +src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java +src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java +src/main/java/org/openapijsonschematools/client/servers/server0/Variables.java +src/main/java/org/openapijsonschematools/client/servers/server1/Variables.java src/test/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlagsTest.java src/test/java/org/openapijsonschematools/client/schemas/AnyTypeSchemaTest.java src/test/java/org/openapijsonschematools/client/schemas/ArrayTypeSchemaTest.java diff --git a/samples/client/petstore/java/README.md b/samples/client/petstore/java/README.md index 4b89c2bf5b5..02018cd771c 100644 --- a/samples/client/petstore/java/README.md +++ b/samples/client/petstore/java/README.md @@ -148,6 +148,13 @@ org.openapijsonschematools.client.components.schemas to validate input payloads output classes. Json schemas allow multiple types for one schema, so a schema's validate method can have allowed input and output types. +## Servers +| server_index | Class | Description | +| ------------ | ----- | ----------- | +| 0 | [Server0](docs/servers/Server0.md) | petstore server | +| 1 | [Server1](docs/servers/Server1.md) | The local server | +| 2 | [Server2](docs/servers/Server2.md) | staging server with no variables | + ## Component Schemas | Class | Description | diff --git a/samples/client/petstore/java/docs/components/schemas/Animal.md b/samples/client/petstore/java/docs/components/schemas/Animal.md index 1d0d49522bd..f4354735af8 100644 --- a/samples/client/petstore/java/docs/components/schemas/Animal.md +++ b/samples/client/petstore/java/docs/components/schemas/Animal.md @@ -150,6 +150,7 @@ String validatedPayload = Animal.Color.validate( | Modifier and Type | Field and Description | | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| +| @Nullable Object | defaultValue = "red" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/components/schemas/Bar.md b/samples/client/petstore/java/docs/components/schemas/Bar.md index 318fc2ea22d..df3d7160e38 100644 --- a/samples/client/petstore/java/docs/components/schemas/Bar.md +++ b/samples/client/petstore/java/docs/components/schemas/Bar.md @@ -42,6 +42,7 @@ String validatedPayload = Bar.Bar1.validate( | Modifier and Type | Field and Description | | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| +| @Nullable Object | defaultValue = "bar" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/components/schemas/Category.md b/samples/client/petstore/java/docs/components/schemas/Category.md index c56f4fbd277..6b7f5eeda85 100644 --- a/samples/client/petstore/java/docs/components/schemas/Category.md +++ b/samples/client/petstore/java/docs/components/schemas/Category.md @@ -153,6 +153,7 @@ String validatedPayload = Category.Name.validate( | Modifier and Type | Field and Description | | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| +| @Nullable Object | defaultValue = "default-name" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/components/schemas/DateTimeTest.md b/samples/client/petstore/java/docs/components/schemas/DateTimeTest.md index 43d8e7a45be..e86d147d13d 100644 --- a/samples/client/petstore/java/docs/components/schemas/DateTimeTest.md +++ b/samples/client/petstore/java/docs/components/schemas/DateTimeTest.md @@ -43,6 +43,7 @@ String validatedPayload = DateTimeTest.DateTimeTest1.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | String | type = "date-time"; | +| @Nullable Object | defaultValue = "2010-01-01T10:10:10.000111+01:00" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/components/schemas/EnumClass.md b/samples/client/petstore/java/docs/components/schemas/EnumClass.md index edd7cdc84a7..ace63932f8b 100644 --- a/samples/client/petstore/java/docs/components/schemas/EnumClass.md +++ b/samples/client/petstore/java/docs/components/schemas/EnumClass.md @@ -45,6 +45,7 @@ String validatedPayload = EnumClass.EnumClass1.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    "_abc",
    "-efg",
    "(xyz)",
    "COUNT_1M",
    "COUNT_50M"
)
| +| @Nullable Object | defaultValue = "-efg" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/components/schemas/IntegerEnumWithDefaultValue.md b/samples/client/petstore/java/docs/components/schemas/IntegerEnumWithDefaultValue.md index 11cbef31f6b..1f95caefd6e 100644 --- a/samples/client/petstore/java/docs/components/schemas/IntegerEnumWithDefaultValue.md +++ b/samples/client/petstore/java/docs/components/schemas/IntegerEnumWithDefaultValue.md @@ -48,6 +48,7 @@ long validatedPayload = IntegerEnumWithDefaultValue.IntegerEnumWithDefaultValue1 | ----------------- | ---------------------- | | Set> | type = Set.of(
    Integer.class,
    Long.class,
    Float.class,
    Double.class
)
| | Set | enumValues = SetMaker.makeSet(
    0,
    1,
    2
)
| +| @Nullable Object | defaultValue = 0 | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/components/schemas/StringEnumWithDefaultValue.md b/samples/client/petstore/java/docs/components/schemas/StringEnumWithDefaultValue.md index 9c7fd6192e9..1d6940f8f46 100644 --- a/samples/client/petstore/java/docs/components/schemas/StringEnumWithDefaultValue.md +++ b/samples/client/petstore/java/docs/components/schemas/StringEnumWithDefaultValue.md @@ -45,6 +45,7 @@ String validatedPayload = StringEnumWithDefaultValue.StringEnumWithDefaultValue1 | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    "placed",
    "approved",
    "delivered"
)
| +| @Nullable Object | defaultValue = "placed" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter0/Schema0.md b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter0/Schema0.md index b62bd5b4069..d924de19ccf 100644 --- a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter0/Schema0.md +++ b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter0/Schema0.md @@ -122,6 +122,7 @@ String validatedPayload = Schema0.Items0.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    ">",
    "$"
)
| +| @Nullable Object | defaultValue = "$" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter1/Schema1.md b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter1/Schema1.md index 920b9f75cd5..45888b83913 100644 --- a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter1/Schema1.md +++ b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter1/Schema1.md @@ -44,6 +44,7 @@ String validatedPayload = Schema1.Schema11.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    "_abc",
    "-efg",
    "(xyz)"
)
| +| @Nullable Object | defaultValue = "-efg" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter2/Schema2.md b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter2/Schema2.md index 83d658a5de2..4ec10cc9e8c 100644 --- a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter2/Schema2.md +++ b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter2/Schema2.md @@ -122,6 +122,7 @@ String validatedPayload = Schema2.Items2.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    ">",
    "$"
)
| +| @Nullable Object | defaultValue = "$" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter3/Schema3.md b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter3/Schema3.md index 5f874610ed6..95b009cce50 100644 --- a/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter3/Schema3.md +++ b/samples/client/petstore/java/docs/paths/fake/get/parameters/parameter3/Schema3.md @@ -44,6 +44,7 @@ String validatedPayload = Schema3.Schema31.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    "_abc",
    "-efg",
    "(xyz)"
)
| +| @Nullable Object | defaultValue = "-efg" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.md b/samples/client/petstore/java/docs/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.md index d93c80134bd..cf72b7962d0 100644 --- a/samples/client/petstore/java/docs/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.md +++ b/samples/client/petstore/java/docs/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.md @@ -149,6 +149,7 @@ String validatedPayload = Schema.EnumFormString.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    "_abc",
    "-efg",
    "(xyz)"
)
| +| @Nullable Object | defaultValue = "-efg" | ### Method Summary | Modifier and Type | Method and Description | @@ -278,6 +279,7 @@ String validatedPayload = Schema.Items.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    ">",
    "$"
)
| +| @Nullable Object | defaultValue = "$" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.md b/samples/client/petstore/java/docs/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.md index 27a0ee9332b..4cef3ad5a0f 100644 --- a/samples/client/petstore/java/docs/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.md +++ b/samples/client/petstore/java/docs/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.md @@ -561,6 +561,7 @@ String validatedPayload = Schema.DateTime.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | String | type = "date-time"; | +| @Nullable Object | defaultValue = "2010-02-01T10:20:10.111110+01:00" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/paths/petfindbystatus/get/parameters/parameter0/Schema0.md b/samples/client/petstore/java/docs/paths/petfindbystatus/get/parameters/parameter0/Schema0.md index 1f36595a735..d745a984fbd 100644 --- a/samples/client/petstore/java/docs/paths/petfindbystatus/get/parameters/parameter0/Schema0.md +++ b/samples/client/petstore/java/docs/paths/petfindbystatus/get/parameters/parameter0/Schema0.md @@ -122,6 +122,7 @@ String validatedPayload = Schema0.Items0.validate( | ----------------- | ---------------------- | | Set> | type = Set.of(
    String.class
)
| | Set | enumValues = SetMaker.makeSet(
    "available",
    "pending",
    "sold"
)
| +| @Nullable Object | defaultValue = "available" | ### Method Summary | Modifier and Type | Method and Description | diff --git a/samples/client/petstore/java/docs/servers/Server0.md b/samples/client/petstore/java/docs/servers/Server0.md new file mode 100644 index 00000000000..28e2d947040 --- /dev/null +++ b/samples/client/petstore/java/docs/servers/Server0.md @@ -0,0 +1,295 @@ +org.openapijsonschematools.client.servers.Server0 +# Server Server0 +public class Server0 + +A class that stores a server url + +## Description +petstore server + +### Constructor Summary +| Constructor and Description | +| --------------------------- | +| Server0()
Creates a server using default values for variables | +| Server0([Variables.VariablesMap](#variablesmap) variables)
Creates a server using input values for variables | + +### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | --------------------- | +| String | url = "http://{server}.swagger.io:{port}/v2" | +| [Variables.VariablesMap](#variablesmap) | variables | + +## Variables +public class Variables + +A class that contains necessary nested +- schema classes (which validate payloads), extends JsonSchema +- classes to store validated map payloads, extends FrozenMap +- classes to build inputs for map payloads +- enum classes + +### Nested Class Summary +| Modifier and Type | Class and Description | +| ----------------- | ---------------------- | +| static class | [Variables.Variables1](#variables1)
schema class | +| static class | [Variables.VariablesMapBuilder](#variablesmapbuilder)
builder for Map payloads | +| static class | [Variables.VariablesMap](#variablesmap)
output class for Map payloads | +| static class | [Variables.Port](#port)
schema class | +| enum | [Variables.StringPortEnums](#stringportenums)
String enum | +| static class | [Variables.Server](#server)
schema class | +| enum | [Variables.StringServerEnums](#stringserverenums)
String enum | +| static class | [Variables.AdditionalProperties](#additionalproperties)
schema class | + +### Variables1 +public static class Variables1
+extends JsonSchema + +A schema class that validates payloads + +#### Code Sample +``` +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; + +import java.util.Arrays; +import java.util.List; +import java.util.AbstractMap; + +static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); + +// Map validation +Variables.VariablesMap validatedPayload = + Variables.Variables1.validate( + new Variables.VariablesMapBuilder() + .port("80") + + .server("petstore") + + .build(), + configuration +); +``` + +#### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | ---------------------- | +| Set> | type = Set.of(Map.class) | +| Map> | properties = Map.ofEntries(
    new PropertyEntry("server", [Server.class](#server))),
    new PropertyEntry("port", [Port.class](#port)))
)
| +| Set | required = Set.of(
    "port",
    "server"
)
| +| Class | additionalProperties = [AdditionalProperties.class](#additionalproperties) | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| [VariablesMap](#variablesmap) | validate([Map<?, ?>](#variablesmapbuilder) arg, SchemaConfiguration configuration) | +| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) | +### VariablesMap00Builder +public class VariablesMap00Builder
+builder for `Map` + +A class that builds the Map input type + +#### Constructor Summary +| Constructor and Description | +| --------------------------- | +| VariablesMap00Builder(Map instance)
Creates a builder that contains the passed instance | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| Map | build()
Returns map input that should be used with Schema.validate | + +### VariablesMap01Builder +public class VariablesMap01Builder
+builder for `Map` + +A class that builds the Map input type + +#### Constructor Summary +| Constructor and Description | +| --------------------------- | +| VariablesMap01Builder(Map instance)
Creates a builder that contains the passed instance | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| [VariablesMap00Builder](#variablesmap00builder) | server(String value) | +| [VariablesMap00Builder](#variablesmap00builder) | server([StringServerEnums](#stringserverenums) value) | + +### VariablesMap10Builder +public class VariablesMap10Builder
+builder for `Map` + +A class that builds the Map input type + +#### Constructor Summary +| Constructor and Description | +| --------------------------- | +| VariablesMap10Builder(Map instance)
Creates a builder that contains the passed instance | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| [VariablesMap00Builder](#variablesmap00builder) | port(String value) | +| [VariablesMap00Builder](#variablesmap00builder) | port([StringPortEnums](#stringportenums) value) | + +### VariablesMapBuilder +public class VariablesMapBuilder
+builder for `Map` + +A class that builds the Map input type + +#### Constructor Summary +| Constructor and Description | +| --------------------------- | +| VariablesMapBuilder()
Creates a builder that contains an empty map | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| [VariablesMap01Builder](#variablesmap01builder) | port(String value) | +| [VariablesMap01Builder](#variablesmap01builder) | port([StringPortEnums](#stringportenums) value) | +| [VariablesMap10Builder](#variablesmap10builder) | server(String value) | +| [VariablesMap10Builder](#variablesmap10builder) | server([StringServerEnums](#stringserverenums) value) | + +### VariablesMap +public static class VariablesMap
+extends FrozenMap + +A class to store validated Map payloads + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| static [VariablesMap](#variablesmap) | of([Map](#variablesmapbuilder) arg, SchemaConfiguration configuration) | +| String | port()
must be one of ["80", "8080"] if omitted the server will use the default value of 80 | +| String | server()
must be one of ["petstore", "qa-petstore", "dev-petstore"] if omitted the server will use the default value of petstore | + +### Port +public static class Port
+extends JsonSchema + +A schema class that validates payloads + +### Description +the port + +#### Code Sample +``` +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; + +import java.util.Arrays; +import java.util.List; +import java.util.AbstractMap; + +static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); + +// String validation +String validatedPayload = Variables.Port.validate( + "80", + configuration +); +``` + +#### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | ---------------------- | +| Set> | type = Set.of(
    String.class
)
| +| Set | enumValues = SetMaker.makeSet(
    "80",
    "8080"
)
| +| @Nullable Object | defaultValue = "80" | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| String | validate(String arg, SchemaConfiguration configuration) | +| String | validate([StringPortEnums](#stringportenums) arg, SchemaConfiguration configuration) | +| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) | +### StringPortEnums +public enum StringPortEnums
+extends `Enum` + +A class that stores String enum values + +#### Enum Constant Summary +| Enum Constant | Description | +| ------------- | ----------- | +| POSITIVE_80 | value = "80" | +| POSITIVE_8080 | value = "8080" | + +### Server +public static class Server
+extends JsonSchema + +A schema class that validates payloads + +### Description +server host prefix + +#### Code Sample +``` +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; + +import java.util.Arrays; +import java.util.List; +import java.util.AbstractMap; + +static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); + +// String validation +String validatedPayload = Variables.Server.validate( + "petstore", + configuration +); +``` + +#### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | ---------------------- | +| Set> | type = Set.of(
    String.class
)
| +| Set | enumValues = SetMaker.makeSet(
    "petstore",
    "qa-petstore",
    "dev-petstore"
)
| +| @Nullable Object | defaultValue = "petstore" | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| String | validate(String arg, SchemaConfiguration configuration) | +| String | validate([StringServerEnums](#stringserverenums) arg, SchemaConfiguration configuration) | +| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) | +### StringServerEnums +public enum StringServerEnums
+extends `Enum` + +A class that stores String enum values + +#### Enum Constant Summary +| Enum Constant | Description | +| ------------- | ----------- | +| PETSTORE | value = "petstore" | +| QA_HYPHEN_MINUS_PETSTORE | value = "qa-petstore" | +| DEV_HYPHEN_MINUS_PETSTORE | value = "dev-petstore" | + +### AdditionalProperties +public static class AdditionalProperties
+extends NotAnyTypeJsonSchema + +A schema class that validates payloads + +| Methods Inherited from class org.openapijsonschematools.client.schemas.NotAnyTypeJsonSchema | +| ------------------------------------------------------------------ | +| validate | + +[[Back to top]](#top) [[Back to Servers]](../../README.md#Servers) [[Back to README]](../../README.md) diff --git a/samples/client/petstore/java/docs/servers/Server1.md b/samples/client/petstore/java/docs/servers/Server1.md new file mode 100644 index 00000000000..e53f1c5a7d1 --- /dev/null +++ b/samples/client/petstore/java/docs/servers/Server1.md @@ -0,0 +1,194 @@ +org.openapijsonschematools.client.servers.Server1 +# Server Server1 +public class Server1 + +A class that stores a server url + +## Description +The local server + +### Constructor Summary +| Constructor and Description | +| --------------------------- | +| Server1()
Creates a server using default values for variables | +| Server1([Variables.VariablesMap](#variablesmap) variables)
Creates a server using input values for variables | + +### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | --------------------- | +| String | url = "https://localhost:8080/{version}" | +| [Variables.VariablesMap](#variablesmap) | variables | + +## Variables +public class Variables + +A class that contains necessary nested +- schema classes (which validate payloads), extends JsonSchema +- classes to store validated map payloads, extends FrozenMap +- classes to build inputs for map payloads +- enum classes + +### Nested Class Summary +| Modifier and Type | Class and Description | +| ----------------- | ---------------------- | +| static class | [Variables.Variables1](#variables1)
schema class | +| static class | [Variables.VariablesMapBuilder](#variablesmapbuilder)
builder for Map payloads | +| static class | [Variables.VariablesMap](#variablesmap)
output class for Map payloads | +| static class | [Variables.Version](#version)
schema class | +| enum | [Variables.StringVersionEnums](#stringversionenums)
String enum | +| static class | [Variables.AdditionalProperties](#additionalproperties)
schema class | + +### Variables1 +public static class Variables1
+extends JsonSchema + +A schema class that validates payloads + +#### Code Sample +``` +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; + +import java.util.Arrays; +import java.util.List; +import java.util.AbstractMap; + +static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); + +// Map validation +Variables.VariablesMap validatedPayload = + Variables.Variables1.validate( + new Variables.VariablesMapBuilder() + .version("v1") + + .build(), + configuration +); +``` + +#### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | ---------------------- | +| Set> | type = Set.of(Map.class) | +| Map> | properties = Map.ofEntries(
    new PropertyEntry("version", [Version.class](#version)))
)
| +| Set | required = Set.of(
    "version"
)
| +| Class | additionalProperties = [AdditionalProperties.class](#additionalproperties) | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| [VariablesMap](#variablesmap) | validate([Map<?, ?>](#variablesmapbuilder) arg, SchemaConfiguration configuration) | +| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) | +### VariablesMap0Builder +public class VariablesMap0Builder
+builder for `Map` + +A class that builds the Map input type + +#### Constructor Summary +| Constructor and Description | +| --------------------------- | +| VariablesMap0Builder(Map instance)
Creates a builder that contains the passed instance | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| Map | build()
Returns map input that should be used with Schema.validate | + +### VariablesMapBuilder +public class VariablesMapBuilder
+builder for `Map` + +A class that builds the Map input type + +#### Constructor Summary +| Constructor and Description | +| --------------------------- | +| VariablesMapBuilder()
Creates a builder that contains an empty map | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| [VariablesMap0Builder](#variablesmap0builder) | version(String value) | +| [VariablesMap0Builder](#variablesmap0builder) | version([StringVersionEnums](#stringversionenums) value) | + +### VariablesMap +public static class VariablesMap
+extends FrozenMap + +A class to store validated Map payloads + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| static [VariablesMap](#variablesmap) | of([Map](#variablesmapbuilder) arg, SchemaConfiguration configuration) | +| String | version()
must be one of ["v1", "v2"] if omitted the server will use the default value of v2 | + +### Version +public static class Version
+extends JsonSchema + +A schema class that validates payloads + +#### Code Sample +``` +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; + +import java.util.Arrays; +import java.util.List; +import java.util.AbstractMap; + +static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); + +// String validation +String validatedPayload = Variables.Version.validate( + "v1", + configuration +); +``` + +#### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | ---------------------- | +| Set> | type = Set.of(
    String.class
)
| +| Set | enumValues = SetMaker.makeSet(
    "v1",
    "v2"
)
| +| @Nullable Object | defaultValue = "v2" | + +#### Method Summary +| Modifier and Type | Method and Description | +| ----------------- | ---------------------- | +| String | validate(String arg, SchemaConfiguration configuration) | +| String | validate([StringVersionEnums](#stringversionenums) arg, SchemaConfiguration configuration) | +| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) | +### StringVersionEnums +public enum StringVersionEnums
+extends `Enum` + +A class that stores String enum values + +#### Enum Constant Summary +| Enum Constant | Description | +| ------------- | ----------- | +| V1 | value = "v1" | +| V2 | value = "v2" | + +### AdditionalProperties +public static class AdditionalProperties
+extends NotAnyTypeJsonSchema + +A schema class that validates payloads + +| Methods Inherited from class org.openapijsonschematools.client.schemas.NotAnyTypeJsonSchema | +| ------------------------------------------------------------------ | +| validate | + +[[Back to top]](#top) [[Back to Servers]](../../README.md#Servers) [[Back to README]](../../README.md) diff --git a/samples/client/petstore/java/docs/servers/Server2.md b/samples/client/petstore/java/docs/servers/Server2.md new file mode 100644 index 00000000000..c45cd5211e5 --- /dev/null +++ b/samples/client/petstore/java/docs/servers/Server2.md @@ -0,0 +1,20 @@ +org.openapijsonschematools.client.servers.Server2 +# Server Server2 +public class Server2 + +A class that stores a server url + +## Description +staging server with no variables + +### Constructor Summary +| Constructor and Description | +| --------------------------- | +| Server2()
Creates a server | + +### Field Summary +| Modifier and Type | Field and Description | +| ----------------- | --------------------- | +| String | url = "https://localhost:8080" | + +[[Back to top]](#top) [[Back to Servers]](../../README.md#Servers) [[Back to README]](../../README.md) diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Animal.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Animal.java index 467862fb74a..edd82147a44 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Animal.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Animal.java @@ -17,6 +17,7 @@ import org.openapijsonschematools.client.schemas.BaseBuilder; import org.openapijsonschematools.client.schemas.StringJsonSchema; import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; @@ -41,7 +42,7 @@ public static ClassName getInstance() { } - public static class Color extends JsonSchema implements StringSchemaValidator { + public static class Color extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { private static @Nullable Color instance = null; protected Color() { @@ -49,6 +50,7 @@ protected Color() { .type(Set.of( String.class )) + .defaultValue("red") ); } @@ -84,6 +86,12 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class AnimalMap extends FrozenMap<@Nullable Object> { diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Bar.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Bar.java index 4550db2e0c9..996fe61f761 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Bar.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Bar.java @@ -9,6 +9,7 @@ import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; @@ -19,7 +20,7 @@ public class Bar { // nest classes so all schemas and input/output classes can be public - public static class Bar1 extends JsonSchema implements StringSchemaValidator { + public static class Bar1 extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator @@ -33,6 +34,7 @@ protected Bar1() { .type(Set.of( String.class )) + .defaultValue("bar") ); } @@ -68,5 +70,11 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Category.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Category.java index f70ac6a9cc4..3f441618736 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Category.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/Category.java @@ -17,6 +17,7 @@ import org.openapijsonschematools.client.schemas.BaseBuilder; import org.openapijsonschematools.client.schemas.Int64JsonSchema; import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; @@ -41,7 +42,7 @@ public static Id getInstance() { } - public static class Name extends JsonSchema implements StringSchemaValidator { + public static class Name extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { private static @Nullable Name instance = null; protected Name() { @@ -49,6 +50,7 @@ protected Name() { .type(Set.of( String.class )) + .defaultValue("default-name") ); } @@ -84,6 +86,12 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class CategoryMap extends FrozenMap<@Nullable Object> { diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeTest.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeTest.java index 1c3290701cd..d9dbe964144 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeTest.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeTest.java @@ -10,6 +10,7 @@ import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; @@ -20,7 +21,7 @@ public class DateTimeTest { // nest classes so all schemas and input/output classes can be public - public static class DateTimeTest1 extends JsonSchema implements StringSchemaValidator { + public static class DateTimeTest1 extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator @@ -35,6 +36,7 @@ protected DateTimeTest1() { String.class )) .format("date-time") + .defaultValue("2010-01-01T10:10:10.000111+01:00") ); } @@ -70,5 +72,11 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumClass.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumClass.java index 2e6e49858d7..ad7d1a057f0 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumClass.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumClass.java @@ -10,6 +10,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; @@ -38,7 +39,7 @@ public String value() { } - public static class EnumClass1 extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class EnumClass1 extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator @@ -59,6 +60,7 @@ protected EnumClass1() { "COUNT_1M", "COUNT_50M" )) + .defaultValue("-efg") ); } @@ -99,5 +101,11 @@ public String validate(StringEnumClassEnums arg,SchemaConfiguration configuratio } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/IntegerEnumWithDefaultValue.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/IntegerEnumWithDefaultValue.java index 2e28c23c230..c0ed05992f9 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/IntegerEnumWithDefaultValue.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/IntegerEnumWithDefaultValue.java @@ -11,6 +11,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.DoubleEnumValidator; import org.openapijsonschematools.client.schemas.validation.DoubleValueMethod; import org.openapijsonschematools.client.schemas.validation.FloatEnumValidator; @@ -107,6 +108,7 @@ protected IntegerEnumWithDefaultValue1() { new BigDecimal("1"), new BigDecimal("2") )) + .defaultValue(0) ); } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/StringEnumWithDefaultValue.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/StringEnumWithDefaultValue.java index 2cea4790f5f..b0779ce18b7 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/StringEnumWithDefaultValue.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/components/schemas/StringEnumWithDefaultValue.java @@ -10,6 +10,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; @@ -36,7 +37,7 @@ public String value() { } - public static class StringEnumWithDefaultValue1 extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class StringEnumWithDefaultValue1 extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator @@ -55,6 +56,7 @@ protected StringEnumWithDefaultValue1() { "approved", "delivered" )) + .defaultValue("placed") ); } @@ -95,5 +97,11 @@ public String validate(StringStringEnumWithDefaultValueEnums arg,SchemaConfigura } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter0/Schema0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter0/Schema0.java index d05ecf7a39e..831d3d79208 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter0/Schema0.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter0/Schema0.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenList; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; @@ -39,7 +40,7 @@ public String value() { } - public static class Items0 extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class Items0 extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { private static @Nullable Items0 instance = null; protected Items0() { @@ -51,6 +52,7 @@ protected Items0() { ">", "$" )) + .defaultValue("$") ); } @@ -91,6 +93,12 @@ public String validate(StringItemsEnums0 arg,SchemaConfiguration configuration) } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class SchemaList0 extends FrozenList { diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter1/Schema1.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter1/Schema1.java index bc31f392a1c..14ce91ea73d 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter1/Schema1.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter1/Schema1.java @@ -10,6 +10,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; @@ -36,7 +37,7 @@ public String value() { } - public static class Schema11 extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class Schema11 extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { private static @Nullable Schema11 instance = null; protected Schema11() { @@ -49,6 +50,7 @@ protected Schema11() { "-efg", "(xyz)" )) + .defaultValue("-efg") ); } @@ -89,5 +91,11 @@ public String validate(StringSchemaEnums1 arg,SchemaConfiguration configuration) } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter2/Schema2.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter2/Schema2.java index 9d216b5f321..1d98fd7c0b0 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter2/Schema2.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter2/Schema2.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenList; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; @@ -39,7 +40,7 @@ public String value() { } - public static class Items2 extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class Items2 extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { private static @Nullable Items2 instance = null; protected Items2() { @@ -51,6 +52,7 @@ protected Items2() { ">", "$" )) + .defaultValue("$") ); } @@ -91,6 +93,12 @@ public String validate(StringItemsEnums2 arg,SchemaConfiguration configuration) } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class SchemaList2 extends FrozenList { diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter3/Schema3.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter3/Schema3.java index 5a162b2258a..5113bcd7b70 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter3/Schema3.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/parameters/parameter3/Schema3.java @@ -10,6 +10,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; @@ -36,7 +37,7 @@ public String value() { } - public static class Schema31 extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class Schema31 extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { private static @Nullable Schema31 instance = null; protected Schema31() { @@ -49,6 +50,7 @@ protected Schema31() { "-efg", "(xyz)" )) + .defaultValue("-efg") ); } @@ -89,5 +91,11 @@ public String validate(StringSchemaEnums3 arg,SchemaConfiguration configuration) } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.java index 92761db9724..b4f7d446209 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/applicationxwwwformurlencoded/Schema.java @@ -17,6 +17,7 @@ import org.openapijsonschematools.client.schemas.BaseBuilder; import org.openapijsonschematools.client.schemas.SetMaker; import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenList; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; @@ -47,7 +48,7 @@ public String value() { } - public static class Items extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class Items extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { private static @Nullable Items instance = null; protected Items() { @@ -59,6 +60,7 @@ protected Items() { ">", "$" )) + .defaultValue("$") ); } @@ -99,6 +101,12 @@ public String validate(StringItemsEnums arg,SchemaConfiguration configuration) t } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class EnumFormStringArrayList extends FrozenList { @@ -218,7 +226,7 @@ public String value() { } - public static class EnumFormString extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class EnumFormString extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { private static @Nullable EnumFormString instance = null; protected EnumFormString() { @@ -231,6 +239,7 @@ protected EnumFormString() { "-efg", "(xyz)" )) + .defaultValue("-efg") ); } @@ -271,6 +280,12 @@ public String validate(StringEnumFormStringEnums arg,SchemaConfiguration configu } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class SchemaMap extends FrozenMap<@Nullable Object> { diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.java index b83d8d1e8b5..f002482d4e0 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.java @@ -21,6 +21,7 @@ import org.openapijsonschematools.client.schemas.Int64JsonSchema; import org.openapijsonschematools.client.schemas.StringJsonSchema; import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; @@ -475,7 +476,7 @@ public static Date getInstance() { } - public static class DateTime extends JsonSchema implements StringSchemaValidator { + public static class DateTime extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { private static @Nullable DateTime instance = null; protected DateTime() { @@ -484,6 +485,7 @@ protected DateTime() { String.class )) .format("date-time") + .defaultValue("2010-02-01T10:20:10.111110+01:00") ); } @@ -519,6 +521,12 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class Password extends JsonSchema implements StringSchemaValidator { diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java new file mode 100644 index 00000000000..515598c4bc9 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java @@ -0,0 +1,9 @@ +package org.openapijsonschematools.client.paths.foo.get.servers; + +import org.openapijsonschematools.client.servers.ServerWithoutVariables; + +class FooGetServer0 extends ServerWithoutVariables { + public FooGetServer0() { + super("https://path-server-test.petstore.local/v2"); + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java new file mode 100644 index 00000000000..3f43ac01d6e --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java @@ -0,0 +1,27 @@ +package org.openapijsonschematools.client.paths.foo.get.servers; + +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.servers.ServerWithVariables; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.paths.foo.get.servers.server1.Variables; + +import java.util.AbstractMap; + +class FooGetServer1 extends ServerWithVariables { + + public FooGetServer1() { + super( + "https://petstore.swagger.io/{version}", + Variables.Variables1.getInstance().validate( + MapUtils.makeMap( + new AbstractMap.SimpleEntry<>("version", Variables.Version.getInstance().defaultValue()) + ), + new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()) + ) + ); + } + public FooGetServer1(Variables.VariablesMap variables) { + super("https://petstore.swagger.io/{version}", variables); + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/server1/Variables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/server1/Variables.java new file mode 100644 index 00000000000..e40f854f0f4 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/server1/Variables.java @@ -0,0 +1,263 @@ +package org.openapijsonschematools.client.paths.foo.get.servers.server1; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.NotAnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.StringEnumValidator; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.StringValueMethod; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Variables { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalProperties extends NotAnyTypeJsonSchema { + // NotAnyTypeSchema + private static @Nullable AdditionalProperties instance = null; + public static AdditionalProperties getInstance() { + if (instance == null) { + instance = new AdditionalProperties(); + } + return instance; + } + } + + public enum StringVersionEnums implements StringValueMethod { + V1("v1"), + V2("v2"); + private final String value; + + StringVersionEnums(String value) { + this.value = value; + } + public String value() { + return this.value; + } + } + + + public static class Version extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { + private static @Nullable Version instance = null; + + protected Version() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .enumValues(SetMaker.makeSet( + "v1", + "v2" + )) + .defaultValue("v1") + ); + } + + public static Version getInstance() { + if (instance == null) { + instance = new Version(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public String validate(StringVersionEnums arg,SchemaConfiguration configuration) throws ValidationException { + return validate(arg.value(), configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + + public static class VariablesMap extends FrozenMap { + protected VariablesMap(FrozenMap m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "version" + ); + public static final Set optionalKeys = Set.of(); + public static VariablesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Variables1.getInstance().validate(arg, configuration); + } + + public String version() { + return getOrThrow("version"); + } + } + + public interface SetterForVersion { + Map getInstance(); + T getBuilderAfterVersion(Map instance); + + default T version(String value) { + var instance = getInstance(); + instance.put("version", value); + return getBuilderAfterVersion(instance); + } + + default T version(StringVersionEnums value) { + var instance = getInstance(); + instance.put("version", value.value()); + return getBuilderAfterVersion(instance); + } + } + + public static class VariablesMap0Builder implements BaseBuilder { + private final Map instance; + private static final Set knownKeys = Set.of( + "version" + ); + public Set getKnownKeys() { + return knownKeys; + } + public VariablesMap0Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + } + + public static class VariablesMapBuilder implements SetterForVersion { + private final Map instance; + public VariablesMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public VariablesMap0Builder getBuilderAfterVersion(Map instance) { + return new VariablesMap0Builder(instance); + } + } + + + public static class Variables1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Variables1 instance = null; + + protected Variables1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("version", Version.class) + )) + .required(Set.of( + "version" + )) + .additionalProperties(AdditionalProperties.class) + ); + } + + public static Variables1 getInstance() { + if (instance == null) { + instance = new Variables1(); + } + return instance; + } + + public VariablesMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof String)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (String) propertyInstance); + } + FrozenMap castProperties = new FrozenMap<>(properties); + return new VariablesMap(castProperties); + } + + public VariablesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/parameters/parameter0/Schema0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/parameters/parameter0/Schema0.java index 101d5425fb4..3c53311c7b1 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/parameters/parameter0/Schema0.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/parameters/parameter0/Schema0.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; import org.openapijsonschematools.client.schemas.validation.FrozenList; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; @@ -40,7 +41,7 @@ public String value() { } - public static class Items0 extends JsonSchema implements StringSchemaValidator, StringEnumValidator { + public static class Items0 extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { private static @Nullable Items0 instance = null; protected Items0() { @@ -53,6 +54,7 @@ protected Items0() { "pending", "sold" )) + .defaultValue("available") ); } @@ -93,6 +95,12 @@ public String validate(StringItemsEnums0 arg,SchemaConfiguration configuration) } throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } } public static class SchemaList0 extends FrozenList { diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java new file mode 100644 index 00000000000..52486b0960f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java @@ -0,0 +1,9 @@ +package org.openapijsonschematools.client.paths.petfindbystatus.servers; + +import org.openapijsonschematools.client.servers.ServerWithoutVariables; + +class PetfindbystatusServer0 extends ServerWithoutVariables { + public PetfindbystatusServer0() { + super("https://path-server-test.petstore.local/v2"); + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java new file mode 100644 index 00000000000..01449e311af --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java @@ -0,0 +1,27 @@ +package org.openapijsonschematools.client.paths.petfindbystatus.servers; + +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.servers.ServerWithVariables; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.paths.petfindbystatus.servers.server1.Variables; + +import java.util.AbstractMap; + +class PetfindbystatusServer1 extends ServerWithVariables { + + public PetfindbystatusServer1() { + super( + "https://petstore.swagger.io/{version}", + Variables.Variables1.getInstance().validate( + MapUtils.makeMap( + new AbstractMap.SimpleEntry<>("version", Variables.Version.getInstance().defaultValue()) + ), + new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()) + ) + ); + } + public PetfindbystatusServer1(Variables.VariablesMap variables) { + super("https://petstore.swagger.io/{version}", variables); + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/server1/Variables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/server1/Variables.java new file mode 100644 index 00000000000..3b7616aaf5f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/server1/Variables.java @@ -0,0 +1,263 @@ +package org.openapijsonschematools.client.paths.petfindbystatus.servers.server1; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.NotAnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.StringEnumValidator; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.StringValueMethod; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Variables { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalProperties extends NotAnyTypeJsonSchema { + // NotAnyTypeSchema + private static @Nullable AdditionalProperties instance = null; + public static AdditionalProperties getInstance() { + if (instance == null) { + instance = new AdditionalProperties(); + } + return instance; + } + } + + public enum StringVersionEnums implements StringValueMethod { + V1("v1"), + V2("v2"); + private final String value; + + StringVersionEnums(String value) { + this.value = value; + } + public String value() { + return this.value; + } + } + + + public static class Version extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { + private static @Nullable Version instance = null; + + protected Version() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .enumValues(SetMaker.makeSet( + "v1", + "v2" + )) + .defaultValue("v1") + ); + } + + public static Version getInstance() { + if (instance == null) { + instance = new Version(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public String validate(StringVersionEnums arg,SchemaConfiguration configuration) throws ValidationException { + return validate(arg.value(), configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + + public static class VariablesMap extends FrozenMap { + protected VariablesMap(FrozenMap m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "version" + ); + public static final Set optionalKeys = Set.of(); + public static VariablesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Variables1.getInstance().validate(arg, configuration); + } + + public String version() { + return getOrThrow("version"); + } + } + + public interface SetterForVersion { + Map getInstance(); + T getBuilderAfterVersion(Map instance); + + default T version(String value) { + var instance = getInstance(); + instance.put("version", value); + return getBuilderAfterVersion(instance); + } + + default T version(StringVersionEnums value) { + var instance = getInstance(); + instance.put("version", value.value()); + return getBuilderAfterVersion(instance); + } + } + + public static class VariablesMap0Builder implements BaseBuilder { + private final Map instance; + private static final Set knownKeys = Set.of( + "version" + ); + public Set getKnownKeys() { + return knownKeys; + } + public VariablesMap0Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + } + + public static class VariablesMapBuilder implements SetterForVersion { + private final Map instance; + public VariablesMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public VariablesMap0Builder getBuilderAfterVersion(Map instance) { + return new VariablesMap0Builder(instance); + } + } + + + public static class Variables1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Variables1 instance = null; + + protected Variables1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("version", Version.class) + )) + .required(Set.of( + "version" + )) + .additionalProperties(AdditionalProperties.class) + ); + } + + public static Variables1 getInstance() { + if (instance == null) { + instance = new Variables1(); + } + return instance; + } + + public VariablesMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof String)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (String) propertyInstance); + } + FrozenMap castProperties = new FrozenMap<>(properties); + return new VariablesMap(castProperties); + } + + public VariablesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java new file mode 100644 index 00000000000..43fe8423969 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java @@ -0,0 +1,5 @@ +package org.openapijsonschematools.client.schemas.validation; + +public interface DefaultValueMethod { + T defaultValue(); +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java index 479de92c1b5..b924c070ae1 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java @@ -42,6 +42,7 @@ public abstract class JsonSchema { public final @Nullable Boolean uniqueItems; public final @Nullable Set<@Nullable Object> enumValues; public final @Nullable Pattern pattern; + public final @Nullable Object defaultValue; private final LinkedHashMap keywordToValidator; protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { @@ -214,6 +215,7 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { new PatternValidator(this.pattern) ); } + this.defaultValue = jsonSchemaInfo.defaultValue; this.keywordToValidator = keywordToValidator; } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java index 3685df5d0bb..27a18717d51 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java @@ -128,4 +128,9 @@ public JsonSchemaInfo pattern(Pattern pattern) { this.pattern = pattern; return this; } + public @Nullable Object defaultValue = null; + public JsonSchemaInfo defaultValue(@Nullable Object defaultValue) { + this.defaultValue = defaultValue; + return this; + } } \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java new file mode 100644 index 00000000000..d4d772a235f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java @@ -0,0 +1,30 @@ +package org.openapijsonschematools.client.servers; + +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.servers.server0.Variables; + +import java.util.AbstractMap; + +class Server0 extends ServerWithVariables { + /* + petstore server + */ + + public Server0() { + super( + "http://{server}.swagger.io:{port}/v2", + Variables.Variables1.getInstance().validate( + MapUtils.makeMap( + new AbstractMap.SimpleEntry<>("port", Variables.Port.getInstance().defaultValue()), + new AbstractMap.SimpleEntry<>("server", Variables.Server.getInstance().defaultValue()) + ), + new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()) + ) + ); + } + public Server0(Variables.VariablesMap variables) { + super("http://{server}.swagger.io:{port}/v2", variables); + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server1.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server1.java new file mode 100644 index 00000000000..40d76ee508d --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server1.java @@ -0,0 +1,29 @@ +package org.openapijsonschematools.client.servers; + +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.servers.server1.Variables; + +import java.util.AbstractMap; + +class Server1 extends ServerWithVariables { + /* + The local server + */ + + public Server1() { + super( + "https://localhost:8080/{version}", + Variables.Variables1.getInstance().validate( + MapUtils.makeMap( + new AbstractMap.SimpleEntry<>("version", Variables.Version.getInstance().defaultValue()) + ), + new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()) + ) + ); + } + public Server1(Variables.VariablesMap variables) { + super("https://localhost:8080/{version}", variables); + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server2.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server2.java new file mode 100644 index 00000000000..cd8cf496918 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server2.java @@ -0,0 +1,9 @@ +package org.openapijsonschematools.client.servers; + +import org.openapijsonschematools.client.servers.ServerWithoutVariables; + +class Server2 extends ServerWithoutVariables { + public Server2() { + super("https://localhost:8080"); + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java new file mode 100644 index 00000000000..acdb6d730ed --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java @@ -0,0 +1,17 @@ +package org.openapijsonschematools.client.servers; + +import java.util.Map; + +public abstract class ServerWithVariables> { + public final String url; + public final T variables; + + protected ServerWithVariables(String url, T variables) { + this.variables = variables; + for (Map.Entry entry: variables.entrySet()) { + url = url.replace("{" + entry.getKey() + "}", entry.getValue()); + } + this.url = url; + } +} + diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java new file mode 100644 index 00000000000..903ac3f4c7d --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java @@ -0,0 +1,10 @@ +package org.openapijsonschematools.client.servers; + +public abstract class ServerWithoutVariables { + public final String url; + + protected ServerWithoutVariables(String url) { + this.url = url; + } +} + diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/server0/Variables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/server0/Variables.java new file mode 100644 index 00000000000..6e474653e6f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/server0/Variables.java @@ -0,0 +1,401 @@ +package org.openapijsonschematools.client.servers.server0; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.NotAnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.StringEnumValidator; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.StringValueMethod; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Variables { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalProperties extends NotAnyTypeJsonSchema { + // NotAnyTypeSchema + private static @Nullable AdditionalProperties instance = null; + public static AdditionalProperties getInstance() { + if (instance == null) { + instance = new AdditionalProperties(); + } + return instance; + } + } + + public enum StringServerEnums implements StringValueMethod { + PETSTORE("petstore"), + QA_HYPHEN_MINUS_PETSTORE("qa-petstore"), + DEV_HYPHEN_MINUS_PETSTORE("dev-petstore"); + private final String value; + + StringServerEnums(String value) { + this.value = value; + } + public String value() { + return this.value; + } + } + + + public static class Server extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { + private static @Nullable Server instance = null; + + protected Server() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .enumValues(SetMaker.makeSet( + "petstore", + "qa-petstore", + "dev-petstore" + )) + .defaultValue("petstore") + ); + } + + public static Server getInstance() { + if (instance == null) { + instance = new Server(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public String validate(StringServerEnums arg,SchemaConfiguration configuration) throws ValidationException { + return validate(arg.value(), configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + public enum StringPortEnums implements StringValueMethod { + POSITIVE_80("80"), + POSITIVE_8080("8080"); + private final String value; + + StringPortEnums(String value) { + this.value = value; + } + public String value() { + return this.value; + } + } + + + public static class Port extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { + private static @Nullable Port instance = null; + + protected Port() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .enumValues(SetMaker.makeSet( + "80", + "8080" + )) + .defaultValue("80") + ); + } + + public static Port getInstance() { + if (instance == null) { + instance = new Port(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public String validate(StringPortEnums arg,SchemaConfiguration configuration) throws ValidationException { + return validate(arg.value(), configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + + public static class VariablesMap extends FrozenMap { + protected VariablesMap(FrozenMap m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "port", + "server" + ); + public static final Set optionalKeys = Set.of(); + public static VariablesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Variables1.getInstance().validate(arg, configuration); + } + + public String port() { + String value = get("port"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for port"); + } + return (String) value; + } + + public String server() { + String value = get("server"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for server"); + } + return (String) value; + } + } + + public interface SetterForPort { + Map getInstance(); + T getBuilderAfterPort(Map instance); + + default T port(String value) { + var instance = getInstance(); + instance.put("port", value); + return getBuilderAfterPort(instance); + } + + default T port(StringPortEnums value) { + var instance = getInstance(); + instance.put("port", value.value()); + return getBuilderAfterPort(instance); + } + } + + public interface SetterForServer { + Map getInstance(); + T getBuilderAfterServer(Map instance); + + default T server(String value) { + var instance = getInstance(); + instance.put("server", value); + return getBuilderAfterServer(instance); + } + + default T server(StringServerEnums value) { + var instance = getInstance(); + instance.put("server", value.value()); + return getBuilderAfterServer(instance); + } + } + + public static class VariablesMap00Builder implements BaseBuilder { + private final Map instance; + private static final Set knownKeys = Set.of( + "port", + "server" + ); + public Set getKnownKeys() { + return knownKeys; + } + public VariablesMap00Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + } + + public static class VariablesMap01Builder implements SetterForServer { + private final Map instance; + public VariablesMap01Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public VariablesMap00Builder getBuilderAfterServer(Map instance) { + return new VariablesMap00Builder(instance); + } + } + + public static class VariablesMap10Builder implements SetterForPort { + private final Map instance; + public VariablesMap10Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public VariablesMap00Builder getBuilderAfterPort(Map instance) { + return new VariablesMap00Builder(instance); + } + } + + public static class VariablesMapBuilder implements SetterForPort, SetterForServer { + private final Map instance; + public VariablesMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public VariablesMap01Builder getBuilderAfterPort(Map instance) { + return new VariablesMap01Builder(instance); + } + public VariablesMap10Builder getBuilderAfterServer(Map instance) { + return new VariablesMap10Builder(instance); + } + } + + + public static class Variables1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Variables1 instance = null; + + protected Variables1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("server", Server.class), + new PropertyEntry("port", Port.class) + )) + .required(Set.of( + "port", + "server" + )) + .additionalProperties(AdditionalProperties.class) + ); + } + + public static Variables1 getInstance() { + if (instance == null) { + instance = new Variables1(); + } + return instance; + } + + public VariablesMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof String)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (String) propertyInstance); + } + FrozenMap castProperties = new FrozenMap<>(properties); + return new VariablesMap(castProperties); + } + + public VariablesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/server1/Variables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/server1/Variables.java new file mode 100644 index 00000000000..40337fb960d --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/server1/Variables.java @@ -0,0 +1,263 @@ +package org.openapijsonschematools.client.servers.server1; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.NotAnyTypeJsonSchema; +import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.StringEnumValidator; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.StringValueMethod; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Variables { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalProperties extends NotAnyTypeJsonSchema { + // NotAnyTypeSchema + private static @Nullable AdditionalProperties instance = null; + public static AdditionalProperties getInstance() { + if (instance == null) { + instance = new AdditionalProperties(); + } + return instance; + } + } + + public enum StringVersionEnums implements StringValueMethod { + V1("v1"), + V2("v2"); + private final String value; + + StringVersionEnums(String value) { + this.value = value; + } + public String value() { + return this.value; + } + } + + + public static class Version extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { + private static @Nullable Version instance = null; + + protected Version() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .enumValues(SetMaker.makeSet( + "v1", + "v2" + )) + .defaultValue("v2") + ); + } + + public static Version getInstance() { + if (instance == null) { + instance = new Version(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public String validate(StringVersionEnums arg,SchemaConfiguration configuration) throws ValidationException { + return validate(arg.value(), configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + + public static class VariablesMap extends FrozenMap { + protected VariablesMap(FrozenMap m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "version" + ); + public static final Set optionalKeys = Set.of(); + public static VariablesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Variables1.getInstance().validate(arg, configuration); + } + + public String version() { + return getOrThrow("version"); + } + } + + public interface SetterForVersion { + Map getInstance(); + T getBuilderAfterVersion(Map instance); + + default T version(String value) { + var instance = getInstance(); + instance.put("version", value); + return getBuilderAfterVersion(instance); + } + + default T version(StringVersionEnums value) { + var instance = getInstance(); + instance.put("version", value.value()); + return getBuilderAfterVersion(instance); + } + } + + public static class VariablesMap0Builder implements BaseBuilder { + private final Map instance; + private static final Set knownKeys = Set.of( + "version" + ); + public Set getKnownKeys() { + return knownKeys; + } + public VariablesMap0Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + } + + public static class VariablesMapBuilder implements SetterForVersion { + private final Map instance; + public VariablesMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public VariablesMap0Builder getBuilderAfterVersion(Map instance) { + return new VariablesMap0Builder(instance); + } + } + + + public static class Variables1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Variables1 instance = null; + + protected Variables1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("version", Version.class) + )) + .required(Set.of( + "version" + )) + .additionalProperties(AdditionalProperties.class) + ); + } + + public static Variables1 getInstance() { + if (instance == null) { + instance = new Variables1(); + } + return instance; + } + + public VariablesMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof String)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (String) propertyInstance); + } + FrozenMap castProperties = new FrozenMap<>(properties); + return new VariablesMap(castProperties); + } + + public VariablesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/python/src/petstore_api/components/schema/date_time_test.py b/samples/client/petstore/python/src/petstore_api/components/schema/date_time_test.py index c9a1bbb451b..f9026fd1cf9 100644 --- a/samples/client/petstore/python/src/petstore_api/components/schema/date_time_test.py +++ b/samples/client/petstore/python/src/petstore_api/components/schema/date_time_test.py @@ -25,3 +25,4 @@ class DateTimeTest( str, }) format: str = 'date-time' + default: typing.Literal["2010-01-01T10:10:10.000111+01:00"] = "2010-01-01T10:10:10.000111+01:00" diff --git a/samples/client/petstore/python/src/petstore_api/paths/fake/post/request_body/content/application_x_www_form_urlencoded/schema.py b/samples/client/petstore/python/src/petstore_api/paths/fake/post/request_body/content/application_x_www_form_urlencoded/schema.py index 7cf4028dc0d..fba8912dd7c 100644 --- a/samples/client/petstore/python/src/petstore_api/paths/fake/post/request_body/content/application_x_www_form_urlencoded/schema.py +++ b/samples/client/petstore/python/src/petstore_api/paths/fake/post/request_body/content/application_x_www_form_urlencoded/schema.py @@ -110,6 +110,7 @@ class DateTime( str, }) format: str = 'date-time' + default: typing.Literal["2010-02-01T10:20:10.111110+01:00"] = "2010-02-01T10:20:10.111110+01:00" @dataclasses.dataclass(frozen=True) diff --git a/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java b/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java index e7682bb8d28..971618f0602 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java +++ b/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java @@ -1401,6 +1401,11 @@ private void generateServers(List files, List servers, Stri String serverJsonPath = jsonPath + "/" + i; generateXs(files, serverJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVER, CodegenConstants.SERVERS, templateData, true); + if (generator.generateSeparateServerSchemas() && server.variables != null) { + String variablesJsonPath = serverJsonPath + "/variables"; + generateSchema(files, server.variables, variablesJsonPath); + } + // doc generation if (server.rootServer) { templateData.put("headerSize", "#"); diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java index 18f277686ba..89d0b5e9ddd 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java @@ -103,6 +103,7 @@ import java.io.File; import java.math.BigDecimal; +import java.time.OffsetDateTime; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentSkipListSet; @@ -961,8 +962,8 @@ public String toSecurityRequirementObjectFilename(String basename, String jsonPa } @Override - public String getPascalCaseServer(String basename) { - return toModelName(basename, null); + public String getPascalCaseServer(String basename, String jsonPath) { + return "Server" + basename; } public String getPascalCaseParameter(String basename) { @@ -1419,6 +1420,9 @@ protected EnumValue getEnumValue(Object value, String description) { type = "boolean"; } else if (value == null) { type = "null"; + } else if (value instanceof OffsetDateTime) { + type = "string"; + usedValue = value.toString(); } return new EnumValue(usedValue, type, description); } @@ -3273,6 +3277,11 @@ public Function> getSchemasFn() { }; } + @Override + public boolean generateSeparateServerSchemas() { + return false; + } + @Override @SuppressWarnings("static-method") public HashMap fromSecurityRequirement(SecurityRequirement securityRequirement, String sourceJsonPath) { @@ -3804,8 +3813,14 @@ private void updatePathsFilepath(String[] pathPieces) { xParameters.add("HeaderParameters"); xParameters.add("CookieParameters"); if (pathPieces[3].equals("servers")) { - // #/paths/somePath/servers/0 - pathPieces[4] = toServerFilename(pathPieces[4], null); + if (pathPieces.length == 5) { + // #/paths/somePath/servers/0 + pathPieces[4] = toServerFilename(pathPieces[4], jsonPath); + } else { + // #/paths/somePath/servers/0/variables + pathPieces[4] = "server" + pathPieces[4]; + pathPieces[5] = "Variables"; + } } else if (pathPieces[3].equals("parameters")) { // #/paths/somePath/parameters/0 pathPieces[4] = toParameterFilename(pathPieces[4], null); @@ -3834,8 +3849,14 @@ private void updatePathsFilepath(String[] pathPieces) { return; } if (pathPieces[4].equals("servers")) { - // #/paths/somePath/get/servers/someServer - pathPieces[5] = toServerFilename(pathPieces[5], null); + if (pathPieces.length == 6) { + // #/paths/somePath/get/servers/0 + pathPieces[5] = toServerFilename(pathPieces[5], jsonPath); + } else { + // #/paths/somePath/get/servers/0/variables + pathPieces[5] = "server" + pathPieces[5]; + pathPieces[6] = "Variables"; + } } else if (pathPieces[4].equals("security")) { // #/paths/somePath/get/security/0 pathPieces[5] = toSecurityRequirementObjectFilename(pathPieces[5], null); @@ -3903,10 +3924,18 @@ private void updatePathsFilepath(String[] pathPieces) { } private void updateServersFilepath(String[] pathPieces) { - if (pathPieces.length < 3) { + if (pathPieces.length == 2) { + // #/servers return; + } else if (pathPieces.length == 3) { + // #/servers/0 + String jsonPath = "#/servers/" + pathPieces[2]; + pathPieces[2] = toServerFilename(pathPieces[2], jsonPath); + } else { + // #/servers/0/variables + pathPieces[2] = toServerFilename(pathPieces[2], null).toLowerCase(Locale.ROOT); + pathPieces[3] = "Variables"; } - pathPieces[2] = toServerFilename(pathPieces[2], null); } private void updateSecurityFilepath(String[] pathPieces) { @@ -4821,7 +4850,7 @@ public CodegenKey getKey(String key, String keyType, String sourceJsonPath) { usedKey = escapeUnsafeCharacters(key); isValid = isValid(usedKey); snakeCaseName = toServerFilename(usedKey, sourceJsonPath); - pascalCaseName = getPascalCaseServer(usedKey); + pascalCaseName = getPascalCaseServer(usedKey, sourceJsonPath); break; } if (pascalCaseName != null) { @@ -5037,14 +5066,16 @@ public List fromServers(List servers, String jsonPath) { boolean rootServer = jsonPath.equals("#/servers"); for (Server server : servers) { String serverJsonPath = jsonPath + "/" + i; - CodegenKey jsonPathPiece = getKey(String.valueOf(i), "servers"); + CodegenKey jsonPathPiece = getKey(String.valueOf(i), "servers", serverJsonPath); CodegenText description = getCodegenText(server.getDescription()); + String subpackage = getSubpackage(serverJsonPath); CodegenServer cs = new CodegenServer( removeTrailingSlash(server.getUrl()), // because trailing slash has no impact on server and path needs slash as first char description, fromServerVariables(server.getVariables(), serverJsonPath + "/variables"), jsonPathPiece, - rootServer + rootServer, + subpackage ); codegenServers.add(cs); i ++; diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java b/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java index 552fedc6aba..b15c2c4249f 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java @@ -197,7 +197,7 @@ public interface Generator { String toSecurityRequirementObjectFilename(String baseName, String jsonPath); - String getPascalCaseServer(String baseName); + String getPascalCaseServer(String baseName, String jsonPath); String toModelImport(String refClass); @@ -298,4 +298,6 @@ public interface Generator { CodegenParameter fromParameter(Parameter parameter, String sourceJsonPath); Function> getSchemasFn(); + + boolean generateSeparateServerSchemas(); } diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java index 8f1c161c951..7da5f43132b 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java @@ -23,12 +23,14 @@ import io.swagger.v3.oas.models.examples.Example; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.parameters.Parameter; +import io.swagger.v3.oas.models.servers.Server; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.openapijsonschematools.codegen.common.ModelUtils; import org.openapijsonschematools.codegen.generators.generatormetadata.FeatureSet; import org.openapijsonschematools.codegen.generators.generatormetadata.Stability; import org.openapijsonschematools.codegen.generators.generatormetadata.features.ClientModificationFeature; +import org.openapijsonschematools.codegen.generators.generatormetadata.features.GlobalFeature; import org.openapijsonschematools.codegen.generators.generatormetadata.features.SchemaFeature; import org.openapijsonschematools.codegen.common.CodegenConstants; import org.openapijsonschematools.codegen.generators.generatormetadata.GeneratorType; @@ -113,9 +115,34 @@ protected Stability getStability() { public String toModuleFilename(String name, String jsonPath) { String usedName = sanitizeName(name, "[^a-zA-Z0-9]+"); // todo check if empty and if so them use enum name + // todo fix this, this does not handle names starting with numbers return usedName.toLowerCase(Locale.ROOT); } + @Override + public String getPascalCaseServer(String basename, String jsonPath) { + if (jsonPath != null) { + String[] pathPieces = jsonPath.split("/"); + if (jsonPath.startsWith("#/servers/")) { + return "Server"+pathPieces[2]; + } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 5) { + // #/paths/somePath/servers/0 + CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); + return pathKey.pascalCase + "Server"+ pathPieces[4]; + } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 6) { + // #/paths/somePath/get/servers/0 + CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); + return pathKey.pascalCase + StringUtils.capitalize(pathPieces[3]) + "Server"+ pathPieces[5]; + } + } + return "Server" + basename; + } + + @Override + public String toServerFilename(String basename, String jsonPath) { + return getPascalCaseServer(basename, jsonPath); + } + @Override public String generatorLanguageVersion() { return "17"; @@ -125,7 +152,7 @@ public JavaClientGenerator() { super(); modifyFeatureSet(features -> features - .includeDocumentationFeatures(DocumentationFeature.Readme) + .includeDocumentationFeatures(DocumentationFeature.Readme, DocumentationFeature.Servers) .wireFormatFeatures(EnumSet.of(WireFormatFeature.JSON, WireFormatFeature.XML)) .securityFeatures(EnumSet.noneOf( SecurityFeature.class @@ -133,8 +160,8 @@ public JavaClientGenerator() { .excludeSchemaFeatures( SchemaFeature.Not ) - .includeClientModificationFeatures( - ClientModificationFeature.BasePath + .includeGlobalFeatures( + GlobalFeature.Servers ) ); @@ -543,6 +570,7 @@ public void processOpts() { keywordValidatorFiles.add("BooleanValueMethod"); keywordValidatorFiles.add("BigDecimalValidator"); keywordValidatorFiles.add("CustomIsoparser"); + keywordValidatorFiles.add("DefaultValueMethod"); keywordValidatorFiles.add("DoubleEnumValidator"); keywordValidatorFiles.add("DoubleValueMethod"); keywordValidatorFiles.add("EnumValidator"); @@ -1283,6 +1311,7 @@ public Set getImports(String sourceJsonPath, CodegenSchema schema, Featu addPatternValidator(schema, imports); addMultipleOfValidator(schema, imports); addAdditionalPropertiesImports(schema, imports); + addDefaultValueImport(schema, imports); if (schema.mapValueSchema != null) { imports.addAll(getDeeperImports(sourceJsonPath, schema.mapValueSchema)); } @@ -1304,6 +1333,13 @@ private void addPatternValidator(CodegenSchema schema, Set imports) { } } + private void addDefaultValueImport(CodegenSchema schema, Set imports) { + if (schema.defaultValue != null) { + imports.add("import "+packageName + ".schemas.validation.DefaultValueMethod;"); + } + } + + private void addEnumValidator(CodegenSchema schema, Set imports) { if (schema.enumInfo != null) { imports.add("import "+packageName + ".schemas.SetMaker;"); @@ -1420,6 +1456,7 @@ private void addBooleanSchemaImports(Set imports, CodegenSchema schema) addAnyOfValidator(schema, imports); addOneOfValidator(schema, imports); addEnumValidator(schema, imports); + addDefaultValueImport(schema, imports); } private void addNullSchemaImports(Set imports, CodegenSchema schema) { @@ -1428,6 +1465,7 @@ private void addNullSchemaImports(Set imports, CodegenSchema schema) { addAnyOfValidator(schema, imports); addOneOfValidator(schema, imports); addEnumValidator(schema, imports); + addDefaultValueImport(schema, imports); } private void addMapSchemaImports(Set imports, CodegenSchema schema) { @@ -1462,6 +1500,7 @@ private void addNumberSchemaImports(Set imports, CodegenSchema schema) { addOneOfValidator(schema, imports); addEnumValidator(schema, imports); addMultipleOfValidator(schema, imports); + addDefaultValueImport(schema, imports); } private void addStringSchemaImports(Set imports, CodegenSchema schema) { @@ -1484,6 +1523,7 @@ private void addStringSchemaImports(Set imports, CodegenSchema schema) { addOneOfValidator(schema, imports); addEnumValidator(schema, imports); addPatternValidator(schema, imports); + addDefaultValueImport(schema, imports); } @@ -2165,6 +2205,35 @@ public String toExampleValue(Schema p) { } } + @Override + public void setOpenAPI(OpenAPI openAPI) { + super.setOpenAPI(openAPI); + List servers = openAPI.getServers(); + if (servers != null && !servers.isEmpty()) { + supportingFiles.add(new SupportingFile( + "src/main/java/packagename/servers/ServerWithoutVariables.hbs", + packagePath() + File.separatorChar + "servers", + "ServerWithoutVariables.java")); + + supportingFiles.add(new SupportingFile( + "src/main/java/packagename/servers/ServerWithVariables.hbs", + packagePath() + File.separatorChar + "servers", + "ServerWithVariables.java")); + jsonPathTemplateFiles.put( + CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVER, + new HashMap<>() {{ + put("src/main/java/packagename/servers/Server.hbs", ".java"); + }} + ); + jsonPathDocTemplateFiles.put( + CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVER, + new HashMap<>() {{ + put("src/main/java/packagename/servers/ServerDoc.hbs", ".md"); + }} + ); + } + } + @Override public void preprocessOpenAPI(OpenAPI openAPI) { super.preprocessOpenAPI(openAPI); @@ -2423,4 +2492,9 @@ public void setParentArtifactId(final String parentArtifactId) { public void setParentVersion(final String parentVersion) { this.parentVersion = parentVersion; } + + @Override + public boolean generateSeparateServerSchemas() { + return true; + } } diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/PythonClientGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/PythonClientGenerator.java index c0e77d763ea..1232335093b 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/PythonClientGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/PythonClientGenerator.java @@ -1859,11 +1859,6 @@ public String toSecurityRequirementObjectFilename(String basename, String jsonPa return "security_requirement_object_" + basename; } - @Override - public String getPascalCaseServer(String basename) { - return "Server" + basename; - } - @Override public String getPascalCaseParameter(String name) { try { diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServer.java b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServer.java index cfc562a4de5..aeed2b45a99 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServer.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServer.java @@ -9,8 +9,9 @@ public class CodegenServer { public final CodegenSchema variables; public final CodegenKey jsonPathPiece; public final boolean rootServer; + public final String subpackage; // needed to define java package - public CodegenServer(String url, CodegenText description, CodegenSchema variables, CodegenKey jsonPathPiece, boolean rootServer) { + public CodegenServer(String url, CodegenText description, CodegenSchema variables, CodegenKey jsonPathPiece, boolean rootServer, String subpackage) { this.url = url; this.description = description; this.variables = variables; @@ -25,6 +26,7 @@ public CodegenServer(String url, CodegenText description, CodegenSchema variable } this.defaultUrl = defaultUrl; } + this.subpackage = subpackage; } @Override @@ -37,12 +39,13 @@ public boolean equals(Object o) { Objects.equals(variables, that.variables) && Objects.equals(jsonPathPiece, that.jsonPathPiece) && Objects.equals(rootServer, that.rootServer) && - Objects.equals(defaultUrl, that.defaultUrl); + Objects.equals(defaultUrl, that.defaultUrl) && + Objects.equals(subpackage, that.subpackage); } @Override public int hashCode() { - return Objects.hash(url, description, variables, jsonPathPiece, rootServer, defaultUrl); + return Objects.hash(url, description, variables, jsonPathPiece, rootServer, defaultUrl, subpackage); } @Override diff --git a/src/main/resources/java/README.hbs b/src/main/resources/java/README.hbs index d1ba310ab3a..8dbee186824 100644 --- a/src/main/resources/java/README.hbs +++ b/src/main/resources/java/README.hbs @@ -152,6 +152,15 @@ Please follow the [installation procedure](#installation) and then use the JsonS {{packageName}}.components.schemas to validate input payloads and instances of validated Map and List output classes. Json schemas allow multiple types for one schema, so a schema's validate method can have allowed input and output types. +{{#if servers}} + +## Servers +| server_index | Class | Description | +| ------------ | ----- | ----------- | +{{#each servers}} +| {{@key}} | [{{jsonPathPiece.pascalCase}}](docs/servers/{{jsonPathPiece.pascalCase}}.md) |{{#if description}} {{description.originalWithBr}}{{/if}} | +{{/each}} +{{/if}} {{#if schemas}} ## Component Schemas diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs index 9c6fc83f2ad..243ec0eb9b3 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs @@ -93,6 +93,9 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements { {{#if patternInfo}} {{> src/main/java/packagename/components/schemas/SchemaClass/_pattern }} {{/if}} + {{#if defaultValue}} + {{> src/main/java/packagename/components/schemas/SchemaClass/_default }} + {{/if}} ); } diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs index c327264abfa..8e9e892926a 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs @@ -35,6 +35,9 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements B {{#if enumInfo}} {{> src/main/java/packagename/components/schemas/SchemaClass/_enum }} {{/if}} + {{#if defaultValue}} + {{> src/main/java/packagename/components/schemas/SchemaClass/_default }} + {{/if}} ); } diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs index f6bbb31cafc..c02cf098f8e 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs @@ -35,6 +35,9 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements N {{#if enumInfo}} {{> src/main/java/packagename/components/schemas/SchemaClass/_enum }} {{/if}} + {{#if defaultValue}} + {{> src/main/java/packagename/components/schemas/SchemaClass/_default }} + {{/if}} ); } diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs index 34725c6bf01..e06296fc773 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs @@ -53,6 +53,9 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements { {{#if enumInfo}} {{> src/main/java/packagename/components/schemas/SchemaClass/_enum }} {{/if}} + {{#if defaultValue}} + {{> src/main/java/packagename/components/schemas/SchemaClass/_default }} + {{/if}} ); } diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs index 5ccfb9d3dad..7667c3ee446 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs @@ -1,6 +1,6 @@ -public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements StringSchemaValidator{{#and enumInfo enumInfo.typeToValues.string}}, StringEnumValidator{{/and}} { +public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements StringSchemaValidator{{#and enumInfo enumInfo.typeToValues.string}}, StringEnumValidator{{/and}}{{#if defaultValue}}, DefaultValueMethod{{/if}} { {{#if componentModule}} /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. @@ -47,6 +47,9 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements S {{#if patternInfo}} {{> src/main/java/packagename/components/schemas/SchemaClass/_pattern }} {{/if}} + {{#if defaultValue}} + {{> src/main/java/packagename/components/schemas/SchemaClass/_default }} + {{/if}} ); } @@ -56,9 +59,6 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements S } return instance; } -{{#if defaultValue}} - {{!> components/schemas/schema_cls/_default }} -{{/if}} {{#if constInfo}} {{!> components/schemas/schema_cls/_const }} {{/if}} @@ -73,4 +73,12 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements S {{/if}} {{> src/main/java/packagename/components/schemas/SchemaClass/_validate_implementor }} {{> src/main/java/packagename/components/schemas/SchemaClass/_getNewInstanceObject_implementor }} +{{#if defaultValue}} + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } +{{/if}} } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_default.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_default.hbs new file mode 100644 index 00000000000..e082984cb09 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_default.hbs @@ -0,0 +1,43 @@ +{{#with defaultValue}} + {{#if forDocs}} + {{#eq type "string"}} +defaultValue = "{{{value}}}" + {{~else}} + {{#or (eq type "number") (eq type "integer")}} +defaultValue = {{{value}}} + {{~else}} + {{#eq type "boolean"}} + {{#if value}} +defaultValue = true + {{~else}} +defaultValue = false + {{~/if}} + {{else}} + {{#eq type "null"}} +defaultValue = null + {{~/eq}} + {{/eq}} + {{/or}} + {{/eq}} + {{else}} + {{#eq type "string"}} +.defaultValue("{{{value}}}") + {{else}} + {{#or (eq type "number") (eq type "integer")}} +.defaultValue({{{value}}}) + {{else}} + {{#eq type "boolean"}} + {{#if value}} +.defaultValue(true) + {{else}} +.defaultValue(false) + {{/if}} + {{else}} + {{#eq type "null"}} +.defaultValue(null) + {{/eq}} + {{/eq}} + {{/or}} + {{/eq}} + {{/if}} +{{/with}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/docschema_fields_field.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/docschema_fields_field.hbs index 2ddddc932de..197f07f82b3 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/docschema_fields_field.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/docschema_fields_field.hbs @@ -69,4 +69,7 @@ {{/if}} {{#if patternInfo}} | Pattern | {{> src/main/java/packagename/components/schemas/SchemaClass/_pattern }} | +{{/if}} +{{#if defaultValue}} +| @Nullable Object | {{> src/main/java/packagename/components/schemas/SchemaClass/_default }} | {{/if}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/DefaultValueMethod.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/DefaultValueMethod.hbs new file mode 100644 index 00000000000..3f920e4042c --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/DefaultValueMethod.hbs @@ -0,0 +1,5 @@ +package {{{packageName}}}.schemas.validation; + +public interface DefaultValueMethod { + T defaultValue(); +} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs index f520aeb7cc8..f6ed0934df6 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs @@ -42,6 +42,7 @@ public abstract class JsonSchema { public final @Nullable Boolean uniqueItems; public final @Nullable Set<@Nullable Object> enumValues; public final @Nullable Pattern pattern; + public final @Nullable Object defaultValue; private final LinkedHashMap keywordToValidator; protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { @@ -214,6 +215,7 @@ public abstract class JsonSchema { new PatternValidator(this.pattern) ); } + this.defaultValue = jsonSchemaInfo.defaultValue; this.keywordToValidator = keywordToValidator; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs index ccd26fb5bf3..692bfa42bcc 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs @@ -128,4 +128,9 @@ public class JsonSchemaInfo { this.pattern = pattern; return this; } + public @Nullable Object defaultValue = null; + public JsonSchemaInfo defaultValue(@Nullable Object defaultValue) { + this.defaultValue = defaultValue; + return this; + } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/Server.hbs b/src/main/resources/java/src/main/java/packagename/servers/Server.hbs new file mode 100644 index 00000000000..91006af68e1 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/Server.hbs @@ -0,0 +1,53 @@ +{{#with server}} +package {{{packageName}}}.{{subpackage}}; + + {{#with variables}} +import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; +import {{{packageName}}}.configurations.SchemaConfiguration; + {{#neq ../subpackage "servers"}} +import {{{packageName}}}.servers.ServerWithVariables; + {{/neq}} +import {{{packageName}}}.schemas.validation.MapUtils; +import {{{packageName}}}.{{subpackage}}.{{containerJsonPathPiece.pascalCase}}; + +import java.util.AbstractMap; + +class {{../jsonPathPiece.pascalCase}} extends ServerWithVariables<{{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}}> { + {{#if ../description}} + /* + {{../description.original}} + */ + {{/if}} + + public {{../jsonPathPiece.pascalCase}}() { + super( + "{{../url}}", + {{containerJsonPathPiece.pascalCase}}.{{jsonPathPiece.pascalCase}}.getInstance().validate( + MapUtils.makeMap( + {{#each requiredProperties}} + new AbstractMap.SimpleEntry<>("{{{@key.original}}}", {{../containerJsonPathPiece.pascalCase}}.{{jsonPathPiece.pascalCase}}.getInstance().defaultValue()){{#unless @last}},{{/unless}} + {{/each}} + ), + new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()) + ) + ); + } + public {{../jsonPathPiece.pascalCase}}({{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}} variables) { + super("{{../url}}", variables); + } +} + {{else}} +import {{{packageName}}}.servers.ServerWithoutVariables; + +class {{jsonPathPiece.pascalCase}} extends ServerWithoutVariables { + {{#if ../description}} + /* + {{../description.original}} + */ + {{/if}} + public {{jsonPathPiece.pascalCase}}() { + super("{{url}}"); + } +} + {{/with}} +{{/with}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerDoc.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerDoc.hbs new file mode 100644 index 00000000000..17ade947f49 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerDoc.hbs @@ -0,0 +1,49 @@ +{{#with server}} + {{#if rootServer}} +{{packageName}}.servers.{{jsonPathPiece.snakeCase}} + {{/if}} + {{#eq identifierPieces.size 0}} +{{> src/main/java/packagename/components/_helper_header_from_identifier_pieces identifierPieces=(append identifierPieces "Server" jsonPathPiece) }} + {{else}} +{{headerSize}} {{jsonPathPiece.pascalCase}} + {{/eq}} +public class {{jsonPathPiece.pascalCase}} + +A class that stores a server url + {{#if description}} + +{{headerSize}}# Description +{{description.originalWithBr}} + {{/if}} + {{#if refInfo}} + +| Ref Class | Description | +| --------- | ----------- | +| [{{refInfo.refClass}}](../../servers/{{refInfo.refModule}}.{{refInfo.refClass}}.md#) |{{#with getDeepestRef}}{{#if description}} {{description.originalWithBr}}{{/if}}{{/with}} | + {{else}} + +{{headerSize}}## Constructor Summary +| Constructor and Description | +| --------------------------- | +| {{jsonPathPiece.pascalCase}}()
Creates a server{{#if variables}} using default values for variables{{/if}} | +{{#if variables}} +| {{jsonPathPiece.pascalCase}}({{#with variables}}[{{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}}](#{{mapOutputJsonPathPiece.kebabCase}}) variables{{/with}})
Creates a server using input values for variables | +{{/if}} + +{{headerSize}}## Field Summary +| Modifier and Type | Field and Description | +| ----------------- | --------------------- | +| String | url = "{{{url}}}" | +{{#with variables}} +| [{{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}}](#{{mapOutputJsonPathPiece.kebabCase}}) | variables | +{{/with}} + {{#with variables}} + +{{> src/main/java/packagename/components/schemas/Schema_doc schema=this headerSize=(join headerSize "#" "") }} + {{/with}} + {{/if}} + {{#if rootServer}} + +[[Back to top]](#top) {{> _helper_footer_links readmePath="../../" serversLink=true}} + {{/if}} +{{/with}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerWithVariables.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerWithVariables.hbs new file mode 100644 index 00000000000..4eb078b7ca2 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerWithVariables.hbs @@ -0,0 +1,17 @@ +package {{{packageName}}}.servers; + +import java.util.Map; + +public abstract class ServerWithVariables> { + public final String url; + public final T variables; + + protected ServerWithVariables(String url, T variables) { + this.variables = variables; + for (Map.Entry entry: variables.entrySet()) { + url = url.replace("{" + entry.getKey() + "}", entry.getValue()); + } + this.url = url; + } +} + diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerWithoutVariables.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerWithoutVariables.hbs new file mode 100644 index 00000000000..06ae0d7e5f0 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerWithoutVariables.hbs @@ -0,0 +1,10 @@ +package {{{packageName}}}.servers; + +public abstract class ServerWithoutVariables { + public final String url; + + protected ServerWithoutVariables(String url) { + this.url = url; + } +} +