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

Commit 3d1e9a3

Browse files
authored
Java, reduce unchecked casts (#331)
* Removes unchecked cast * Samples regen
1 parent 658c2f1 commit 3d1e9a3

File tree

3 files changed

+75
-90
lines changed
  • samples/client
    • 3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation
    • petstore/java/src/main/java/org/openapijsonschematools/client/schemas/validation
  • src/main/resources/java/src/main/java/packagename/schemas/validation

3 files changed

+75
-90
lines changed

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

+25-30
Original file line numberDiff line numberDiff line change
@@ -125,43 +125,40 @@ private static Object castToAllowedTypes(Object arg, List<Object> pathToItem, Se
125125

126126
private PathToSchemasMap getPathToSchemas(Object arg, ValidationMetadata validationMetadata, Set<List<Object>> pathSet) {
127127
PathToSchemasMap pathToSchemasMap = new PathToSchemasMap();
128-
if (validationMetadata.validationRanEarlier(this)) {
129-
// todo add deeper validated schemas
130-
} else {
131-
PathToSchemasMap otherPathToSchemas = validate(arg, validationMetadata);
132-
pathToSchemasMap.update(otherPathToSchemas);
133-
for (var schemas: pathToSchemasMap.values()) {
134-
JsonSchema<?, ?, ?, ?, ?, ?> firstSchema = schemas.entrySet().iterator().next().getKey();
135-
schemas.clear();
136-
schemas.put(firstSchema, null);
137-
}
138-
pathSet.removeAll(pathToSchemasMap.keySet());
139-
if (!pathSet.isEmpty()) {
140-
LinkedHashMap<JsonSchema<?, ?, ?, ?, ?, ?>, Void> unsetAnyTypeSchema = new LinkedHashMap<>();
141-
unsetAnyTypeSchema.put(JsonSchemaFactory.getInstance(UnsetAnyTypeJsonSchema.class), null);
142-
for (List<Object> pathToItem: pathSet) {
143-
pathToSchemasMap.put(pathToItem, unsetAnyTypeSchema);
144-
}
128+
// todo add check of validationMetadata.validationRanEarlier(this)
129+
PathToSchemasMap otherPathToSchemas = validate(arg, validationMetadata);
130+
pathToSchemasMap.update(otherPathToSchemas);
131+
for (var schemas: pathToSchemasMap.values()) {
132+
JsonSchema<?, ?, ?, ?, ?, ?> firstSchema = schemas.entrySet().iterator().next().getKey();
133+
schemas.clear();
134+
schemas.put(firstSchema, null);
135+
}
136+
pathSet.removeAll(pathToSchemasMap.keySet());
137+
if (!pathSet.isEmpty()) {
138+
LinkedHashMap<JsonSchema<?, ?, ?, ?, ?, ?>, Void> unsetAnyTypeSchema = new LinkedHashMap<>();
139+
unsetAnyTypeSchema.put(JsonSchemaFactory.getInstance(UnsetAnyTypeJsonSchema.class), null);
140+
for (List<Object> pathToItem: pathSet) {
141+
pathToSchemasMap.put(pathToItem, unsetAnyTypeSchema);
145142
}
146143
}
147144
return pathToSchemasMap;
148145
}
149146

150-
private FrozenMap<String, MapOutValueType> getProperties(Map<String, MapInValueType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
147+
private FrozenMap<String, MapOutValueType> getProperties(Map<?, ?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
151148
LinkedHashMap<String, MapOutValueType> properties = new LinkedHashMap<>();
152-
for(Map.Entry<String, MapInValueType> entry: arg.entrySet()) {
153-
String propertyName = entry.getKey();
149+
for(Map.Entry<?, ?> entry: arg.entrySet()) {
150+
String propertyName = (String) entry.getKey();
154151
List<Object> propertyPathToItem = new ArrayList<>(pathToItem);
155152
propertyPathToItem.add(propertyName);
156-
MapInValueType value = entry.getValue();
157-
JsonSchema<?, ?, MapOutValueType, ?, ?, ?> propertySchema = (JsonSchema<?, ?, MapOutValueType, ?, ?, ?>) pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey();
153+
MapInValueType value = (MapInValueType) entry.getValue();
154+
JsonSchema<?, ?, ?, ?, ?, ?> propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey();
158155
MapOutValueType castValue = (MapOutValueType) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas);
159156
properties.put(propertyName, castValue);
160157
}
161158
return new FrozenMap<>(properties);
162159
}
163160

164-
private FrozenList<ListOutItemType> getItems(List<ListInItemType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
161+
private FrozenList<ListOutItemType> getItems(List<?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
165162
ArrayList<ListOutItemType> items = new ArrayList<>();
166163
int i = 0;
167164
for (Object item: arg) {
@@ -183,21 +180,21 @@ protected ListOutType getListOutputInstance(FrozenList<ListOutItemType> arg) {
183180
return (ListOutType) arg;
184181
}
185182

186-
private MapOutType getNewInstance(Map<String, MapInValueType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
183+
private MapOutType getNewInstance(Map<?, ?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
187184
FrozenMap<String, MapOutValueType> usedArg = getProperties(arg, pathToItem, pathToSchemas);
188185
return getMapOutputInstance(usedArg);
189186
}
190187

191-
private ListOutType getNewInstance(List<ListInItemType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
188+
private ListOutType getNewInstance(List<?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
192189
FrozenList<ListOutItemType> usedArg = getItems(arg, pathToItem, pathToSchemas);
193190
return getListOutputInstance(usedArg);
194191
}
195192

196193
private Object getNewInstance(Object arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
197194
if (arg instanceof List) {
198-
return getNewInstance((List<ListInItemType>) arg, pathToItem, pathToSchemas);
195+
return getNewInstance((List<?>) arg, pathToItem, pathToSchemas);
199196
} else if (arg instanceof Map) {
200-
return getNewInstance((Map<String, MapInValueType>) arg, pathToItem, pathToSchemas);
197+
return getNewInstance((Map<?, ?>) arg, pathToItem, pathToSchemas);
201198
}
202199
// str, int, float, boolean, null, FileIO, bytes
203200
return arg;
@@ -254,9 +251,7 @@ protected ListOutType validateList(List<ListInItemType> arg, SchemaConfiguration
254251
// todo add bytes and FileIO
255252

256253
public Object validate(Object arg, SchemaConfiguration configuration) throws ValidationException {
257-
if (arg instanceof Map || arg instanceof List) {
258-
// todo don't run validation if the instance is one of the class generic types
259-
}
254+
// todo don't run validation if the instance is one of the class generic types
260255
Set<List<Object>> pathSet = new HashSet<>();
261256
List<Object> pathToItem = new ArrayList<>();
262257
pathToItem.add("args[0]");

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

+25-30
Original file line numberDiff line numberDiff line change
@@ -125,43 +125,40 @@ private static Object castToAllowedTypes(Object arg, List<Object> pathToItem, Se
125125

126126
private PathToSchemasMap getPathToSchemas(Object arg, ValidationMetadata validationMetadata, Set<List<Object>> pathSet) {
127127
PathToSchemasMap pathToSchemasMap = new PathToSchemasMap();
128-
if (validationMetadata.validationRanEarlier(this)) {
129-
// todo add deeper validated schemas
130-
} else {
131-
PathToSchemasMap otherPathToSchemas = validate(arg, validationMetadata);
132-
pathToSchemasMap.update(otherPathToSchemas);
133-
for (var schemas: pathToSchemasMap.values()) {
134-
JsonSchema<?, ?, ?, ?, ?, ?> firstSchema = schemas.entrySet().iterator().next().getKey();
135-
schemas.clear();
136-
schemas.put(firstSchema, null);
137-
}
138-
pathSet.removeAll(pathToSchemasMap.keySet());
139-
if (!pathSet.isEmpty()) {
140-
LinkedHashMap<JsonSchema<?, ?, ?, ?, ?, ?>, Void> unsetAnyTypeSchema = new LinkedHashMap<>();
141-
unsetAnyTypeSchema.put(JsonSchemaFactory.getInstance(UnsetAnyTypeJsonSchema.class), null);
142-
for (List<Object> pathToItem: pathSet) {
143-
pathToSchemasMap.put(pathToItem, unsetAnyTypeSchema);
144-
}
128+
// todo add check of validationMetadata.validationRanEarlier(this)
129+
PathToSchemasMap otherPathToSchemas = validate(arg, validationMetadata);
130+
pathToSchemasMap.update(otherPathToSchemas);
131+
for (var schemas: pathToSchemasMap.values()) {
132+
JsonSchema<?, ?, ?, ?, ?, ?> firstSchema = schemas.entrySet().iterator().next().getKey();
133+
schemas.clear();
134+
schemas.put(firstSchema, null);
135+
}
136+
pathSet.removeAll(pathToSchemasMap.keySet());
137+
if (!pathSet.isEmpty()) {
138+
LinkedHashMap<JsonSchema<?, ?, ?, ?, ?, ?>, Void> unsetAnyTypeSchema = new LinkedHashMap<>();
139+
unsetAnyTypeSchema.put(JsonSchemaFactory.getInstance(UnsetAnyTypeJsonSchema.class), null);
140+
for (List<Object> pathToItem: pathSet) {
141+
pathToSchemasMap.put(pathToItem, unsetAnyTypeSchema);
145142
}
146143
}
147144
return pathToSchemasMap;
148145
}
149146

150-
private FrozenMap<String, MapOutValueType> getProperties(Map<String, MapInValueType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
147+
private FrozenMap<String, MapOutValueType> getProperties(Map<?, ?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
151148
LinkedHashMap<String, MapOutValueType> properties = new LinkedHashMap<>();
152-
for(Map.Entry<String, MapInValueType> entry: arg.entrySet()) {
153-
String propertyName = entry.getKey();
149+
for(Map.Entry<?, ?> entry: arg.entrySet()) {
150+
String propertyName = (String) entry.getKey();
154151
List<Object> propertyPathToItem = new ArrayList<>(pathToItem);
155152
propertyPathToItem.add(propertyName);
156-
MapInValueType value = entry.getValue();
157-
JsonSchema<?, ?, MapOutValueType, ?, ?, ?> propertySchema = (JsonSchema<?, ?, MapOutValueType, ?, ?, ?>) pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey();
153+
MapInValueType value = (MapInValueType) entry.getValue();
154+
JsonSchema<?, ?, ?, ?, ?, ?> propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey();
158155
MapOutValueType castValue = (MapOutValueType) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas);
159156
properties.put(propertyName, castValue);
160157
}
161158
return new FrozenMap<>(properties);
162159
}
163160

164-
private FrozenList<ListOutItemType> getItems(List<ListInItemType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
161+
private FrozenList<ListOutItemType> getItems(List<?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
165162
ArrayList<ListOutItemType> items = new ArrayList<>();
166163
int i = 0;
167164
for (Object item: arg) {
@@ -183,21 +180,21 @@ protected ListOutType getListOutputInstance(FrozenList<ListOutItemType> arg) {
183180
return (ListOutType) arg;
184181
}
185182

186-
private MapOutType getNewInstance(Map<String, MapInValueType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
183+
private MapOutType getNewInstance(Map<?, ?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
187184
FrozenMap<String, MapOutValueType> usedArg = getProperties(arg, pathToItem, pathToSchemas);
188185
return getMapOutputInstance(usedArg);
189186
}
190187

191-
private ListOutType getNewInstance(List<ListInItemType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
188+
private ListOutType getNewInstance(List<?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
192189
FrozenList<ListOutItemType> usedArg = getItems(arg, pathToItem, pathToSchemas);
193190
return getListOutputInstance(usedArg);
194191
}
195192

196193
private Object getNewInstance(Object arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
197194
if (arg instanceof List) {
198-
return getNewInstance((List<ListInItemType>) arg, pathToItem, pathToSchemas);
195+
return getNewInstance((List<?>) arg, pathToItem, pathToSchemas);
199196
} else if (arg instanceof Map) {
200-
return getNewInstance((Map<String, MapInValueType>) arg, pathToItem, pathToSchemas);
197+
return getNewInstance((Map<?, ?>) arg, pathToItem, pathToSchemas);
201198
}
202199
// str, int, float, boolean, null, FileIO, bytes
203200
return arg;
@@ -254,9 +251,7 @@ protected ListOutType validateList(List<ListInItemType> arg, SchemaConfiguration
254251
// todo add bytes and FileIO
255252

256253
public Object validate(Object arg, SchemaConfiguration configuration) throws ValidationException {
257-
if (arg instanceof Map || arg instanceof List) {
258-
// todo don't run validation if the instance is one of the class generic types
259-
}
254+
// todo don't run validation if the instance is one of the class generic types
260255
Set<List<Object>> pathSet = new HashSet<>();
261256
List<Object> pathToItem = new ArrayList<>();
262257
pathToItem.add("args[0]");

src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs

+25-30
Original file line numberDiff line numberDiff line change
@@ -125,43 +125,40 @@ public abstract class JsonSchema <MapInValueType, MapOutValueType, MapOutType, L
125125

126126
private PathToSchemasMap getPathToSchemas(Object arg, ValidationMetadata validationMetadata, Set<List<Object>> pathSet) {
127127
PathToSchemasMap pathToSchemasMap = new PathToSchemasMap();
128-
if (validationMetadata.validationRanEarlier(this)) {
129-
// todo add deeper validated schemas
130-
} else {
131-
PathToSchemasMap otherPathToSchemas = validate(arg, validationMetadata);
132-
pathToSchemasMap.update(otherPathToSchemas);
133-
for (var schemas: pathToSchemasMap.values()) {
134-
JsonSchema<?, ?, ?, ?, ?, ?> firstSchema = schemas.entrySet().iterator().next().getKey();
135-
schemas.clear();
136-
schemas.put(firstSchema, null);
137-
}
138-
pathSet.removeAll(pathToSchemasMap.keySet());
139-
if (!pathSet.isEmpty()) {
140-
LinkedHashMap<JsonSchema<?, ?, ?, ?, ?, ?>, Void> unsetAnyTypeSchema = new LinkedHashMap<>();
141-
unsetAnyTypeSchema.put(JsonSchemaFactory.getInstance(UnsetAnyTypeJsonSchema.class), null);
142-
for (List<Object> pathToItem: pathSet) {
143-
pathToSchemasMap.put(pathToItem, unsetAnyTypeSchema);
144-
}
128+
// todo add check of validationMetadata.validationRanEarlier(this)
129+
PathToSchemasMap otherPathToSchemas = validate(arg, validationMetadata);
130+
pathToSchemasMap.update(otherPathToSchemas);
131+
for (var schemas: pathToSchemasMap.values()) {
132+
JsonSchema<?, ?, ?, ?, ?, ?> firstSchema = schemas.entrySet().iterator().next().getKey();
133+
schemas.clear();
134+
schemas.put(firstSchema, null);
135+
}
136+
pathSet.removeAll(pathToSchemasMap.keySet());
137+
if (!pathSet.isEmpty()) {
138+
LinkedHashMap<JsonSchema<?, ?, ?, ?, ?, ?>, Void> unsetAnyTypeSchema = new LinkedHashMap<>();
139+
unsetAnyTypeSchema.put(JsonSchemaFactory.getInstance(UnsetAnyTypeJsonSchema.class), null);
140+
for (List<Object> pathToItem: pathSet) {
141+
pathToSchemasMap.put(pathToItem, unsetAnyTypeSchema);
145142
}
146143
}
147144
return pathToSchemasMap;
148145
}
149146

150-
private FrozenMap<String, MapOutValueType> getProperties(Map<String, MapInValueType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
147+
private FrozenMap<String, MapOutValueType> getProperties(Map<?, ?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
151148
LinkedHashMap<String, MapOutValueType> properties = new LinkedHashMap<>();
152-
for(Map.Entry<String, MapInValueType> entry: arg.entrySet()) {
153-
String propertyName = entry.getKey();
149+
for(Map.Entry<?, ?> entry: arg.entrySet()) {
150+
String propertyName = (String) entry.getKey();
154151
List<Object> propertyPathToItem = new ArrayList<>(pathToItem);
155152
propertyPathToItem.add(propertyName);
156-
MapInValueType value = entry.getValue();
157-
JsonSchema<?, ?, MapOutValueType, ?, ?, ?> propertySchema = (JsonSchema<?, ?, MapOutValueType, ?, ?, ?>) pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey();
153+
MapInValueType value = (MapInValueType) entry.getValue();
154+
JsonSchema<?, ?, ?, ?, ?, ?> propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey();
158155
MapOutValueType castValue = (MapOutValueType) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas);
159156
properties.put(propertyName, castValue);
160157
}
161158
return new FrozenMap<>(properties);
162159
}
163160

164-
private FrozenList<ListOutItemType> getItems(List<ListInItemType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
161+
private FrozenList<ListOutItemType> getItems(List<?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
165162
ArrayList<ListOutItemType> items = new ArrayList<>();
166163
int i = 0;
167164
for (Object item: arg) {
@@ -183,21 +180,21 @@ public abstract class JsonSchema <MapInValueType, MapOutValueType, MapOutType, L
183180
return (ListOutType) arg;
184181
}
185182

186-
private MapOutType getNewInstance(Map<String, MapInValueType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
183+
private MapOutType getNewInstance(Map<?, ?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
187184
FrozenMap<String, MapOutValueType> usedArg = getProperties(arg, pathToItem, pathToSchemas);
188185
return getMapOutputInstance(usedArg);
189186
}
190187

191-
private ListOutType getNewInstance(List<ListInItemType> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
188+
private ListOutType getNewInstance(List<?> arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
192189
FrozenList<ListOutItemType> usedArg = getItems(arg, pathToItem, pathToSchemas);
193190
return getListOutputInstance(usedArg);
194191
}
195192

196193
private Object getNewInstance(Object arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
197194
if (arg instanceof List) {
198-
return getNewInstance((List<ListInItemType>) arg, pathToItem, pathToSchemas);
195+
return getNewInstance((List<?>) arg, pathToItem, pathToSchemas);
199196
} else if (arg instanceof Map) {
200-
return getNewInstance((Map<String, MapInValueType>) arg, pathToItem, pathToSchemas);
197+
return getNewInstance((Map<?, ?>) arg, pathToItem, pathToSchemas);
201198
}
202199
// str, int, float, boolean, null, FileIO, bytes
203200
return arg;
@@ -254,9 +251,7 @@ public abstract class JsonSchema <MapInValueType, MapOutValueType, MapOutType, L
254251
// todo add bytes and FileIO
255252

256253
public Object validate(Object arg, SchemaConfiguration configuration) throws ValidationException {
257-
if (arg instanceof Map || arg instanceof List) {
258-
// todo don't run validation if the instance is one of the class generic types
259-
}
254+
// todo don't run validation if the instance is one of the class generic types
260255
Set<List<Object>> pathSet = new HashSet<>();
261256
List<Object> pathToItem = new ArrayList<>();
262257
pathToItem.add("args[0]");

0 commit comments

Comments
 (0)