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

Commit 6254a92

Browse files
authored
Java, improves schema generics (#332)
* Changes JsonSchema to be parameterized on 3 generics * Updates UnsetAnyTypeJsonSchema * Updates AnyType and NotAnyType schemas * Updates AnyType and NotAnyType schema imports * Fixes JsonSchema imports * Fixes BooleanJsonSchema * Updates DateJsonSchema * Updates DateTimeJsonSchema * Updates StringJsonSchema * Fixes UuidJsonSchema * Updates NullJsonSchema * Updates DecimalJsonSchema * Updates NumberJsonSchema * Fixes JsonSchemaTest * Adds validate methods to AnyType * Adds more validate methods to NumberSchema * Updates number schema tests * Fixes float and double validate methods * Updates IntJsonSchema * Updates Int32JsonSchema * Updates Int64 and JsonSchema * Gets validator interface working for anytypejsonschema * Adds SchemaX validators and uses them in unsetanytypeschema * Updates AnyType and NotAnyType schemas * Fixes anytype and notanytype imports * Updates BooleanJsonSchema * Updates DateJsonSchema * Updates DateTimejson schema * Updates DecimalJsonSchema * Updates StringJsonSchema * Updates UuidJsonSchema * Updates NullJsonSchema * Updates NumberJsonSchema * Updates DoubleJsonSchema * Updates FloatJsonSchema * Updates Int32 * Updates Int64JsonSchema * Updates IntJsonSchema * Updates ListJsonSchema * Updates MapJsonSchema * Fixes validators and not any type schema import * Updates ArrayTypeSchemaTest * Fixes JsonSchemaTest * Updates ObjectTypeSchemaTest * Fixes NullSchemaTest * Fixes BoolSchemaTest * Fixes RefBooleanSchemaTest * Fixes MapSchemaTest * Updates ListSchemaTest * Fixes AnyTypeSchemaTest * Fixes AdditionalPropertiesValidatorTest * Adds ItemsValidatorTest * Fixes PropertiesValidatorTest * Fixes RequiredValidatorTest * Fixes number tests * Updates gen schema implements for one of types * String schemas updated * Updates FrozenMap classes to only pass in one generic * Adds missing JsonSchemaFactory import * Adds number schema validation, fixes class closing braces * Fixes validate method for integer types * Updates null schema validate * Updates boolean validation * Updates list validate method * Adds castToAllowedTypesfor list schemas * Updates list schema methods * Adds missing imports in anytype * Adds getNewinstance as required JsonSchema method * Updates anytype schemas to have object getNewInstance * Adds getNewInstance to all except list and map schema classes * Updates array test to use the new object method getNewInstance in list getNewInstance * Adds missing getNewInstance method to list schemas * Adds missing methods to boolean, null, number, and string schemas * Adds castToAllowedTypes * Adds getNewInstance * Adds object getNewInstance for map types * Adds validate method for maps * Fixes map output types input to generic * Fixes some list casting issues * Makes array and map bulders static classes * Updates any type validate methods * Adds missing override annotations * Improves validators used in multi type * Adds list output type for anytype schemas * Adds map output type generic to map validator to anytype schemas * Adds private instance for singleton in all schema classes * Adds getInstance methods * Has of method use getInstance * Fix map methods in anytype schemas * Makes constructors protected so schemas can be extended * Removes unused import * Removes JsonSchemaFactory usages * Fixes some java tests * Fixes object schema tests * Petstore regen * Samples regen * Adds missing > * Fixes multitype array interface * Fixes multitype map generics * Updates list schema template * Updates number schema validate implementation * Updates map validate implementation * Updates list validate implementation * Adds missing null and boolean validator imports * Fixes removes key type from frozenmap output types * Adds _castToAllowedTypesObject_implementor * Writes getNewInstance in all schemas for Object input * Fixes indentation in getNewInstance * Adds missing imports for FrozenList and FrozenMap * Samples regen * Fixes cast type and java test
1 parent 3d1e9a3 commit 6254a92

File tree

784 files changed

+57539
-10114
lines changed

Some content is hidden

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

784 files changed

+57539
-10114
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,19 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/MinLengthVali
226226
src/main/java/org/openapijsonschematools/client/schemas/validation/MinPropertiesValidator.java
227227
src/main/java/org/openapijsonschematools/client/schemas/validation/MinimumValidator.java
228228
src/main/java/org/openapijsonschematools/client/schemas/validation/MultipleOfValidator.java
229-
src/main/java/org/openapijsonschematools/client/schemas/validation/NonCollectionJsonSchema.java
230229
src/main/java/org/openapijsonschematools/client/schemas/validation/NotValidator.java
231230
src/main/java/org/openapijsonschematools/client/schemas/validation/OneOfValidator.java
232231
src/main/java/org/openapijsonschematools/client/schemas/validation/PathToSchemasMap.java
233232
src/main/java/org/openapijsonschematools/client/schemas/validation/PatternValidator.java
234233
src/main/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidator.java
235234
src/main/java/org/openapijsonschematools/client/schemas/validation/PropertyEntry.java
236235
src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValidator.java
236+
src/main/java/org/openapijsonschematools/client/schemas/validation/SchemaBooleanValidator.java
237+
src/main/java/org/openapijsonschematools/client/schemas/validation/SchemaListValidator.java
238+
src/main/java/org/openapijsonschematools/client/schemas/validation/SchemaMapValidator.java
239+
src/main/java/org/openapijsonschematools/client/schemas/validation/SchemaNullValidator.java
240+
src/main/java/org/openapijsonschematools/client/schemas/validation/SchemaNumberValidator.java
241+
src/main/java/org/openapijsonschematools/client/schemas/validation/SchemaStringValidator.java
237242
src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java
238243
src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java
239244
src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java

samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.java

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
package org.openapijsonschematools.client.components.schemas;
2+
import java.util.ArrayList;
3+
import java.util.HashSet;
24
import java.util.LinkedHashMap;
5+
import java.util.LinkedHashSet;
6+
import java.util.List;
37
import java.util.Map;
8+
import java.util.Objects;
49
import java.util.Set;
10+
import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags;
511
import org.openapijsonschematools.client.configurations.SchemaConfiguration;
12+
import org.openapijsonschematools.client.exceptions.InvalidTypeException;
613
import org.openapijsonschematools.client.exceptions.ValidationException;
714
import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema;
815
import org.openapijsonschematools.client.schemas.BooleanJsonSchema;
916
import org.openapijsonschematools.client.schemas.validation.AdditionalPropertiesValidator;
10-
import org.openapijsonschematools.client.schemas.validation.FrozenList;
1117
import org.openapijsonschematools.client.schemas.validation.FrozenMap;
1218
import org.openapijsonschematools.client.schemas.validation.JsonSchema;
13-
import org.openapijsonschematools.client.schemas.validation.JsonSchemaFactory;
1419
import org.openapijsonschematools.client.schemas.validation.KeywordEntry;
15-
import org.openapijsonschematools.client.schemas.validation.KeywordValidator;
20+
import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap;
1621
import org.openapijsonschematools.client.schemas.validation.PropertiesValidator;
1722
import org.openapijsonschematools.client.schemas.validation.PropertyEntry;
23+
import org.openapijsonschematools.client.schemas.validation.SchemaMapValidator;
1824
import org.openapijsonschematools.client.schemas.validation.TypeValidator;
25+
import org.openapijsonschematools.client.schemas.validation.ValidationMetadata;
1926

2027
public class AdditionalpropertiesAllowsASchemaWhichShouldValidate {
2128
// nest classes so all schemas and input/output classes can be public
@@ -30,8 +37,8 @@ public static class Foo extends AnyTypeJsonSchema {}
3037
public static class Bar extends AnyTypeJsonSchema {}
3138

3239

33-
public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMap extends FrozenMap<String, Object> {
34-
AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(FrozenMap<String, Object> m) {
40+
public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMap extends FrozenMap<Object> {
41+
AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(FrozenMap<Object> m) {
3542
super(m);
3643
}
3744
public static final Set<String> requiredKeys = Set.of();
@@ -40,7 +47,7 @@ public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMap exte
4047
"bar"
4148
);
4249
public static AdditionalpropertiesAllowsASchemaWhichShouldValidateMap of(Map<String, Object> arg, SchemaConfiguration configuration) throws ValidationException {
43-
return JsonSchemaFactory.getInstance(AdditionalpropertiesAllowsASchemaWhichShouldValidate1.class).validate(arg, configuration);
50+
return AdditionalpropertiesAllowsASchemaWhichShouldValidate1.getInstance().validate(arg, configuration);
4451
}
4552

4653
public Object foo() {
@@ -60,19 +67,20 @@ public boolean getAdditionalProperty(String name) {
6067
return (boolean) get(name);
6168
}
6269
}
63-
public class AdditionalpropertiesAllowsASchemaWhichShouldValidateMapInput {
70+
public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMapInput {
6471
// optionalProperties + additionalProperties
6572
}
6673

6774

68-
public static class AdditionalpropertiesAllowsASchemaWhichShouldValidate1 extends JsonSchema<Object, Object, AdditionalpropertiesAllowsASchemaWhichShouldValidateMap, Object, Object, FrozenList<Object>> {
75+
public static class AdditionalpropertiesAllowsASchemaWhichShouldValidate1 extends JsonSchema implements SchemaMapValidator<Object, Object, AdditionalpropertiesAllowsASchemaWhichShouldValidateMap> {
6976
/*
7077
NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
7178
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
7279
7380
Do not edit the class manually.
7481
*/
75-
public AdditionalpropertiesAllowsASchemaWhichShouldValidate1() {
82+
private static AdditionalpropertiesAllowsASchemaWhichShouldValidate1 instance;
83+
protected AdditionalpropertiesAllowsASchemaWhichShouldValidate1() {
7684
super(new LinkedHashMap<>(Map.ofEntries(
7785
new KeywordEntry("type", new TypeValidator(Set.of(FrozenMap.class))),
7886
new KeywordEntry("properties", new PropertiesValidator(Map.ofEntries(
@@ -82,13 +90,64 @@ public AdditionalpropertiesAllowsASchemaWhichShouldValidate1() {
8290
new KeywordEntry("additionalProperties", new AdditionalPropertiesValidator(AdditionalProperties.class))
8391
)));
8492
}
93+
94+
public static AdditionalpropertiesAllowsASchemaWhichShouldValidate1 getInstance() {
95+
if (instance == null) {
96+
instance = new AdditionalpropertiesAllowsASchemaWhichShouldValidate1();
97+
}
98+
return instance;
99+
}
85100

86101
@Override
87-
protected AdditionalpropertiesAllowsASchemaWhichShouldValidateMap getMapOutputInstance(FrozenMap<String, Object> arg) {
88-
return new AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(arg);
102+
public FrozenMap<Object> castToAllowedTypes(Map<String, Object> arg, List<Object> pathToItem, Set<List<Object>> pathSet) {
103+
pathSet.add(pathToItem);
104+
LinkedHashMap<String, Object> argFixed = new LinkedHashMap<>();
105+
for (Map.Entry<String, Object> entry: arg.entrySet()) {
106+
String key = entry.getKey();
107+
Object val = entry.getValue();
108+
List<Object> newPathToItem = new ArrayList<>(pathToItem);
109+
newPathToItem.add(key);
110+
Object fixedVal = (Object) castToAllowedObjectTypes(val, newPathToItem, pathSet);
111+
argFixed.put(key, fixedVal);
112+
}
113+
return new FrozenMap<>(argFixed);
114+
}
115+
116+
public AdditionalpropertiesAllowsASchemaWhichShouldValidateMap getNewInstance(FrozenMap<Object> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
117+
LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
118+
for(Map.Entry<String, Object> entry: arg.entrySet()) {
119+
String propertyName = entry.getKey();
120+
List<Object> propertyPathToItem = new ArrayList<>(pathToItem);
121+
propertyPathToItem.add(propertyName);
122+
Object value = entry.getValue();
123+
JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey();
124+
Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas);
125+
properties.put(propertyName, castValue);
126+
}
127+
FrozenMap<Object> castProperties = new FrozenMap<>(properties);
128+
return new AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(castProperties);
89129
}
90-
public AdditionalpropertiesAllowsASchemaWhichShouldValidateMap validate(Map<String, Object> arg, SchemaConfiguration configuration) throws ValidationException {
91-
return validateMap(arg, configuration);
130+
131+
@Override
132+
public AdditionalpropertiesAllowsASchemaWhichShouldValidateMap validate(Map<String, Object> arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException {
133+
Set<List<Object>> pathSet = new HashSet<>();
134+
List<Object> pathToItem = List.of("args[0");
135+
FrozenMap<Object> castArg = castToAllowedTypes(arg, pathToItem, pathSet);
136+
SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()));
137+
ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>());
138+
PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet);
139+
return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap);
140+
}
141+
142+
143+
@Override
144+
public Object getNewInstance(Object arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
145+
if (arg instanceof FrozenMap) {
146+
@SuppressWarnings("unchecked") FrozenMap<Object> castArg = (FrozenMap<Object>) arg;
147+
return getNewInstance(castArg, pathToItem, pathToSchemas);
148+
}
149+
throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema");
92150
}
93151
}
152+
94153
}

0 commit comments

Comments
 (0)