Skip to content

Commit

Permalink
fix issue with refs in other keywords (#137)
Browse files Browse the repository at this point in the history
* fix issue with refs in other keywords
  • Loading branch information
sebastian-toepfer authored Jun 12, 2024
1 parent d81242a commit 1001a5a
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@

import io.github.sebastiantoepfer.ddd.common.Printable;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;

public interface JsonSchema extends JsonValue, Printable {
Validator validator();

Optional<Keyword> keywordByName(String name);

Optional<JsonSubSchema> asSubSchema(String name);
Optional<JsonSubSchema> subSchema(String name);

Stream<JsonSubSchema> subSchemas(String name);

Optional<JsonSubSchema> subSchema(JsonPointer pointer);

default JsonSchema rootSchema() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.spi.JsonSchemaFactory;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;

public final class FakeJsonSchemaFactory implements JsonSchemaFactory {

Expand All @@ -54,7 +56,17 @@ public JsonValue.ValueType getValueType() {
}

@Override
public Optional<JsonSubSchema> asSubSchema(String name) {
public Optional<JsonSubSchema> subSchema(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Stream<JsonSubSchema> subSchemas(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<JsonSubSchema> subSchema(JsonPointer pointer) {
throw new UnsupportedOperationException("Not supported yet.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;

class JsonSubSchemaTest {
Expand Down Expand Up @@ -61,7 +63,17 @@ public JsonValue.ValueType getValueType() {
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
public Optional<JsonSubSchema> subSchema(final String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Stream<JsonSubSchema> subSchemas(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<JsonSubSchema> subSchema(JsonPointer pointer) {
throw new UnsupportedOperationException("Not supported yet.");
}

Expand Down
2 changes: 0 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@
<!-- how to validate?
<include>**/tests/draft2020-12/id.json</include>
-->
<!-- more than items keyword needed :(
<include>**/tests/draft2020-12/items.json</include>
-->
<include>**/tests/draft2020-12/maxContains.json</include>
<include>**/tests/draft2020-12/maxItems.json</include>
<include>**/tests/draft2020-12/maxLength.json</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonObject;
import jakarta.json.JsonPointer;
import java.util.Optional;
import java.util.stream.Stream;

Expand Down Expand Up @@ -61,9 +62,32 @@ private Stream<Keyword> keywords() {
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
public Optional<JsonSubSchema> subSchema(final String name) {
return Optional.ofNullable(asJsonObject().get(name))
.flatMap(new DefaultJsonSchemaFactory()::tryToCreateSchemaFrom)
.map(subSchema -> new DefaultJsonSubSchema(this, subSchema));
}

@Override
public Stream<JsonSubSchema> subSchemas(final String name) {
return asJsonObject()
.getJsonArray(name)
.stream()
.map(new DefaultJsonSchemaFactory()::tryToCreateSchemaFrom)
.flatMap(Optional::stream)
.map(subSchema -> new DefaultJsonSubSchema(this, subSchema));
}

@Override
public Optional<JsonSubSchema> subSchema(final JsonPointer pointer) {
final Optional<JsonSubSchema> result;
if (pointer.containsValue(asJsonObject())) {
result = new DefaultJsonSchemaFactory()
.tryToCreateSchemaFrom(pointer.getValue(asJsonObject()))
.map(subSchema -> new DefaultJsonSubSchema(this, subSchema));
} else {
result = Optional.empty();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonObject;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -92,10 +93,18 @@ private boolean isJsonObject() {
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
return Optional.ofNullable(asJsonObject().get(name))
.flatMap(new DefaultJsonSchemaFactory()::tryToCreateSchemaFrom)
.map(subSchema -> new DefaultJsonSubSchema(this, subSchema));
public Optional<JsonSubSchema> subSchema(final String name) {
return schema.subSchema(name).map(sub -> new DefaultJsonSubSchema(this, sub));
}

@Override
public Stream<JsonSubSchema> subSchemas(final String name) {
return schema.subSchemas(name).map(sub -> new DefaultJsonSubSchema(this, sub));
}

@Override
public Optional<JsonSubSchema> subSchema(final JsonPointer pointer) {
return schema.subSchema(pointer).map(sub -> new DefaultJsonSubSchema(this, sub));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;

final class EmptyJsonSchema extends AbstractJsonValueSchema {

Expand All @@ -48,7 +50,17 @@ public Optional<Keyword> keywordByName(final String name) {
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
public Optional<JsonSubSchema> subSchema(final String name) {
return Optional.empty();
}

@Override
public Stream<JsonSubSchema> subSchemas(final String name) {
return Stream.empty();
}

@Override
public Optional<JsonSubSchema> subSchema(final JsonPointer pointer) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;

final class FalseJsonSchema extends AbstractJsonValueSchema {

Expand All @@ -53,7 +55,17 @@ public Optional<Keyword> keywordByName(final String name) {
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
public Optional<JsonSubSchema> subSchema(final String name) {
return Optional.empty();
}

@Override
public Stream<JsonSubSchema> subSchemas(final String name) {
return Stream.empty();
}

@Override
public Optional<JsonSubSchema> subSchema(final JsonPointer pointer) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;

final class TrueJsonSchema extends AbstractJsonValueSchema {

Expand All @@ -48,7 +50,17 @@ public Optional<Keyword> keywordByName(final String name) {
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
public Optional<JsonSubSchema> subSchema(final String name) {
return Optional.empty();
}

@Override
public Stream<JsonSubSchema> subSchemas(final String name) {
return Stream.empty();
}

@Override
public Optional<JsonSubSchema> subSchema(final JsonPointer pointer) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
final JsonSubSchema pseudoSchema = schema.asSubSchema(name).orElseThrow(IllegalArgumentException::new);
final JsonSubSchema pseudoSchema = schema.subSchema(name).orElseThrow(IllegalArgumentException::new);
return pseudoSchema
.asJsonObject()
.keySet()
.stream()
.map(n -> Map.entry(n, pseudoSchema.asSubSchema(n).orElseThrow(IllegalArgumentException::new)))
.map(n -> Map.entry(n, pseudoSchema.subSchema(n).orElseThrow(IllegalArgumentException::new)))
.collect(
collectingAndThen(
toMap(Map.Entry::getKey, Map.Entry::getValue),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static java.util.stream.Collectors.toList;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import java.util.List;
Expand All @@ -51,11 +50,6 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
return schema
.asJsonObject()
.getJsonArray(name)
.stream()
.map(new DefaultJsonSchemaFactory()::create)
.collect(collectingAndThen(toList(), keywordCreator));
return schema.subSchemas(name).collect(collectingAndThen(toList(), keywordCreator));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
return schema.asSubSchema(name()).map(keywordCreator).orElseThrow(IllegalArgumentException::new);
return schema.subSchema(name()).map(keywordCreator).orElseThrow(IllegalArgumentException::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.JsonSchemas;
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
import jakarta.json.Json;
import jakarta.json.JsonPointer;
import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -82,23 +80,14 @@ private JsonSchema retrieveJsonSchema() {
} else {
json = retrieveSchemaFromLocalSchema();
}
return JsonSchemas.load(json);
return json;
} catch (IOException ex) {
throw new IllegalStateException("can not load schema!", ex);
}
}

private JsonSchema retrieveSchemaFromLocalSchema() throws IOException {
final JsonPointer pointer = createPointer();
if (pointer.containsValue(searchAnchor())) {
return JsonSchemas.load(pointer.getValue(searchAnchor()));
} else {
throw new IOException("can not find referenced value.");
}
}

private JsonStructure searchAnchor() {
return schema.rootSchema().asJsonObject();
return schema.rootSchema().subSchema(createPointer()).orElseThrow();
}

private JsonPointer createPointer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;

class AbstractJsonValueSchemaTest {
Expand Down Expand Up @@ -81,7 +83,17 @@ public Optional<Keyword> keywordByName(final String name) {
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
public Optional<JsonSubSchema> subSchema(final String name) {
return Optional.empty();
}

@Override
public Stream<JsonSubSchema> subSchemas(final String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<JsonSubSchema> subSchema(final JsonPointer pointer) {
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void should_return_empty_for_non_existing_keyword() {
@Test
void should_return_empty_if_non_subschema_exists_under_the_given_name() {
assertThat(
new DefaultJsonObjectSchema(Json.createObjectBuilder().add("test", "hallo").build()).asSubSchema(
new DefaultJsonObjectSchema(Json.createObjectBuilder().add("test", "hallo").build()).subSchema(
"properties"
),
isEmpty()
Expand All @@ -124,7 +124,7 @@ void should_return_empty_if_non_subschema_exists_under_the_given_name() {
@Test
void should_return_subschema_if_subschema_exists_under_the_given_name() {
assertThat(
new DefaultJsonObjectSchema(Json.createObjectBuilder().add("test", JsonValue.FALSE).build()).asSubSchema(
new DefaultJsonObjectSchema(Json.createObjectBuilder().add("test", JsonValue.FALSE).build()).subSchema(
"test"
),
isPresent()
Expand All @@ -136,7 +136,7 @@ void should_return_empty_if_given_name_not_resolve_to_a_valid_schematype() {
assertThat(
new DefaultJsonObjectSchema(
Json.createObjectBuilder().add("test", JsonValue.EMPTY_JSON_ARRAY).build()
).asSubSchema("test"),
).subSchema("test"),
isEmpty()
);
}
Expand Down
Loading

0 comments on commit 1001a5a

Please sign in to comment.