Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java, adds pattern properties #366

Merged
merged 7 commits into from
Jan 18, 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
2 changes: 1 addition & 1 deletion docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Nullable|✓|OAS3
|OneOf|✓|OAS3
|Pattern|✓|OAS2,OAS3
|PatternProperties||OAS3
|PatternProperties||OAS3
|PrefixItems|✗|OAS3
|Properties|✓|OAS2,OAS3
|PropertyNames|✓|OAS3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/NullValueMeth
src/main/java/org/openapijsonschematools/client/schemas/validation/NumberSchemaValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/OneOfValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/PathToSchemasMap.java
src/main/java/org/openapijsonschematools/client/schemas/validation/PatternPropertiesValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/PatternValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/PropertyEntry.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A schema class that validates payloads
### Field Summary
| Modifier and Type | Field and Description |
| ----------------- | ---------------------- |
| Pattern | pattern =<br>&nbsp;&nbsp;&nbsp;&nbsp;"a+"<br>)))<br> |
| Pattern | pattern = Pattern.compile(<br>&nbsp;&nbsp;&nbsp;&nbsp;"a+"<br>)<br> |

### Method Summary
| Modifier and Type | Method and Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A schema class that validates payloads
### Field Summary
| Modifier and Type | Field and Description |
| ----------------- | ---------------------- |
| Pattern | pattern =<br>&nbsp;&nbsp;&nbsp;&nbsp;"^a*$"<br>)))<br> |
| Pattern | pattern = Pattern.compile(<br>&nbsp;&nbsp;&nbsp;&nbsp;"^a*$"<br>)<br> |

### Method Summary
| Modifier and Type | Method and Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public AdditionalPropertiesValidator(Class<? extends JsonSchema> additionalPrope
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Map<?, ?> mapArg)) {
return null;
Expand All @@ -34,11 +35,13 @@ public AdditionalPropertiesValidator(Class<? extends JsonSchema> additionalPrope
presentAdditionalProperties.removeAll(schema.properties.keySet());
}
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
// todo add handling for validatedPatternProperties
for(String addPropName: presentAdditionalProperties) {
@Nullable Object propValue = mapArg.get(addPropName);
List<Object> propPathToItem = new ArrayList<>(validationMetadata.pathToItem());
propPathToItem.add(addPropName);
if (patternPropertiesPathToSchemas != null && patternPropertiesPathToSchemas.containsKey(propPathToItem)) {
continue;
}
ValidationMetadata propValidationMetadata = new ValidationMetadata(
propPathToItem,
validationMetadata.configuration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public AllOfValidator(List<Class<? extends JsonSchema>> allOf) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
for(Class<? extends JsonSchema> allOfClass: allOf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public AnyOfValidator(List<Class<? extends JsonSchema>> anyOf) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
List<Class<? extends JsonSchema>> validatedAnyOfClasses = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public ConstValidator(@Nullable Object constValue) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (arg instanceof Number) {
BigDecimal castArg = getBigDecimal((Number) arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public ContainsValidator(Class<? extends JsonSchema> contains) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof List)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public DependentRequiredValidator(Map<String, Set<String>> dependentRequired) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Map)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public DependentSchemasValidator(Map<String, Class<? extends JsonSchema>> depend
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Map<?, ?> mapArg)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ private boolean enumContainsArg(@Nullable Object arg){
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (enumValues.isEmpty()) {
throw new ValidationException("No value can match enum because enum is empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public ExclusiveMaximumValidator(Number exclusiveMaximum) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Number)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public ExclusiveMinimumValidator(Number exclusiveMinimum) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Number)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ private Void validateStringFormat(String arg, ValidationMetadata validationMetad
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (arg instanceof Number) {
validateNumericFormat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public ItemsValidator(Class<? extends JsonSchema> items) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof List)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class JsonSchema {
public final @Nullable Class<? extends JsonSchema> propertyNames;
public @Nullable Map<String, Set<String>> dependentRequired;
public final @Nullable Map<String, Class<? extends JsonSchema>> dependentSchemas;
public @Nullable Map<Pattern, Class<? extends JsonSchema>> patternProperties;
private final LinkedHashMap<String, KeywordValidator> keywordToValidator;

protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
Expand Down Expand Up @@ -276,6 +277,13 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
new DependentSchemasValidator(this.dependentSchemas)
);
}
this.patternProperties = jsonSchemaInfo.patternProperties;
if (this.patternProperties != null) {
keywordToValidator.put(
"patternProperties",
new PatternPropertiesValidator(this.patternProperties)
);
}
this.keywordToValidator = keywordToValidator;
}

Expand All @@ -295,6 +303,11 @@ public static PathToSchemasMap validate(
if (containsValidator != null) {
containsPathToSchemas = containsValidator.getContainsPathToSchemas(arg, validationMetadata);
}
@Nullable PathToSchemasMap patternPropertiesPathToSchemas = null;
KeywordValidator patternPropertiesValidator = thisKeywordToValidator.get("patternProperties");
if (patternPropertiesValidator != null) {
patternPropertiesPathToSchemas = patternPropertiesValidator.getPatternPropertiesPathToSchemas(arg, validationMetadata);
}
for (Map.Entry<String, KeywordValidator> entry: thisKeywordToValidator.entrySet()) {
String jsonKeyword = entry.getKey();
if (disabledKeywords.contains(jsonKeyword)) {
Expand All @@ -308,7 +321,8 @@ public static PathToSchemasMap validate(
jsonSchema,
arg,
validationMetadata,
containsPathToSchemas
containsPathToSchemas,
patternPropertiesPathToSchemas
);
if (otherPathToSchemas == null) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,9 @@ public JsonSchemaInfo dependentSchemas(Map<String, Class<? extends JsonSchema>>
this.dependentSchemas = dependentSchemas;
return this;
}
public @Nullable Map<Pattern, Class<? extends JsonSchema>> patternProperties = null;
public JsonSchemaInfo patternProperties(Map<Pattern, Class<? extends JsonSchema>> patternProperties) {
this.patternProperties = patternProperties;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public interface KeywordValidator {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) throws ValidationException;

default List<PathToSchemasMap> getContainsPathToSchemas(
Expand All @@ -20,4 +21,11 @@ default List<PathToSchemasMap> getContainsPathToSchemas(
) {
return new ArrayList<>();
}

default PathToSchemasMap getPatternPropertiesPathToSchemas(
@Nullable Object arg,
ValidationMetadata validationMetadata
) {
return new PathToSchemasMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MaxContainsValidator(int maxContains) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof List)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MaxItemsValidator(int maxItems) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof List)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MaxLengthValidator(int maxLength) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof String)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public MaxPropertiesValidator(int maxProperties) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Map)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MaximumValidator(Number maximum) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Number)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MinContainsValidator(int minContains) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof List)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MinItemsValidator(int minItems) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof List)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MinLengthValidator(int minLength) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof String)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public MinPropertiesValidator(int minProperties) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Map)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public MinimumValidator(Number minimum) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Number)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public MultipleOfValidator(BigDecimal multipleOf) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
if (!(arg instanceof Number)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public NotValidator(Class<? extends JsonSchema> not) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
PathToSchemasMap pathToSchemas;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public OneOfValidator(List<Class<? extends JsonSchema>> oneOf) {
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
@Nullable List<PathToSchemasMap> containsPathToSchemas,
@Nullable PathToSchemasMap patternPropertiesPathToSchemas
) {
PathToSchemasMap pathToSchemas = new PathToSchemasMap();
List<Class<? extends JsonSchema>> validatedOneOfClasses = new ArrayList<>();
Expand Down
Loading