Skip to content

Commit

Permalink
remove scope from AbstractJsonSchemaElement
Browse files Browse the repository at this point in the history
  • Loading branch information
redmitry committed Aug 30, 2024
1 parent ce5c2fd commit 4701671
Show file tree
Hide file tree
Showing 27 changed files with 126 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public AbstractJsonSchema parse(JsonSchemaLocator locator, AbstractJsonSchemaEle

if (value.getValueType() == ValueType.TRUE ||
value.getValueType() == ValueType.FALSE) {
return new BooleanJsonSchemaImpl(parent, locator, locator, jsonPointer).read(this, value);
return new BooleanJsonSchemaImpl(parent, locator, jsonPointer).read(this, value);
}

if (value.getValueType() != ValueType.OBJECT) {
Expand All @@ -106,11 +106,29 @@ public AbstractJsonSchema parse(JsonSchemaLocator locator, AbstractJsonSchemaEle

// before draft 2019-09 $ref ignored any other properties
if (JsonSchemaVersion.SCHEMA_DRAFT_2019_09.compareTo(getJsonSchemaVersion(locator)) > 0) {
return new JsonReferenceImpl(parent, locator, locator, jsonPointer).read(this, object);
return new JsonReferenceImpl(parent, locator, jsonPointer).read(this, object);
}
}

final JsonSchemaLocator scope = getScope(locator, object);
JsonValue $id = object.get(JsonSchema.ID);
if ($id == null) {
$id = object.get("id"); // draft4
}

if ($id != null) {
if ($id.getValueType() != JsonValue.ValueType.STRING) {
throw new JsonSchemaException(new ParsingError(ParsingMessage.INVALID_ATTRIBUTE_TYPE,
"id", $id.getValueType().name(), JsonValue.ValueType.STRING.name()));
} else {
final String id = ((JsonString)$id).getString();
try {
locator = locator.resolve(URI.create(id));
locator.setSchema(object);
} catch(IllegalArgumentException ex) {
throw new JsonSchemaException(new ParsingError(ParsingMessage.INVALID_REFERENCE, id));
}
}
}

final JsonValue type_value = object.get(TYPE);
final ValueType vtype;
Expand Down Expand Up @@ -140,29 +158,29 @@ public AbstractJsonSchema parse(JsonSchemaLocator locator, AbstractJsonSchemaEle
if (jenum.isEmpty()) {
throw new JsonSchemaException(new ParsingError(ParsingMessage.EMPTY_ENUM));
}
return new JsonEnumImpl(parent, scope, locator, jsonPointer).read(this, object);
return new JsonEnumImpl(parent, locator, jsonPointer).read(this, object);
}

final JsonValue jconst = object.get(CONST);
if (jconst != null) {
return new JsonConstImpl(parent, scope, locator, jsonPointer).read(this, object);
return new JsonConstImpl(parent, locator, jsonPointer).read(this, object);
}

if (type == null) {
return new JsonMultitypeSchemaWrapper(parent, scope, locator, jsonPointer,
return new JsonMultitypeSchemaWrapper(parent, locator, jsonPointer,
vtype == ValueType.ARRAY ? type_value.asJsonArray() : null)
.read(this, object);
}

final AbstractJsonSchema schema;
switch(type) {
case OBJECT: schema = new JsonObjectSchemaImpl(parent, scope, locator, jsonPointer); break;
case ARRAY: schema = new JsonArraySchemaImpl(parent, scope, locator, jsonPointer); break;
case STRING: schema = new JsonStringSchemaImpl(parent, scope, locator, jsonPointer); break;
case NUMBER: schema = new JsonNumberSchemaImpl(parent, scope, locator, jsonPointer); break;
case INTEGER: schema = new JsonIntegerSchemaImpl(parent, scope, locator, jsonPointer); break;
case BOOLEAN: schema = new JsonBooleanSchemaImpl(parent, scope, locator, jsonPointer); break;
case NULL: schema = new JsonNullSchemaImpl(parent, scope, locator, jsonPointer); break;
case OBJECT: schema = new JsonObjectSchemaImpl(parent, locator, jsonPointer); break;
case ARRAY: schema = new JsonArraySchemaImpl(parent, locator, jsonPointer); break;
case STRING: schema = new JsonStringSchemaImpl(parent, locator, jsonPointer); break;
case NUMBER: schema = new JsonNumberSchemaImpl(parent, locator, jsonPointer); break;
case INTEGER: schema = new JsonIntegerSchemaImpl(parent, locator, jsonPointer); break;
case BOOLEAN: schema = new JsonBooleanSchemaImpl(parent, locator, jsonPointer); break;
case NULL: schema = new JsonNullSchemaImpl(parent, locator, jsonPointer); break;
default: return null;
}

Expand Down Expand Up @@ -198,30 +216,4 @@ public JsonSchemaVersion getJsonSchemaVersion(JsonSchemaLocator locator) {

return JsonSchemaVersion.SCHEMA_DRAFT_07; // default
}

private JsonSchemaLocator getScope(JsonSchemaLocator locator, JsonObject object)
throws JsonSchemaException {

JsonValue $id = object.get(JsonSchema.ID);
if ($id == null) {
$id = object.get("id"); // draft4
}

if ($id != null) {
if ($id.getValueType() != JsonValue.ValueType.STRING) {
throw new JsonSchemaException(new ParsingError(ParsingMessage.INVALID_ATTRIBUTE_TYPE,
"id", $id.getValueType().name(), JsonValue.ValueType.STRING.name()));
} else {
final String id = ((JsonString)$id).getString();
try {
locator = locator.resolve(URI.create(id));
locator.setSchema(object);
} catch(IllegalArgumentException ex) {
throw new JsonSchemaException(new ParsingError(ParsingMessage.INVALID_REFERENCE, id));
}
}
}

return locator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public abstract class AbstractJsonReferenceImpl extends AbstractJsonSchema<JsonO
protected JsonSubschemaParser parser;

public AbstractJsonReferenceImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

protected void read(JsonSubschemaParser parser, JsonObject object, String tag)
Expand All @@ -71,21 +71,21 @@ protected void read(JsonSubschemaParser parser, JsonObject object, String tag)
final String fragment = ref.getFragment();
if (fragment == null) {
ref_pointer = "/";
ref_locator = scope.resolve(ref);
ref_locator = locator.resolve(ref);
} else if ("#".equals(jref)) {
ref_pointer = "/";
ref_locator = scope;
ref_locator = locator;
} else if (fragment.startsWith("/")) {
ref_pointer = fragment;
if (jref.startsWith("#")) {
ref_locator = scope;
ref_locator = locator;
} else {
ref_locator = scope.resolve(
ref_locator = locator.resolve(
new URI(ref.getScheme(), ref.getSchemeSpecificPart(), null));
}
} else {
ref_pointer = "/";
ref_locator = scope.resolve(ref);
ref_locator = locator.resolve(ref);
}
} catch(JsonException | IllegalArgumentException | URISyntaxException ex) {
throw new JsonSchemaException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public abstract class AbstractJsonSchema<T extends JsonValue>
extends AbstractJsonSchemaElement implements JsonSchema {

public AbstractJsonSchema(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

public AbstractJsonSchema<T> read(JsonSubschemaParser parser, T value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public abstract class AbstractJsonSchemaElement

private AbstractJsonSchemaElement parent;

public final JsonSchemaLocator scope;
public final JsonSchemaLocator locator;
public final String jsonPointer;

Expand All @@ -55,29 +54,26 @@ public abstract class AbstractJsonSchemaElement
* the JSON Schema.
*
* @param parent a parent element that encloses this one
* @param scope current element scope (may or may not be equal to the location)
* @param locator the locator that was used to load this document
* @param locator current element scope (may or may not be equal to the location)
* @param jsonPointer JSON Pointer to the parsed JSON Value that represents this element.
*/
public AbstractJsonSchemaElement(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
JsonSchemaLocator locator, String jsonPointer) {

this.parent = parent;

this.scope = scope;
this.locator = locator;
this.jsonPointer = jsonPointer.startsWith("//") ? jsonPointer.substring(1) : jsonPointer;
}

@Override
public final URI getId() {
return scope.uri;
return locator.uri;
}

@Override
public String getJsonPointer() {
// when scope != locator (new scope) jsonPointer is 'root'
return scope == locator ? jsonPointer : "/";
return parent == null || parent.locator != locator ? "/" : jsonPointer;
}

@Override
Expand Down Expand Up @@ -108,15 +104,15 @@ public boolean equals(Object obj) {
if (obj instanceof AbstractJsonSchemaElement other &&
this.getClass() == obj.getClass()) {
return Objects.equals(jsonPointer, other.jsonPointer) &&
Objects.equals(scope.uri, other.scope.uri);
Objects.equals(locator.uri, other.locator.uri);
}
return false;
}

@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + Objects.hashCode(this.scope.uri);
hash = 31 * hash + Objects.hashCode(this.locator.uri);
hash = 31 * hash + Objects.hashCode(this.jsonPointer);
hash = 31 * hash + Objects.hashCode(this.getClass().hashCode());
return hash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class BooleanJsonSchemaImpl extends AbstractJsonSchema<JsonValue>
private boolean evaluation;

public BooleanJsonSchemaImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class JsonAllOfImpl extends SchemaArrayImpl<JsonValue>
implements JsonAllOf<AbstractJsonSchema> {

public JsonAllOfImpl(AbstractJsonSchema parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class JsonAnyOfImpl<T extends JsonValue> extends SchemaArrayImpl<T>
implements JsonAnyOf<AbstractJsonSchema> {

public JsonAnyOfImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public class JsonArraySchemaImpl extends PrimitiveSchemaImpl
private Long maxContains;

public JsonArraySchemaImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down Expand Up @@ -178,7 +178,7 @@ public JsonArraySchemaImpl read(JsonSubschemaParser parser, JsonObject object)

final JsonValue jcontains = object.get(CONTAINS);
if (jcontains != null) {
contains = parser.parse(scope, this, getJsonPointer() + "/" + CONTAINS, jcontains, null);
contains = parser.parse(locator, this, getJsonPointer() + "/" + CONTAINS, jcontains, null);
}

final JsonNumber jminContains = JsonSchemaUtil.check(object.getJsonNumber(MIN_CONTAINS), JsonValue.ValueType.NUMBER);
Expand All @@ -196,7 +196,7 @@ public JsonArraySchemaImpl read(JsonSubschemaParser parser, JsonObject object)
switch(jitems.getValueType()) {
case OBJECT:
case TRUE:
case FALSE: final AbstractJsonSchema schema = parser.parse(scope, this, getJsonPointer() + "/" + ITEMS, jitems, null);
case FALSE: final AbstractJsonSchema schema = parser.parse(locator, this, getJsonPointer() + "/" + ITEMS, jitems, null);
getItems().add(schema);
break;
case ARRAY: additionalItems = true;
Expand All @@ -205,7 +205,7 @@ public JsonArraySchemaImpl read(JsonSubschemaParser parser, JsonObject object)
switch(value.getValueType()) {
case OBJECT:
case TRUE:
case FALSE: final AbstractJsonSchema arr = parser.parse(scope, this, getJsonPointer() + "/" + ITEMS + "/" + i, value, null);
case FALSE: final AbstractJsonSchema arr = parser.parse(locator, this, getJsonPointer() + "/" + ITEMS + "/" + i, value, null);
getItems().add(arr);
break;
default: throw new JsonSchemaException(new ParsingError(ParsingMessage.INVALID_ATTRIBUTE_TYPE,
Expand All @@ -229,15 +229,15 @@ public JsonArraySchemaImpl read(JsonSubschemaParser parser, JsonObject object)
default: throw new JsonSchemaException(new ParsingError(ParsingMessage.INVALID_ATTRIBUTE_TYPE,
ADDITIONAL_ITEMS, jadditionalItems.getValueType().name(), "either object or boolean"));
}
additionalItemsSchema = parser.parse(scope, this, getJsonPointer() + "/" + ADDITIONAL_ITEMS, jadditionalItems, null);
additionalItemsSchema = parser.parse(locator, this, getJsonPointer() + "/" + ADDITIONAL_ITEMS, jadditionalItems, null);
}
}

final JsonValue junevaluatedItems = object.get(UNEVALUATED_ITEMS);
if (junevaluatedItems != null) {
switch(junevaluatedItems.getValueType()) {
case OBJECT: unevaluatedItems = null;
unevaluatedItemsSchema = parser.parse(scope, this, getJsonPointer() + "/" + UNEVALUATED_ITEMS, junevaluatedItems, null);
unevaluatedItemsSchema = parser.parse(locator, this, getJsonPointer() + "/" + UNEVALUATED_ITEMS, junevaluatedItems, null);
break;
case TRUE: unevaluatedItems = true; break;
case FALSE: unevaluatedItems = false; break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class JsonBooleanSchemaImpl extends PrimitiveSchemaImpl
implements JsonBooleanSchema {

public JsonBooleanSchemaImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class JsonConstImpl extends PrimitiveSchemaImpl implements JsonConst {
private JsonValue value;

public JsonConstImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class JsonDependentPropertiesImpl extends AbstractJsonSchemaElement
private final Map<String, StringArray> properties = new LinkedHashMap();

public JsonDependentPropertiesImpl(AbstractJsonSchema parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class JsonDynamicReferenceImpl extends JsonReferenceImpl
implements JsonDynamicReference {

public JsonDynamicReferenceImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);

AbstractJsonSchemaElement e = this;
do {
Expand Down Expand Up @@ -97,7 +97,7 @@ public AbstractJsonSchemaElement getSchema() throws JsonSchemaException {
private AbstractJsonSchemaElement getSchema(AbstractJsonSchemaElement e, URI uri)
throws IOException, JsonSchemaException {
final String fragment = uri.getFragment();
final JsonSchemaLocator l = e.scope.resolve(uri);
final JsonSchemaLocator l = e.locator.resolve(uri);
final JsonValue value = l.getSchema("/");
if (value instanceof JsonObject jsubschema) {
final String anchor = jsubschema.getString(DYNAMIC_ANCHOR, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class JsonEnumImpl extends PrimitiveSchemaImpl implements JsonEnum {
private List<JsonValue> values;

public JsonEnumImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class JsonIntegerSchemaImpl extends NumericSchemaImpl<BigInteger>
implements JsonIntegerSchema {

public JsonIntegerSchemaImpl(AbstractJsonSchemaElement parent,
JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) {
super(parent, scope, locator, jsonPointer);
JsonSchemaLocator locator, String jsonPointer) {
super(parent, locator, jsonPointer);
}

@Override
Expand Down
Loading

0 comments on commit 4701671

Please sign in to comment.