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

Java, adds then + else keyword functionality #371

Merged
merged 8 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|DependentRequired|✓|OAS3
|DependentSchemas|✓|OAS3
|Discriminator|✗|OAS2,OAS3
|Else||OAS3
|Else||OAS3
|Enum|✓|OAS2,OAS3
|ExclusiveMinimum|✓|OAS2,OAS3
|ExclusiveMaximum|✓|OAS2,OAS3
Expand Down Expand Up @@ -310,7 +310,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|PropertyNames|✓|OAS3
|Ref|✓|OAS2,OAS3
|Required|✓|OAS2,OAS3
|Then||OAS3
|Then||OAS3
|Type|✓|OAS2,OAS3
|UnevaluatedItems|✗|OAS3
|UnevaluatedProperties|✗|OAS3
Expand Down
2 changes: 2 additions & 0 deletions samples/client/3_0_3_unit_test/java/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/DependentRequ
src/main/java/org/openapijsonschematools/client/schemas/validation/DependentSchemasValidator.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/ElseValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java
Expand Down Expand Up @@ -268,6 +269,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValid
src/main/java/org/openapijsonschematools/client/schemas/validation/StringEnumValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/StringSchemaValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/StringValueMethod.java
src/main/java/org/openapijsonschematools/client/schemas/validation/ThenValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.openapijsonschematools.client.schemas.validation;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.openapijsonschematools.client.exceptions.InvalidTypeException;
import org.openapijsonschematools.client.exceptions.ValidationException;

