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

Java, adds dependentRequired #363

Merged
merged 3 commits into from
Jan 16, 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 @@ -279,7 +279,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Const|✓|OAS3
|Contains|✓|OAS3
|Default|✓|OAS2,OAS3
|DependentRequired||OAS3
|DependentRequired||OAS3
|DependentSchemas|✗|OAS3
|Discriminator|✗|OAS2,OAS3
|Else|✗|OAS3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/ConstValidato
src/main/java/org/openapijsonschematools/client/schemas/validation/ContainsValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/CustomIsoparser.java
src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java
src/main/java/org/openapijsonschematools/client/schemas/validation/DependentRequiredValidator.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/EnumValidator.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openapijsonschematools.client.schemas.validation;

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

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DependentRequiredValidator implements KeywordValidator {
public final Map<String, Set<String>> dependentRequired;

public DependentRequiredValidator(Map<String, Set<String>> dependentRequired) {
this.dependentRequired = dependentRequired;
}

@Override
public @Nullable PathToSchemasMap validate(
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
) {
if (!(arg instanceof Map)) {
return null;
}
for (Map.Entry<String, Set<String>> entry: dependentRequired.entrySet()) {
if (!((Map<?, ?>) arg).containsKey(entry.getKey())) {
continue;
}
Set<String> missingKeys = new HashSet<>(entry.getValue());
for (Object objKey: ((Map<?, ?>) arg).keySet()) {
if (objKey instanceof String key) {
missingKeys.remove(key);
}
}
if (missingKeys.isEmpty()) {
continue;
}
throw new ValidationException(
"Validation failed for dependentRequired because these_keys="+missingKeys+" are "+
"missing at pathToItem="+validationMetadata.pathToItem()+" in class "+schema.getClass()
);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class JsonSchema {
public final @Nullable Integer maxContains;
public final @Nullable Integer minContains;
public final @Nullable Class<? extends JsonSchema> propertyNames;
public @Nullable Map<String, Set<String>> dependentRequired;
private final LinkedHashMap<String, KeywordValidator> keywordToValidator;

protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
Expand Down Expand Up @@ -260,6 +261,13 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
new PropertyNamesValidator(this.propertyNames)
);
}
this.dependentRequired = jsonSchemaInfo.dependentRequired;
if (this.dependentRequired != null) {
keywordToValidator.put(
"dependentRequired",
new DependentRequiredValidator(this.dependentRequired)
);
}
this.keywordToValidator = keywordToValidator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ public JsonSchemaInfo propertyNames(Class<? extends JsonSchema> propertyNames) {
this.propertyNames = propertyNames;
return this;
}
public @Nullable Map<String, Set<String>> dependentRequired = null;
public JsonSchemaInfo dependentRequired(Map<String, Set<String>> dependentRequired) {
this.dependentRequired = dependentRequired;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/ConstValidato
src/main/java/org/openapijsonschematools/client/schemas/validation/ContainsValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/CustomIsoparser.java
src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java
src/main/java/org/openapijsonschematools/client/schemas/validation/DependentRequiredValidator.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/EnumValidator.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ A schema class that validates payloads
### Field Summary
| Modifier and Type | Field and Description |
| ----------------- | ---------------------- |
| Map<String, Set<String>> | dependentRequired = MapUtils.makeMap(<br>
&nbsp;&nbsp;&nbsp;&nbsp;new AbstractMap.SimpleEntry<>(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"bar",<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetMaker.makeSet(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)
&nbsp;&nbsp;&nbsp;&nbsp;)<br>
)
|

### Method Summary
| Modifier and Type | Method and Description |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapijsonschematools.client.components.schemas;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand All @@ -17,6 +18,7 @@
import org.openapijsonschematools.client.exceptions.InvalidTypeException;
import org.openapijsonschematools.client.exceptions.UnsetPropertyException;
import org.openapijsonschematools.client.exceptions.ValidationException;
import org.openapijsonschematools.client.schemas.SetMaker;
import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter;
import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator;
import org.openapijsonschematools.client.schemas.validation.FrozenList;
Expand All @@ -25,6 +27,7 @@
import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo;
import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator;
import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator;
import org.openapijsonschematools.client.schemas.validation.MapUtils;
import org.openapijsonschematools.client.schemas.validation.NullSchemaValidator;
import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator;
import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap;
Expand All @@ -46,6 +49,13 @@ public static class EmptyDependents1 extends JsonSchema implements NullSchemaVal

protected EmptyDependents1() {
super(new JsonSchemaInfo()
.dependentRequired(MapUtils.makeMap(
new AbstractMap.SimpleEntry<>(
"bar",
SetMaker.makeSet(
)
)
))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openapijsonschematools.client.schemas.validation;

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

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DependentRequiredValidator implements KeywordValidator {
public final Map<String, Set<String>> dependentRequired;

public DependentRequiredValidator(Map<String, Set<String>> dependentRequired) {
this.dependentRequired = dependentRequired;
}

@Override
public @Nullable PathToSchemasMap validate(
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
) {
if (!(arg instanceof Map)) {
return null;
}
for (Map.Entry<String, Set<String>> entry: dependentRequired.entrySet()) {
if (!((Map<?, ?>) arg).containsKey(entry.getKey())) {
continue;
}
Set<String> missingKeys = new HashSet<>(entry.getValue());
for (Object objKey: ((Map<?, ?>) arg).keySet()) {
if (objKey instanceof String key) {
missingKeys.remove(key);
}
}
if (missingKeys.isEmpty()) {
continue;
}
throw new ValidationException(
"Validation failed for dependentRequired because these_keys="+missingKeys+" are "+
"missing at pathToItem="+validationMetadata.pathToItem()+" in class "+schema.getClass()
);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class JsonSchema {
public final @Nullable Integer maxContains;
public final @Nullable Integer minContains;
public final @Nullable Class<? extends JsonSchema> propertyNames;
public @Nullable Map<String, Set<String>> dependentRequired;
private final LinkedHashMap<String, KeywordValidator> keywordToValidator;

protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
Expand Down Expand Up @@ -260,6 +261,13 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
new PropertyNamesValidator(this.propertyNames)
);
}
this.dependentRequired = jsonSchemaInfo.dependentRequired;
if (this.dependentRequired != null) {
keywordToValidator.put(
"dependentRequired",
new DependentRequiredValidator(this.dependentRequired)
);
}
this.keywordToValidator = keywordToValidator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ public JsonSchemaInfo propertyNames(Class<? extends JsonSchema> propertyNames) {
this.propertyNames = propertyNames;
return this;
}
public @Nullable Map<String, Set<String>> dependentRequired = null;
public JsonSchemaInfo dependentRequired(Map<String, Set<String>> dependentRequired) {
this.dependentRequired = dependentRequired;
return this;
}
}
1 change: 1 addition & 0 deletions samples/client/petstore/java/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/ConstValidato
src/main/java/org/openapijsonschematools/client/schemas/validation/ContainsValidator.java
src/main/java/org/openapijsonschematools/client/schemas/validation/CustomIsoparser.java
src/main/java/org/openapijsonschematools/client/schemas/validation/DefaultValueMethod.java
src/main/java/org/openapijsonschematools/client/schemas/validation/DependentRequiredValidator.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/EnumValidator.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openapijsonschematools.client.schemas.validation;

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

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DependentRequiredValidator implements KeywordValidator {
public final Map<String, Set<String>> dependentRequired;

public DependentRequiredValidator(Map<String, Set<String>> dependentRequired) {
this.dependentRequired = dependentRequired;
}

@Override
public @Nullable PathToSchemasMap validate(
JsonSchema schema,
@Nullable Object arg,
ValidationMetadata validationMetadata,
@Nullable List<PathToSchemasMap> containsPathToSchemas
) {
if (!(arg instanceof Map)) {
return null;
}
for (Map.Entry<String, Set<String>> entry: dependentRequired.entrySet()) {
if (!((Map<?, ?>) arg).containsKey(entry.getKey())) {
continue;
}
Set<String> missingKeys = new HashSet<>(entry.getValue());
for (Object objKey: ((Map<?, ?>) arg).keySet()) {
if (objKey instanceof String key) {
missingKeys.remove(key);
}
}
if (missingKeys.isEmpty()) {
continue;
}
throw new ValidationException(
"Validation failed for dependentRequired because these_keys="+missingKeys+" are "+
"missing at pathToItem="+validationMetadata.pathToItem()+" in class "+schema.getClass()
);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class JsonSchema {
public final @Nullable Integer maxContains;
public final @Nullable Integer minContains;
public final @Nullable Class<? extends JsonSchema> propertyNames;
public @Nullable Map<String, Set<String>> dependentRequired;
private final LinkedHashMap<String, KeywordValidator> keywordToValidator;

protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
Expand Down Expand Up @@ -260,6 +261,13 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) {
new PropertyNamesValidator(this.propertyNames)
);
}
this.dependentRequired = jsonSchemaInfo.dependentRequired;
if (this.dependentRequired != null) {
keywordToValidator.put(
"dependentRequired",
new DependentRequiredValidator(this.dependentRequired)
);
}
this.keywordToValidator = keywordToValidator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ public JsonSchemaInfo propertyNames(Class<? extends JsonSchema> propertyNames) {
this.propertyNames = propertyNames;
return this;
}
public @Nullable Map<String, Set<String>> dependentRequired = null;
public JsonSchemaInfo dependentRequired(Map<String, Set<String>> dependentRequired) {
this.dependentRequired = dependentRequired;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public JavaClientGenerator() {
SchemaFeature.Const,
SchemaFeature.Contains,
SchemaFeature.Default,
// SchemaFeature.DependentRequired,
SchemaFeature.DependentRequired,
// SchemaFeature.DependentSchemas,
// SchemaFeature.Discriminator,
// SchemaFeature.Else,
Expand Down Expand Up @@ -567,6 +567,7 @@ public void processOpts() {
keywordValidatorFiles.add("BigDecimalValidator");
keywordValidatorFiles.add("CustomIsoparser");
keywordValidatorFiles.add("DefaultValueMethod");
keywordValidatorFiles.add("DependentRequiredValidator");
keywordValidatorFiles.add("DoubleEnumValidator");
keywordValidatorFiles.add("DoubleValueMethod");
keywordValidatorFiles.add("EnumValidator");
Expand Down Expand Up @@ -1347,6 +1348,7 @@ public Set<String> getImports(String sourceJsonPath, CodegenSchema schema, Featu
addMultipleOfValidator(schema, imports);
addAdditionalPropertiesImports(schema, imports);
addDefaultValueImport(schema, imports);
addDependentRequiredImports(schema, imports);
if (schema.mapValueSchema != null) {
imports.addAll(getDeeperImports(sourceJsonPath, schema.mapValueSchema));
}
Expand Down Expand Up @@ -1460,6 +1462,14 @@ private void addPropertiesValidator(CodegenSchema schema, Set<String> imports) {
}
}

private void addDependentRequiredImports(CodegenSchema schema, Set<String> imports) {
if (schema.dependentRequired != null) {
imports.add("import "+packageName + ".schemas.validation.MapUtils;");
imports.add("import java.util.AbstractMap;");
imports.add("import "+packageName + ".schemas.SetMaker;");
}
}

private void addAllOfValidator(CodegenSchema schema, Set<String> imports) {
if (schema.allOf != null) {
imports.add("import java.util.List;");
Expand Down Expand Up @@ -1554,6 +1564,7 @@ private void addMapSchemaImports(Set<String> imports, CodegenSchema schema) {
addAnyOfValidator(schema, imports);
addOneOfValidator(schema, imports);
addAdditionalPropertiesImports(schema, imports);
addDependentRequiredImports(schema, imports);
}

private void addListSchemaImports(Set<String> imports, CodegenSchema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements {
{{#if propertyNames}}
{{> src/main/java/packagename/components/schemas/SchemaClass/_propertyNames }}
{{/if}}
{{#if dependentRequired}}
{{> src/main/java/packagename/components/schemas/SchemaClass/_dependentRequired }}
{{/if}}
);
}

Expand All @@ -135,9 +138,6 @@ public static class {{jsonPathPiece.pascalCase}} extends JsonSchema implements {
{{#if else_}}
{{!> components/schemas/schema_cls/_else }}
{{/if}}
{{#if dependentRequired}}
{{!> components/schemas/schema_cls/_dependent_required }}
{{/if}}
{{#if dependentSchemas}}
{{!> components/schemas/schema_cls/_dependent_schemas }}
{{/if}}
Expand Down
Loading