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

Commit 1092290

Browse files
authored
Java, adds then + else keyword functionality (#371)
* Adds then validator, partial else validator * Adds else validator implementation * Updates templates to implement then and else * 310 Sample document updated to include new then + else samples * 310 sample regen * Fixes else validator functionality * Fixes then functionality * Samples and docs updated
1 parent 73aa768 commit 1092290

File tree

55 files changed

+6975
-53
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+6975
-53
lines changed

docs/generators/java.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
282282
|DependentRequired|✓|OAS3
283283
|DependentSchemas|✓|OAS3
284284
|Discriminator|✗|OAS2,OAS3
285-
|Else||OAS3
285+
|Else||OAS3
286286
|Enum|✓|OAS2,OAS3
287287
|ExclusiveMinimum|✓|OAS2,OAS3
288288
|ExclusiveMaximum|✓|OAS2,OAS3
@@ -310,7 +310,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
310310
|PropertyNames|✓|OAS3
311311
|Ref|✓|OAS2,OAS3
312312
|Required|✓|OAS2,OAS3
313-
|Then||OAS3
313+
|Then||OAS3
314314
|Type|✓|OAS2,OAS3
315315
|UnevaluatedItems|✗|OAS3
316316
|UnevaluatedProperties|✗|OAS3

samples/client/3_0_3_unit_test/java/.openapi-generator/FILES

+2
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/DependentRequ
217217
src/main/java/org/openapijsonschematools/client/schemas/validation/DependentSchemasValidator.java
218218
src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleEnumValidator.java
219219
src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleValueMethod.java
220+
src/main/java/org/openapijsonschematools/client/schemas/validation/ElseValidator.java
220221
src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java
221222
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java
222223
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java
@@ -268,6 +269,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValid
268269
src/main/java/org/openapijsonschematools/client/schemas/validation/StringEnumValidator.java
269270
src/main/java/org/openapijsonschematools/client/schemas/validation/StringSchemaValidator.java
270271
src/main/java/org/openapijsonschematools/client/schemas/validation/StringValueMethod.java
272+
src/main/java/org/openapijsonschematools/client/schemas/validation/ThenValidator.java
271273
src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java
272274
src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java
273275
src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.openapijsonschematools.client.schemas.validation;
2+
3+
import org.checkerframework.checker.nullness.qual.Nullable;
4+
import org.openapijsonschematools.client.exceptions.InvalidTypeException;
5+
import org.openapijsonschematools.client.exceptions.ValidationException;
6+
7+
public class ElseValidator implements KeywordValidator {
8+
@Override
9+
public @Nullable PathToSchemasMap validate(
10+
ValidationData data
11+
) {
12+
var elseSchema = data.schema().elseSchema;
13+
if (elseSchema == null) {
14+
return null;
15+
}
16+
var ifPathToSchemas = data.ifPathToSchemas();
17+
if (ifPathToSchemas == null) {
18+
// if unset
19+
return null;
20+
}
21+
if (!ifPathToSchemas.isEmpty()) {
22+
// if validation is true
23+
return null;
24+
}
25+
JsonSchema elseSchemaInstance = JsonSchemaFactory.getInstance(elseSchema);
26+
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
27+
var elsePathToSchemas = JsonSchema.validate(elseSchemaInstance, data.arg(), data.validationMetadata());
28+
// todo capture validation error and describe it as an else error?
29+
pathToSchemas.update(elsePathToSchemas);
30+
return pathToSchemas;
31+
}
32+
}

samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public abstract class JsonSchema {
5555
public final @Nullable Map<Pattern, Class<? extends JsonSchema>> patternProperties;
5656
public final @Nullable List<Class<? extends JsonSchema>> prefixItems;
5757
public final @Nullable Class<? extends JsonSchema> ifSchema;
58+
public final @Nullable Class<? extends JsonSchema> then;
59+
public final @Nullable Class<? extends JsonSchema> elseSchema;
5860
private final LinkedHashMap<String, KeywordValidator> keywordToValidator;
5961

6062
protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
@@ -198,6 +200,14 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
198200
if (this.ifSchema != null) {
199201
keywordToValidator.put("if", new IfValidator());
200202
}
203+
this.then = jsonSchemaInfo.then;
204+
if (this.then != null) {
205+
keywordToValidator.put("then", new ThenValidator());
206+
}
207+
this.elseSchema = jsonSchemaInfo.elseSchema;
208+
if (this.elseSchema != null) {
209+
keywordToValidator.put("else", new ElseValidator());
210+
}
201211
this.keywordToValidator = keywordToValidator;
202212
}
203213

samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java

+10
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,14 @@ public JsonSchemaInfo ifSchema(Class<? extends JsonSchema> ifSchema) {
187187
this.ifSchema = ifSchema;
188188
return this;
189189
}
190+
public @Nullable Class<? extends JsonSchema> then = null;
191+
public JsonSchemaInfo then(Class<? extends JsonSchema> then) {
192+
this.then = then;
193+
return this;
194+
}
195+
public @Nullable Class<? extends JsonSchema> elseSchema = null;
196+
public JsonSchemaInfo elseSchema(Class<? extends JsonSchema> elseSchema) {
197+
this.elseSchema = elseSchema;
198+
return this;
199+
}
190200
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.openapijsonschematools.client.schemas.validation;
2+
3+
import org.checkerframework.checker.nullness.qual.Nullable;
4+
import org.openapijsonschematools.client.exceptions.InvalidTypeException;
5+
import org.openapijsonschematools.client.exceptions.ValidationException;
6+
7+
public class ThenValidator implements KeywordValidator {
8+
@Override
9+
public @Nullable PathToSchemasMap validate(
10+
ValidationData data
11+
) {
12+
var then = data.schema().then;
13+
if (then == null) {
14+
return null;
15+
}
16+
var ifPathToSchemas = data.ifPathToSchemas();
17+
if (ifPathToSchemas == null) {
18+
// if unset
19+
return null;
20+
}
21+
if (ifPathToSchemas.isEmpty()) {
22+
// if validation is false
23+
return null;
24+
}
25+
JsonSchema thenSchema = JsonSchemaFactory.getInstance(then);
26+
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
27+
var thenPathToSchemas = JsonSchema.validate(thenSchema, data.arg(), data.validationMetadata());
28+
// todo capture validation error and describe it as an then error?
29+
pathToSchemas.update(ifPathToSchemas);
30+
pathToSchemas.update(thenPathToSchemas);
31+
return pathToSchemas;
32+
}
33+
}

samples/client/3_1_0_unit_test/java/.openapi-generator/FILES

+16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ docs/components/schemas/ForbiddenProperty.md
4747
docs/components/schemas/HostnameFormat.md
4848
docs/components/schemas/IdnEmailFormat.md
4949
docs/components/schemas/IdnHostnameFormat.md
50+
docs/components/schemas/IfAndElseWithoutThen.md
51+
docs/components/schemas/IfAndThenWithoutElse.md
52+
docs/components/schemas/IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.md
53+
docs/components/schemas/IgnoreElseWithoutIf.md
5054
docs/components/schemas/IgnoreIfWithoutThenOrElse.md
55+
docs/components/schemas/IgnoreThenWithoutIf.md
5156
docs/components/schemas/IntegerTypeMatchesIntegers.md
5257
docs/components/schemas/Ipv4Format.md
5358
docs/components/schemas/Ipv6Format.md
@@ -78,6 +83,7 @@ docs/components/schemas/NestedAnyofToCheckValidationSemantics.md
7883
docs/components/schemas/NestedItems.md
7984
docs/components/schemas/NestedOneofToCheckValidationSemantics.md
8085
docs/components/schemas/NonAsciiPatternWithAdditionalproperties.md
86+
docs/components/schemas/NonInterferenceAcrossCombinedSchemas.md
8187
docs/components/schemas/Not.md
8288
docs/components/schemas/NotMoreComplexSchema.md
8389
docs/components/schemas/NotMultipleTypes.md
@@ -130,6 +136,7 @@ docs/components/schemas/UriFormat.md
130136
docs/components/schemas/UriReferenceFormat.md
131137
docs/components/schemas/UriTemplateFormat.md
132138
docs/components/schemas/UuidFormat.md
139+
docs/components/schemas/ValidateAgainstCorrectBranchThenVsElse.md
133140
docs/servers/Server0.md
134141
pom.xml
135142
src/main/java/org/openapijsonschematools/client/components/schemas/ASchemaGivenForPrefixitems.java
@@ -180,7 +187,12 @@ src/main/java/org/openapijsonschematools/client/components/schemas/ForbiddenProp
180187
src/main/java/org/openapijsonschematools/client/components/schemas/HostnameFormat.java
181188
src/main/java/org/openapijsonschematools/client/components/schemas/IdnEmailFormat.java
182189
src/main/java/org/openapijsonschematools/client/components/schemas/IdnHostnameFormat.java
190+
src/main/java/org/openapijsonschematools/client/components/schemas/IfAndElseWithoutThen.java
191+
src/main/java/org/openapijsonschematools/client/components/schemas/IfAndThenWithoutElse.java
192+
src/main/java/org/openapijsonschematools/client/components/schemas/IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.java
193+
src/main/java/org/openapijsonschematools/client/components/schemas/IgnoreElseWithoutIf.java
183194
src/main/java/org/openapijsonschematools/client/components/schemas/IgnoreIfWithoutThenOrElse.java
195+
src/main/java/org/openapijsonschematools/client/components/schemas/IgnoreThenWithoutIf.java
184196
src/main/java/org/openapijsonschematools/client/components/schemas/IntegerTypeMatchesIntegers.java
185197
src/main/java/org/openapijsonschematools/client/components/schemas/Ipv4Format.java
186198
src/main/java/org/openapijsonschematools/client/components/schemas/Ipv6Format.java
@@ -211,6 +223,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/NestedAnyofTo
211223
src/main/java/org/openapijsonschematools/client/components/schemas/NestedItems.java
212224
src/main/java/org/openapijsonschematools/client/components/schemas/NestedOneofToCheckValidationSemantics.java
213225
src/main/java/org/openapijsonschematools/client/components/schemas/NonAsciiPatternWithAdditionalproperties.java
226+
src/main/java/org/openapijsonschematools/client/components/schemas/NonInterferenceAcrossCombinedSchemas.java
214227
src/main/java/org/openapijsonschematools/client/components/schemas/Not.java
215228
src/main/java/org/openapijsonschematools/client/components/schemas/NotMoreComplexSchema.java
216229
src/main/java/org/openapijsonschematools/client/components/schemas/NotMultipleTypes.java
@@ -263,6 +276,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/UriFormat.jav
263276
src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceFormat.java
264277
src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java
265278
src/main/java/org/openapijsonschematools/client/components/schemas/UuidFormat.java
279+
src/main/java/org/openapijsonschematools/client/components/schemas/ValidateAgainstCorrectBranchThenVsElse.java
266280
src/main/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlags.java
267281
src/main/java/org/openapijsonschematools/client/configurations/SchemaConfiguration.java
268282
src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java
@@ -305,6 +319,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/DependentRequ
305319
src/main/java/org/openapijsonschematools/client/schemas/validation/DependentSchemasValidator.java
306320
src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleEnumValidator.java
307321
src/main/java/org/openapijsonschematools/client/schemas/validation/DoubleValueMethod.java
322+
src/main/java/org/openapijsonschematools/client/schemas/validation/ElseValidator.java
308323
src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java
309324
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java
310325
src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java
@@ -356,6 +371,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValid
356371
src/main/java/org/openapijsonschematools/client/schemas/validation/StringEnumValidator.java
357372
src/main/java/org/openapijsonschematools/client/schemas/validation/StringSchemaValidator.java
358373
src/main/java/org/openapijsonschematools/client/schemas/validation/StringValueMethod.java
374+
src/main/java/org/openapijsonschematools/client/schemas/validation/ThenValidator.java
359375
src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java
360376
src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java
361377
src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java

samples/client/3_1_0_unit_test/java/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,12 @@ allowed input and output types.
205205
| [HostnameFormat.HostnameFormat1](docs/components/schemas/HostnameFormat.md#hostnameformat1) | |
206206
| [IdnEmailFormat.IdnEmailFormat1](docs/components/schemas/IdnEmailFormat.md#idnemailformat1) | |
207207
| [IdnHostnameFormat.IdnHostnameFormat1](docs/components/schemas/IdnHostnameFormat.md#idnhostnameformat1) | |
208+
| [IfAndElseWithoutThen.IfAndElseWithoutThen1](docs/components/schemas/IfAndElseWithoutThen.md#ifandelsewithoutthen1) | |
209+
| [IfAndThenWithoutElse.IfAndThenWithoutElse1](docs/components/schemas/IfAndThenWithoutElse.md#ifandthenwithoutelse1) | |
210+
| [IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence1](docs/components/schemas/IfAppearsAtTheEndWhenSerializedKeywordProcessingSequence.md#ifappearsattheendwhenserializedkeywordprocessingsequence1) | |
211+
| [IgnoreElseWithoutIf.IgnoreElseWithoutIf1](docs/components/schemas/IgnoreElseWithoutIf.md#ignoreelsewithoutif1) | |
208212
| [IgnoreIfWithoutThenOrElse.IgnoreIfWithoutThenOrElse1](docs/components/schemas/IgnoreIfWithoutThenOrElse.md#ignoreifwithoutthenorelse1) | |
213+
| [IgnoreThenWithoutIf.IgnoreThenWithoutIf1](docs/components/schemas/IgnoreThenWithoutIf.md#ignorethenwithoutif1) | |
209214
| [IntegerTypeMatchesIntegers.IntegerTypeMatchesIntegers1](docs/components/schemas/IntegerTypeMatchesIntegers.md#integertypematchesintegers1) | |
210215
| [Ipv4Format.Ipv4Format1](docs/components/schemas/Ipv4Format.md#ipv4format1) | |
211216
| [Ipv6Format.Ipv6Format1](docs/components/schemas/Ipv6Format.md#ipv6format1) | |
@@ -236,6 +241,7 @@ allowed input and output types.
236241
| [NestedItems.NestedItems1](docs/components/schemas/NestedItems.md#nesteditems1) | |
237242
| [NestedOneofToCheckValidationSemantics.NestedOneofToCheckValidationSemantics1](docs/components/schemas/NestedOneofToCheckValidationSemantics.md#nestedoneoftocheckvalidationsemantics1) | |
238243
| [NonAsciiPatternWithAdditionalproperties.NonAsciiPatternWithAdditionalproperties1](docs/components/schemas/NonAsciiPatternWithAdditionalproperties.md#nonasciipatternwithadditionalproperties1) | |
244+
| [NonInterferenceAcrossCombinedSchemas.NonInterferenceAcrossCombinedSchemas1](docs/components/schemas/NonInterferenceAcrossCombinedSchemas.md#noninterferenceacrosscombinedschemas1) | |
239245
| [Not.Not1](docs/components/schemas/Not.md#not1) | |
240246
| [NotMoreComplexSchema.NotMoreComplexSchema1](docs/components/schemas/NotMoreComplexSchema.md#notmorecomplexschema1) | |
241247
| [NotMultipleTypes.NotMultipleTypes1](docs/components/schemas/NotMultipleTypes.md#notmultipletypes1) | |
@@ -288,3 +294,4 @@ allowed input and output types.
288294
| [UriReferenceFormat.UriReferenceFormat1](docs/components/schemas/UriReferenceFormat.md#urireferenceformat1) | |
289295
| [UriTemplateFormat.UriTemplateFormat1](docs/components/schemas/UriTemplateFormat.md#uritemplateformat1) | |
290296
| [UuidFormat.UuidFormat1](docs/components/schemas/UuidFormat.md#uuidformat1) | |
297+
| [ValidateAgainstCorrectBranchThenVsElse.ValidateAgainstCorrectBranchThenVsElse1](docs/components/schemas/ValidateAgainstCorrectBranchThenVsElse.md#validateagainstcorrectbranchthenvselse1) | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# IfAndElseWithoutThen
2+
org.openapijsonschematools.client.components.schemas.IfAndElseWithoutThen.java
3+
public class IfAndElseWithoutThen
4+
5+
A class that contains necessary nested
6+
- schema classes (which validate payloads), extends JsonSchema
7+
8+
## Nested Class Summary
9+
| Modifier and Type | Class and Description |
10+
| ----------------- | ---------------------- |
11+
| static class | [IfAndElseWithoutThen.IfAndElseWithoutThen1](#ifandelsewithoutthen1)<br> schema class |
12+
| static class | [IfAndElseWithoutThen.IfSchema](#ifschema)<br> schema class |
13+
| static class | [IfAndElseWithoutThen.ElseSchema](#elseschema)<br> schema class |
14+
15+
## IfAndElseWithoutThen1
16+
public static class IfAndElseWithoutThen1<br>
17+
extends JsonSchema
18+
19+
A schema class that validates payloads
20+
21+
### Field Summary
22+
| Modifier and Type | Field and Description |
23+
| ----------------- | ---------------------- |
24+
| Class<? extends JsonSchema> | if = [IfSchema.class](#ifschema) |
25+
| Class<? extends JsonSchema> | elseSchema = [ElseSchema.class](#elseschema) |
26+
27+
### Method Summary
28+
| Modifier and Type | Method and Description |
29+
| ----------------- | ---------------------- |
30+
| String | validate(String arg, SchemaConfiguration configuration) |
31+
| Void | validate(Void arg, SchemaConfiguration configuration) |
32+
| int | validate(int arg, SchemaConfiguration configuration) |
33+
| long | validate(long arg, SchemaConfiguration configuration) |
34+
| float | validate(float arg, SchemaConfiguration configuration) |
35+
| double | validate(double arg, SchemaConfiguration configuration) |
36+
| boolean | validate(boolean arg, SchemaConfiguration configuration) |
37+
| FrozenMap<String, @Nullable Object> | validate(Map&lt;?, ?&gt; arg, SchemaConfiguration configuration) |
38+
| FrozenList<@Nullable Object> | validate(List<?> arg, SchemaConfiguration configuration) |
39+
| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) |
40+
## IfSchema
41+
public static class IfSchema<br>
42+
extends JsonSchema
43+
44+
A schema class that validates payloads
45+
46+
### Field Summary
47+
| Modifier and Type | Field and Description |
48+
| ----------------- | ---------------------- |
49+
| Number | exclusiveMaximum = 0 |
50+
51+
### Method Summary
52+
| Modifier and Type | Method and Description |
53+
| ----------------- | ---------------------- |
54+
| String | validate(String arg, SchemaConfiguration configuration) |
55+
| Void | validate(Void arg, SchemaConfiguration configuration) |
56+
| int | validate(int arg, SchemaConfiguration configuration) |
57+
| long | validate(long arg, SchemaConfiguration configuration) |
58+
| float | validate(float arg, SchemaConfiguration configuration) |
59+
| double | validate(double arg, SchemaConfiguration configuration) |
60+
| boolean | validate(boolean arg, SchemaConfiguration configuration) |
61+
| FrozenMap<String, @Nullable Object> | validate(Map&lt;?, ?&gt; arg, SchemaConfiguration configuration) |
62+
| FrozenList<@Nullable Object> | validate(List<?> arg, SchemaConfiguration configuration) |
63+
| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) |
64+
## ElseSchema
65+
public static class ElseSchema<br>
66+
extends JsonSchema
67+
68+
A schema class that validates payloads
69+
70+
### Field Summary
71+
| Modifier and Type | Field and Description |
72+
| ----------------- | ---------------------- |
73+
| BigDecimal | multipleOf = new BigDecimal("2") |
74+
75+
### Method Summary
76+
| Modifier and Type | Method and Description |
77+
| ----------------- | ---------------------- |
78+
| String | validate(String arg, SchemaConfiguration configuration) |
79+
| Void | validate(Void arg, SchemaConfiguration configuration) |
80+
| int | validate(int arg, SchemaConfiguration configuration) |
81+
| long | validate(long arg, SchemaConfiguration configuration) |
82+
| float | validate(float arg, SchemaConfiguration configuration) |
83+
| double | validate(double arg, SchemaConfiguration configuration) |
84+
| boolean | validate(boolean arg, SchemaConfiguration configuration) |
85+
| FrozenMap<String, @Nullable Object> | validate(Map&lt;?, ?&gt; arg, SchemaConfiguration configuration) |
86+
| FrozenList<@Nullable Object> | validate(List<?> arg, SchemaConfiguration configuration) |
87+
| @Nullable Object | validate(@Nullable Object arg, SchemaConfiguration configuration) |
88+
[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)

0 commit comments

Comments
 (0)