public class ElseValidator implements KeywordValidator {
@Override
public @Nullable PathToSchemasMap validate(
ValidationData data
) {
var elseSchema = data.schema().elseSchema;
if (elseSchema == null) {
return null;
}
var ifPathToSchemas = data.ifPathToSchemas();
if (ifPathToSchemas == null) {
// if unset
return null;
}
if (!ifPathToSchemas.isEmpty()) {
// if validation is true
return null;
}
JsonSchema elseSchemaInstance = JsonSchemaFactory.getInstance(elseSchema);
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
var elsePathToSchemas = JsonSchema.validate(elseSchemaInstance, data.arg(), data.validationMetadata());
// todo capture validation error and describe it as an else error?
pathToSchemas.update(elsePathToSchemas);
return pathToSchemas;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public abstract class JsonSchema {
public final @Nullable Map<Pattern, Class<? extends JsonSchema>> patternProperties;
public final @Nullable List<Class<? extends JsonSchema>> prefixItems;
public final @Nullable Class<? extends JsonSchema> ifSchema;
public final @Nullable Class<? extends JsonSchema> then;
public final @Nullable Class<? extends JsonSchema> elseSchema;
private final LinkedHashMap<String, KeywordValidator> keywordToValidator;

protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
Expand Down Expand Up @@ -198,6 +200,14 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
if (this.ifSchema != null) {
keywordToValidator.put("if", new IfValidator());
}
this.then = jsonSchemaInfo.then;
if (this.then != null) {
keywordToValidator.put("then", new ThenValidator());
}
this.elseSchema = jsonSchemaInfo.elseSchema;
if (this.elseSchema != null) {
keywordToValidator.put("else", new ElseValidator());
}
this.keywordToValidator = keywordToValidator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,14 @@ public JsonSchemaInfo ifSchema(Class<? extends JsonSchema> ifSchema) {
this.ifSchema = ifSchema;
return this;
}
public @Nullable Class<? extends JsonSchema> then = null;
public JsonSchemaInfo then(Class<? extends JsonSchema> then) {
this.then = then;
return this;
}
public @Nullable Class<? extends JsonSchema> elseSchema = null;
public JsonSchemaInfo elseSchema(Class<? extends JsonSchema> elseSchema) {
this.elseSchema = elseSchema;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.openapijsonschematools.client.schemas.validation;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.openapijsonschematools.client.exceptions.InvalidTypeException;
import org.openapijsonschematools.client.exceptions.ValidationException;

public class ThenValidator implements KeywordValidator {
@Override
public @Nullable PathToSchemasMap validate(
ValidationData data
) {
var then = data.schema().then;
if (then == null) {
return null;
}
var ifPathToSchemas = data.ifPathToSchemas();
if (ifPathToSchemas == null) {
// if unset
return null;
}
if (ifPathToSchemas.isEmpty()) {
// if validation is false
return null;
}
JsonSchema thenSchema = JsonSchemaFactory.getInstance(then);
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
var thenPathToSchemas = JsonSchema.validate(thenSchema, data.arg(), data.validationMetadata());
// todo capture validation error and describe it as an then error?
pathToSchemas.update(ifPathToSchemas);
pathToSchemas.update(thenPathToSchemas);
return pathToSchemas;
}
}
16 changes: 16 additions & 0 deletions samples/client/3_1_0_unit_test/java/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ docs/components/schemas/ForbiddenProperty.md
docs/components/schemas/HostnameFormat.md
docs/components/schemas/IdnEmailFormat.md
docs/components/schemas/IdnHostnameFormat.md
docs/components/schemas/IfAndElseWithoutThen.md
docs/components/schemas/IfAndThenWithoutElse.md
docs/components/schemas/IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.md
docs/components/schemas/IgnoreElseWithoutIf.md
docs/components/schemas/IgnoreIfWithoutThenOrElse.md
docs/components/schemas/IgnoreThenWithoutIf.md
docs/components/schemas/IntegerTypeMatchesIntegers.md
docs/components/schemas/Ipv4Format.md
docs/components/schemas/Ipv6Format.md
Expand Down Expand Up @@ -78,6 +83,7 @@ docs/components/schemas/NestedAnyofToCheckValidationSemantics.md
docs/components/schemas/NestedItems.md
docs/components/schemas/NestedOneofToCheckValidationSemantics.md
docs/components/schemas/NonAsciiPatternWithAdditionalproperties.md
docs/components/schemas/NonInterferenceAcrossCombinedSchemas.md
docs/components/schemas/Not.md
docs/components/schemas/NotMoreComplexSchema.md
docs/components/schemas/NotMultipleTypes.md
Expand Down Expand Up @@ -130,6 +136,7 @@ docs/components/schemas/UriFormat.md
docs/components/schemas/UriReferenceFormat.md
docs/components/schemas/UriTemplateFormat.md
docs/components/schemas/UuidFormat.md
docs/components/schemas/ValidateAgainstCorrectBranchThenVsElse.md
docs/servers/Server0.md
pom.xml
src/main/java/org/openapijsonschematools/client/components/schemas/ASchemaGivenForPrefixitems.java
Expand Down Expand Up @@ -180,7 +187,12 @@ src/main/java/org/openapijsonschematools/client/components/schemas/ForbiddenProp
src/main/java/org/openapijsonschematools/client/components/schemas/HostnameFormat.java
src/main/java/org/openapijsonschematools/client/components/schemas/IdnEmailFormat.java
src/main/java/org/openapijsonschematools/client/components/schemas/IdnHostnameFormat.java
src/main/java/org/openapijsonschematools/client/components/schemas/IfAndElseWithoutThen.java
src/main/java/org/openapijsonschematools/client/components/schemas/IfAndThenWithoutElse.java
src/main/java/org/openapijsonschematools/client/components/schemas/IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.java
src/main/java/org/openapijsonschematools/client/components/schemas/IgnoreElseWithoutIf.java
src/main/java/org/openapijsonschematools/client/components/schemas/IgnoreIfWithoutThenOrElse.java
src/main/java/org/openapijsonschematools/client/components/schemas/IgnoreThenWithoutIf.java
src/main/java/org/openapijsonschematools/client/components/schemas/IntegerTypeMatchesIntegers.java
src/main/java/org/openapijsonschematools/client/components/schemas/Ipv4Format.java
src/main/java/org/openapijsonschematools/client/components/schemas/Ipv6Format.java
Expand Down Expand Up @@ -211,6 +223,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/NestedAnyofTo
src/main/java/org/openapijsonschematools/client/components/schemas/NestedItems.java
src/main/java/org/openapijsonschematools/client/components/schemas/NestedOneofToCheckValidationSemantics.java
src/main/java/org/openapijsonschematools/client/components/schemas/NonAsciiPatternWithAdditionalproperties.java
src/main/java/org/openapijsonschematools/client/components/schemas/NonInterferenceAcrossCombinedSchemas.java
src/main/java/org/openapijsonschematools/client/components/schemas/Not.java
src/main/java/org/openapijsonschematools/client/components/schemas/NotMoreComplexSchema.java
src/main/java/org/openapijsonschematools/client/components/schemas/NotMultipleTypes.java
Expand Down Expand Up @@ -263,6 +276,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/UriFormat.jav
src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceFormat.java
src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java
src/main/java/org/openapijsonschematools/client/components/schemas/UuidFormat.java
src/main/java/org/openapijsonschematools/client/components/schemas/ValidateAgainstCorrectBranchThenVsElse.java
src/main/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlags.java
src/main/java/org/openapijsonschematools/client/configurations/SchemaConfiguration.java
src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java
Expand Down Expand Up @@ -305,6 +319,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/DependentRequ
src/main/java/org/openapijsonschematools/client/schemas/validation/DependentSchemasValidator.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/ElseValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java
Expand Down Expand Up @@ -356,6 +371,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValid
src/main/java/org/openapijsonschematools/client/schemas/validation/StringEnumValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/StringSchemaValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/StringValueMethod.java
src/main/java/org/openapijsonschematools/client/schemas/validation/ThenValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java
Expand Down
7 changes: 7 additions & 0 deletions samples/client/3_1_0_unit_test/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ allowed input and output types.
| [HostnameFormat.HostnameFormat1](docs/components/schemas/HostnameFormat.md#hostnameformat1) | |
| [IdnEmailFormat.IdnEmailFormat1](docs/components/schemas/IdnEmailFormat.md#idnemailformat1) | |
| [IdnHostnameFormat.IdnHostnameFormat1](docs/components/schemas/IdnHostnameFormat.md#idnhostnameformat1) | |
| [IfAndElseWithoutThen.IfAndElseWithoutThen1](docs/components/schemas/IfAndElseWithoutThen.md#ifandelsewithoutthen1) | |
| [IfAndThenWithoutElse.IfAndThenWithoutElse1](docs/components/schemas/IfAndThenWithoutElse.md#ifandthenwithoutelse1) | |
| [IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence1](docs/components/schemas/IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.md#ifappearsattheendwhenserializedkeywordprocessingsequence1) | |
| [IgnoreElseWithoutIf.IgnoreElseWithoutIf1](docs/components/schemas/IgnoreElseWithoutIf.md#ignoreelsewithoutif1) | |
| [IgnoreIfWithoutThenOrElse.IgnoreIfWithoutThenOrElse1](docs/components/schemas/IgnoreIfWithoutThenOrElse.md#ignoreifwithoutthenorelse1) | |
| [IgnoreThenWithoutIf.IgnoreThenWithoutIf1](docs/components/schemas/IgnoreThenWithoutIf.md#ignorethenwithoutif1) | |
| [IntegerTypeMatchesIntegers.IntegerTypeMatchesIntegers1](docs/components/schemas/IntegerTypeMatchesIntegers.md#integertypematchesintegers1) | |
| [Ipv4Format.Ipv4Format1](docs/components/schemas/Ipv4Format.md#ipv4format1) | |
| [Ipv6Format.Ipv6Format1](docs/components/schemas/Ipv6Format.md#ipv6format1) | |
Expand Down Expand Up @@ -236,6 +241,7 @@ allowed input and output types.
| [NestedItems.NestedItems1](docs/components/schemas/NestedItems.md#nesteditems1) | |
| [NestedOneofToCheckValidationSemantics.NestedOneofToCheckValidationSemantics1](docs/components/schemas/NestedOneofToCheckValidationSemantics.md#nestedoneoftocheckvalidationsemantics1) | |
| [NonAsciiPatternWithAdditionalproperties.NonAsciiPatternWithAdditionalproperties1](docs/components/schemas/NonAsciiPatternWithAdditionalproperties.md#nonasciipatternwithadditionalproperties1) | |
| [NonInterferenceAcrossCombinedSchemas.NonInterferenceAcrossCombinedSchemas1](docs/components/schemas/NonInterferenceAcrossCombinedSchemas.md#noninterferenceacrosscombinedschemas1) | |
| [Not.Not1](docs/components/schemas/Not.md#not1) | |
| [NotMoreComplexSchema.NotMoreComplexSchema1](docs/components/schemas/NotMoreComplexSchema.md#notmorecomplexschema1) | |
| [NotMultipleTypes.NotMultipleTypes1](docs/components/schemas/NotMultipleTypes.md#notmultipletypes1) | |
Expand Down Expand Up @@ -288,3 +294,4 @@ allowed input and output types.
| [UriReferenceFormat.UriReferenceFormat1](docs/components/schemas/UriReferenceFormat.md#urireferenceformat1) | |
| [UriTemplateFormat.UriTemplateFormat1](docs/components/schemas/UriTemplateFormat.md#uritemplateformat1) | |
| [UuidFormat.UuidFormat1](docs/components/schemas/UuidFormat.md#uuidformat1) | |
| [ValidateAgainstCorrectBranchThenVsElse.ValidateAgainstCorrectBranchThenVsElse1](docs/components/schemas/ValidateAgainstCorrectBranchThenVsElse.md#validateagainstcorrectbranchthenvselse1) | |
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# IfAndElseWithoutThen
org.openapijsonschematools.client.components.schemas.IfAndElseWithoutThen.java
public class IfAndElseWithoutThen

A class that contains necessary nested
- schema classes (which validate payloads), extends JsonSchema

## Nested Class Summary
| Modifier and Type | Class and Description |
| ----------------- | ---------------------- |
| static class | [IfAndElseWithoutThen.IfAndElseWithoutThen1](#ifandelsewithoutthen1)<br> schema class |
| static class | [IfAndElseWithoutThen.IfSchema](#ifschema)<br> schema class |
| static class | [IfAndElseWithoutThen.ElseSchema](#elseschema)<br> schema class |

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

A schema class that validates payloads

### Field Summary
| Modifier and Type | Field and Description |
| ----------------- | ---------------------- |
| Class<? extends JsonSchema> | if = [IfSchema.class](#ifschema) |
| Class<? extends JsonSchema> | elseSchema = [ElseSchema.class](#elseschema) |

### Method Summary
| Modifier and Type | Method and Description |
| ----------------- | ---------------------- |
| String | validate(String arg, SchemaConfiguration configuration) |
| Void | validate(Void arg, SchemaConfiguration configuration) |
| int | validate(int arg, SchemaConfiguration configuration) |
| long | validate(long arg, SchemaConfiguration configuration) |
| float | validate(float arg, SchemaConfiguration configuration) |
| double | validate(double arg, SchemaConfiguration configuration) |
| boolean | validate(boolean arg, SchemaConfiguration configuration) |
| FrozenMap<String, @Nullable Object> | validate(Map&lt;?, ?&gt; arg, SchemaConfiguration configuration) |
| FrozenList<@Nullable Object> | validate(List<?> arg, SchemaConfiguration configuration) |
| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) |
## IfSchema
public static class IfSchema<br>
extends JsonSchema

A schema class that validates payloads

### Field Summary
| Modifier and Type | Field and Description |
| ----------------- | ---------------------- |
| Number | exclusiveMaximum = 0 |

### Method Summary
| Modifier and Type | Method and Description |
| ----------------- | ---------------------- |
| String | validate(String arg, SchemaConfiguration configuration) |
| Void | validate(Void arg, SchemaConfiguration configuration) |
| int | validate(int arg, SchemaConfiguration configuration) |
| long | validate(long arg, SchemaConfiguration configuration) |
| float | validate(float arg, SchemaConfiguration configuration) |
| double | validate(double arg, SchemaConfiguration configuration) |
| boolean | validate(boolean arg, SchemaConfiguration configuration) |
| FrozenMap<String, @Nullable Object> | validate(Map&lt;?, ?&gt; arg, SchemaConfiguration configuration) |
| FrozenList<@Nullable Object> | validate(List<?> arg, SchemaConfiguration configuration) |
| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) |
## ElseSchema
public static class ElseSchema<br>
extends JsonSchema

A schema class that validates payloads

### Field Summary
| Modifier and Type | Field and Description |
| ----------------- | ---------------------- |
| BigDecimal | multipleOf = new BigDecimal("2") |

### Method Summary
| Modifier and Type | Method and Description |
| ----------------- | ---------------------- |
| String | validate(String arg, SchemaConfiguration configuration) |
| Void | validate(Void arg, SchemaConfiguration configuration) |
| int | validate(int arg, SchemaConfiguration configuration) |
| long | validate(long arg, SchemaConfiguration configuration) |
| float | validate(float arg, SchemaConfiguration configuration) |
| double | validate(double arg, SchemaConfiguration configuration) |
| boolean | validate(boolean arg, SchemaConfiguration configuration) |
| FrozenMap<String, @Nullable Object> | validate(Map&lt;?, ?&gt; arg, SchemaConfiguration configuration) |
| FrozenList<@Nullable Object> | validate(List<?> arg, SchemaConfiguration configuration) |
| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) |
[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)
Loading