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

definition schemas #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions core/src/main/java/org/everit/json/schema/ObjectSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ private static final Regexp toRegexp(String pattern) {

private boolean requiresObject = true;

private final Map<String, Schema> definitionSchemas = new HashMap<>();

private final Map<String, Schema> propertySchemas = new HashMap<>();

private boolean additionalProperties = true;
Expand All @@ -64,6 +66,22 @@ public Builder additionalProperties(boolean additionalProperties) {
return this;
}

/**
* Adds a definition schema.
*
* @param definitionName
* the name of the definition
* @param schema
* the definition of the {@code schema}
* @return {@code this}
*/
public Builder addDefinitionSchema(String definitionName, Schema schema) {
requireNonNull(definitionName, "definitionName cannot be null");
requireNonNull(schema, "schema cannot be null");
definitionSchemas.put(definitionName, schema);
return this;
}

/**
* Adds a property schema.
*
Expand Down Expand Up @@ -170,6 +188,8 @@ private static <K, V> Map<K, V> copyMap(Map<K, V> original) {
return Collections.unmodifiableMap(new HashMap<>(original));
}

private final Map<String, Schema> definitionSchemas;

private final Map<String, Schema> propertySchemas;

private final boolean additionalProperties;
Expand Down Expand Up @@ -202,8 +222,10 @@ private static <K, V> Map<K, V> copyMap(Map<K, V> original) {
*/
public ObjectSchema(Builder builder) {
super(builder);
this.definitionSchemas = builder.definitionSchemas == null ? null
: Collections.unmodifiableMap(builder.definitionSchemas);
this.propertySchemas = builder.propertySchemas == null ? null
: Collections.unmodifiableMap(builder.propertySchemas);
: Collections.unmodifiableMap(builder.propertySchemas);
this.additionalProperties = builder.additionalProperties;
this.schemaOfAdditionalProperties = builder.schemaOfAdditionalProperties;
if (!additionalProperties && schemaOfAdditionalProperties != null) {
Expand Down Expand Up @@ -248,6 +270,10 @@ public Map<String, Set<String>> getPropertyDependencies() {
return propertyDependencies;
}

public Map<String, Schema> getDefinitionSchemas() {
return definitionSchemas;
}

public Map<String, Schema> getPropertySchemas() {
return propertySchemas;
}
Expand Down Expand Up @@ -344,6 +370,7 @@ public boolean equals(Object o) {
return that.canEqual(this) &&
additionalProperties == that.additionalProperties &&
requiresObject == that.requiresObject &&
Objects.equals(definitionSchemas, that.definitionSchemas) &&
Objects.equals(propertySchemas, that.propertySchemas) &&
Objects.equals(schemaOfAdditionalProperties, that.schemaOfAdditionalProperties) &&
Objects.equals(requiredProperties, that.requiredProperties) &&
Expand All @@ -362,7 +389,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), propertySchemas, propertyNameSchema, additionalProperties,
return Objects.hash(super.hashCode(), definitionSchemas, propertySchemas, propertyNameSchema, additionalProperties,
schemaOfAdditionalProperties, requiredProperties, minProperties, maxProperties, propertyDependencies,
schemaDependencies, requiresObject, patternProperties, oneOrMoreDefaultProperty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ private void describePropertyDependencies(Map<String, Set<String>> propertyDepen
visit(propertyNameSchema);
}

@Override void visitDefinitionSchemas(Map<String, Schema> definitionSchemas) {
if (!definitionSchemas.isEmpty()) {
writer.key("definitions");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to use $defs since that is the currently recommended keyword according to the spec?

printSchemaMap(definitionSchemas);
}
}

@Override void visitPropertySchemas(Map<String, Schema> propertySchemas) {
if (!propertySchemas.isEmpty()) {
writer.key("properties");
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/org/everit/json/schema/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ void visitObjectSchema(ObjectSchema objectSchema) {
for (Map.Entry<String, Schema> schemaDep : objectSchema.getSchemaDependencies().entrySet()) {
visitSchemaDependency(schemaDep.getKey(), schemaDep.getValue());
}
Map<String, Schema> definitionSchemas = objectSchema.getDefinitionSchemas();
if (definitionSchemas != null) {
visitDefinitionSchemas(definitionSchemas);
}
Map<String, Schema> propertySchemas = objectSchema.getPropertySchemas();
if (propertySchemas != null) {
visitPropertySchemas(propertySchemas);
Expand All @@ -177,6 +181,12 @@ void visitPatternProperties(Map<Regexp, Schema> patternProperties) {
}
}

void visitDefinitionSchemas(Map<String, Schema> definitionSchemas) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this makes sense? It may trigger unnecessary validation. For example if the schema is

{
   "type": "object",
   "properties": {
      "A": { "type": "string" }
   },
   "required": ["A"']
   "$defs": {
      "A": { "type": "integer" }
   }
}

Then I suspect no instance would pass the validation (the "A" prop would be validated against both {"type":"string"} and { "type": "integer" }).

for (Map.Entry<String, Schema> entry : definitionSchemas.entrySet()) {
visitPropertySchema(entry.getKey(), entry.getValue());
}
}

void visitPropertySchemas(Map<String, Schema> propertySchemas) {
for (Map.Entry<String, Schema> entry : propertySchemas.entrySet()) {
visitPropertySchema(entry.getKey(), entry.getValue());
Expand Down