Skip to content
Open
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
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
@@ -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;
@@ -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.
*
@@ -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;
@@ -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) {
@@ -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;
}
@@ -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) &&
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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");
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
@@ -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);
@@ -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());