org.everit.json.schema.ReferenceSchema Java Examples

The following examples show how to use org.everit.json.schema.ReferenceSchema. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReferenceLookupTest.java    From json-schema with Apache License 2.0 6 votes vote down vote up
@Test
public void multipleReferencesToSameSchema() {
    JSONObject rawSchema = ResourceLoader.DEFAULT.readObj("multi-pointer.json");
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(rawSchema);

    ReferenceSchema aRefSchema = (ReferenceSchema) actual.getPropertySchemas().get("a");
    assertEquals(aRefSchema.getUnprocessedProperties(), ImmutableMap.of("unproc0", "unproc0 of A"));
    assertEquals("A side", aRefSchema.getTitle());
    assertEquals("length of the A side", aRefSchema.getDescription());
    assertEquals(SchemaLocation.parseURI("#/properties/a"), aRefSchema.getLocation());
    assertNotNull(aRefSchema.getReferredSchema());

    ReferenceSchema bRefSchema = (ReferenceSchema) actual.getPropertySchemas().get("b");
    assertEquals(bRefSchema.getUnprocessedProperties(), ImmutableMap.of("unproc0", "unproc0 of B"));
    assertEquals("length of the B side", bRefSchema.getDescription());
    assertEquals(SchemaLocation.parseURI("#/properties/b"), bRefSchema.getLocation());
    assertEquals("B side", bRefSchema.getTitle());
    assertNotNull(bRefSchema.getReferredSchema());

    assertSame(aRefSchema.getReferredSchema(), bRefSchema.getReferredSchema());

}
 
Example #2
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void pointerResolvedToBoolean() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution"));
    TrueSchema boolSchema = (TrueSchema) ((ReferenceSchema) actual.getPropertySchemas()
            .get("boolRef"))
            .getReferredSchema();

    assertNotNull(boolSchema);
}
 
Example #3
Source File: ReferenceLookupTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
private ReferenceSchema obtainReferenceSchema(String pointerToRef) {
    JsonObject jsonValue = query(pointerToRef).requireObject();
    ReferenceLookup subject = new ReferenceLookup(jsonValue.ls);
    String refPointer = jsonValue.require("$ref").requireString();
    Schema.Builder<?> actual = subject.lookup(refPointer, jsonValue);
    return (ReferenceSchema) actual.build();
}
 
Example #4
Source File: RegisteredURIResolutionTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void httpURL() throws URISyntaxException {
    SchemaClient mock = mock(SchemaClient.class);
    SchemaLoader loader = SchemaLoader.builder()
            .schemaClient(mock)
            .schemaJson(LOADER.readObj("ref-example-org.json"))
            .registerSchemaByURI(new URI("http://example.org"), LOADER.readObj("schema-by-urn.json"))
            .build();

    ReferenceSchema actual = (ReferenceSchema) loader.load().build();

    assertEquals("schema-by-urn", actual.getReferredSchema().getTitle());
    verifyNoMoreInteractions(mock);
}
 
Example #5
Source File: RegisteredURIResolutionTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void urnWithFragment() throws URISyntaxException {
    SchemaClient mock = mock(SchemaClient.class);
    SchemaLoader loader = SchemaLoader.builder()
            .schemaClient(mock)
            .schemaJson(LOADER.readObj("ref-urn-fragment.json"))
            .registerSchemaByURI(new URI("urn:uuid:a773c7a2-1a13-4f6a-a70d-694befe0ce63"), LOADER.readObj("schema-by-urn.json"))
            .build();

    ReferenceSchema actual = (ReferenceSchema) loader.load().build();

    assertEquals("subschema-by-urn", actual.getReferredSchema().getTitle());
    verifyNoMoreInteractions(mock);
}
 
Example #6
Source File: RegisteredURIResolutionTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void success() throws URISyntaxException {
    SchemaClient mock = mock(SchemaClient.class);
    SchemaLoader loader = SchemaLoader.builder()
            .schemaClient(mock)
            .schemaJson(LOADER.readObj("ref-urn.json"))
            .registerSchemaByURI(new URI("urn:uuid:a773c7a2-1a13-4f6a-a70d-694befe0ce63"), LOADER.readObj("schema-by-urn.json"))
            .build();

    ReferenceSchema actual = (ReferenceSchema) loader.load().build();

    assertEquals("schema-by-urn", actual.getReferredSchema().getTitle());
    verifyNoMoreInteractions(mock);
}
 
Example #7
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void unprocessedPropertiesAreLoadedForRefElement() {
    SchemaLoader loader =
            SchemaLoader.builder()
                    .draftV7Support()
                    .useDefaults(true)
                    .schemaJson(get("schemaRefWithUnprocessedProperties"))
                    .build();
    ObjectSchema actual = (ObjectSchema) loader.load().build();

    assertEquals(ImmutableMap.of(
            "unproc6", false
    ), actual.getPropertySchemas().get("prop3").getUnprocessedProperties());

    assertEquals(
            ImmutableMap.of("unproc8", false),
            ((ReferenceSchema) actual.getPropertySchemas().get("prop4"))
                    .getReferredSchema()
                    .getUnprocessedProperties());

    assertEquals(
            ImmutableMap.of("unproc4", true, "unproc5", JSONObject.NULL),
            actual.getPropertySchemas().get("prop2").getUnprocessedProperties());

    assertEquals(
            ImmutableMap.of("unproc7", JSONObject.NULL),
            actual.getPropertySchemas().get("prop4").getUnprocessedProperties());

    assertEquals(
            ImmutableMap.of("unproc8", false),
            ((ReferenceSchema) actual.getPropertySchemas().get("prop4")).getReferredSchema().getUnprocessedProperties());

    assertEquals(
            ImmutableMap.of("unproc9", ImmutableMap.of("unproc9-01", false)),
            actual.getPropertySchemas().get("prop5").getUnprocessedProperties());

    assertEquals(
            ImmutableMap.of("unproc8", false),
            ((ReferenceSchema) actual.getPropertySchemas().get("prop5")).getReferredSchema().getUnprocessedProperties());
}
 
Example #8
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void refWithType() {
    ObjectSchema actualRoot = (ObjectSchema) SchemaLoader.load(get("refWithType"));
    ReferenceSchema actual = (ReferenceSchema) actualRoot.getPropertySchemas().get("prop");
    ObjectSchema propSchema = (ObjectSchema) actual.getReferredSchema();
    assertEquals(propSchema.getRequiredProperties(), asList("a", "b"));
}
 
Example #9
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test @Ignore
public void propsAroundRefExtendTheReferredSchema() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader
            .load(get("propsAroundRefExtendTheReferredSchema"));
    ReferenceSchema propRef = (ReferenceSchema) actual.getPropertySchemas().get("prop");
    ObjectSchema prop = (ObjectSchema) propRef
            .getReferredSchema();
    assertTrue(prop.requiresObject());
    assertEquals(1, prop.getMinProperties().intValue());
}
 
Example #10
Source File: ReferenceSchemaDiffVisitor.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
public ReferenceSchemaDiffVisitor(DiffContext ctx, Schema original) {
    this.ctx = ctx;
    if (original instanceof ReferenceSchema) {
        this.referredOriginal = ((ReferenceSchema) original).getReferredSchema();
    } else {
        this.referredOriginal = original;
    }
}
 
Example #11
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void pointerResolution() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution"));
    ObjectSchema rectangleSchema = (ObjectSchema) ((ReferenceSchema) actual.getPropertySchemas()
            .get("rectangle"))
            .getReferredSchema();
    assertNotNull(rectangleSchema);
    ReferenceSchema aRef = (ReferenceSchema) rectangleSchema.getPropertySchemas().get("a");
    assertTrue(aRef.getReferredSchema() instanceof NumberSchema);
}
 
Example #12
Source File: ReferenceLookup.java    From json-schema with Apache License 2.0 5 votes vote down vote up
private Schema.Builder<?> createReferenceSchema(String relPointerString, String absPointerString, JsonValue rawReferenced) {
    ReferenceKnot knot = new ReferenceKnot();
    ReferenceSchema.Builder refBuilder = knot.initReference(relPointerString);
    ls.pointerSchemas.put(absPointerString, knot);
    Schema referredSchema = new SchemaLoader(rawReferenced.ls).load().build();
    knot.resolveWith(referredSchema);
    return refBuilder;
}
 
Example #13
Source File: ReferenceLookup.java    From json-schema with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a schema builder instance after looking up the JSON pointer.
 */
Schema.Builder<?> lookup(String relPointerString, JsonObject ctx) {
    String absPointerString = ReferenceResolver.resolve(ls.id, relPointerString).toString();
    if (ls.pointerSchemas.containsKey(absPointerString)) {
        return ls.pointerSchemas.get(absPointerString).initReference(absPointerString);
    }
    JsonValue rawInternalReferenced = lookupObjById(ls.rootSchemaJson, absPointerString);
    if (rawInternalReferenced != null) {
        return createReferenceSchema(relPointerString, absPointerString, rawInternalReferenced);
    }
    if (isSameDocumentRef(relPointerString)) {
        return performQueryEvaluation(relPointerString, JsonPointerEvaluator.forDocument(ls.rootSchemaJson(), relPointerString));
    }
    JsonPointerEvaluator pointer = createPointerEvaluator(absPointerString);
    ReferenceKnot knot = new ReferenceKnot();
    ReferenceSchema.Builder refBuilder = knot.initReference(relPointerString);
    ls.pointerSchemas.put(absPointerString, knot);
    JsonPointerEvaluator.QueryResult result = pointer.query();

    URI resolutionScope = !isSameDocumentRef(absPointerString) ? withoutFragment(absPointerString) : ls.id;
    JsonObject containingDocument = result.getContainingDocument();
    SchemaLocation resultLocation = result.getQueryResult().ls.pointerToCurrentObj;
    SchemaLoader childLoader = ls.initNewDocumentLoader()
            .resolutionScope(resolutionScope)
            .pointerToCurrentObj(resultLocation)
            .schemaJson(result.getQueryResult())
            .rootSchemaJson(containingDocument).build();
    Schema referredSchema = childLoader.load().build();
    refBuilder.schemaLocation(resultLocation);
    knot.resolveWith(referredSchema);
    return refBuilder;
}
 
Example #14
Source File: ReferenceLookup.java    From json-schema with Apache License 2.0 5 votes vote down vote up
ReferenceSchema.Builder initReference(String refValue) {
    ReferenceSchema.Builder builder = new ReferenceSchema.Builder().refValue(refValue);
    if (referredSchema != null) {
        builder.build().setReferredSchema(referredSchema);
    }
    refs.add(builder);
    return builder;
}
 
Example #15
Source File: ReferenceSchemaDiff.java    From nakadi with MIT License 5 votes vote down vote up
static void recursiveCheck(
        final ReferenceSchema referenceSchemaOriginal, final ReferenceSchema referenceSchemaUpdate,
        final SchemaDiffState state) {
    state.runOnPath("$ref", () -> {
        SchemaDiff.recursiveCheck(referenceSchemaOriginal.getReferredSchema(),
                referenceSchemaUpdate.getReferredSchema(), state);
    });
}
 
Example #16
Source File: WrapUtil.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
public static SchemaWrapper wrap(Schema schema) {
    if (schema == null)
        return null;

    if (schema instanceof ObjectSchema) {
        return new ObjectSchemaWrapper((ObjectSchema) schema);
    } else if (schema instanceof ArraySchema) {
        return new ArraySchemaWrapper((ArraySchema) schema);
    } else if (schema instanceof StringSchema) {
        return new StringSchemaWrapper((StringSchema) schema);
    } else if (schema instanceof EmptySchema && !(schema instanceof TrueSchema)) {
        return new EmptySchemaWrapper((EmptySchema) schema);
    } else if (schema instanceof TrueSchema) {
        return new TrueSchemaWrapper((TrueSchema) schema);
    } else if (schema instanceof FalseSchema) {
        return new FalseSchemaWrapper((FalseSchema) schema);
    } else if (schema instanceof BooleanSchema) {
        return new BooleanSchemaWrapper((BooleanSchema) schema);
    } else if (schema instanceof ConstSchema) {
        return new ConstSchemaWrapper((ConstSchema) schema);
    } else if (schema instanceof EnumSchema) {
        return new EnumSchemaWrapper((EnumSchema) schema);
    } else if (schema instanceof NullSchema) {
        return new NullSchemaWrapper((NullSchema) schema);
    } else if (schema instanceof NotSchema) {
        return new NotSchemaWrapper((NotSchema) schema);
    } else if (schema instanceof ReferenceSchema) {
        return new ReferenceSchemaWrapper((ReferenceSchema) schema);
    } else if (schema instanceof CombinedSchema) {
        return new CombinedSchemaWrapper((CombinedSchema) schema);
    } else if (schema instanceof ConditionalSchema) {
        return new ConditionalSchemaWrapper((ConditionalSchema) schema);
    } else if (schema instanceof NumberSchema) {
        return new NumberSchemaWrapper((NumberSchema) schema);
    } else {
        throw new IllegalStateException("No wrapper for an underlying schema type '" + schema.getClass() + "': " + schema);
    }
}
 
Example #17
Source File: SchemaDiffVisitor.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
private Schema getReferencedOrOriginal(Schema original) {
    requireNonNull(original);
    if /* while */ (original instanceof ReferenceSchema) {
        original = ((ReferenceSchema) original).getReferredSchema();
    }
    return original;
}
 
Example #18
Source File: SchemaReferencedEvent.java    From json-schema with Apache License 2.0 4 votes vote down vote up
public SchemaReferencedEvent(ReferenceSchema schema, Object instance, Schema referredSchema) {
    super(schema, instance);
    this.referredSchema = referredSchema;
}
 
Example #19
Source File: SchemaDiff.java    From nakadi with MIT License 4 votes vote down vote up
static void recursiveCheck(
        final Schema originalIn,
        final Schema updateIn,
        final SchemaDiffState state) {

    if (originalIn == null && updateIn == null) {
        return;
    }

    if (updateIn == null) {
        state.addChange(SCHEMA_REMOVED);
        return;
    }

    if (originalIn == null) {
        state.addChange(SCHEMA_REMOVED);
        return;
    }

    final Schema original;
    final Schema update;
    if (!originalIn.getClass().equals(updateIn.getClass())) {
        // Tricky part. EmptySchema is the same as an empty ObjectSchema.
        if (originalIn instanceof EmptySchema && updateIn instanceof ObjectSchema) {
            original = replaceWithEmptyObjectSchema(originalIn);
            update = updateIn;
        } else if (typeNarrowed(originalIn, updateIn)) {
            state.addChange(TYPE_NARROWED);
            return;
        } else {
            state.addChange(TYPE_CHANGED);
            return;
        }
    } else {
        original = originalIn;
        update = updateIn;
    }

    state.analyzeSchema(originalIn, () -> {
        if (!Objects.equals(original.getId(), update.getId())) {
            state.addChange(ID_CHANGED);
        }

        if (!Objects.equals(original.getTitle(), update.getTitle())) {
            state.addChange(TITLE_CHANGED);
        }

        if (!Objects.equals(original.getDescription(), update.getDescription())) {
            state.addChange(DESCRIPTION_CHANGED);
        }

        if (original instanceof StringSchema) {
            StringSchemaDiff.recursiveCheck((StringSchema) original, (StringSchema) update, state);
        } else if (original instanceof NumberSchema) {
            NumberSchemaDiff.recursiveCheck((NumberSchema) original, (NumberSchema) update, state);
        } else if (original instanceof EnumSchema) {
            EnumSchemaDiff.recursiveCheck((EnumSchema) original, (EnumSchema) update, state);
        } else if (original instanceof CombinedSchema) {
            CombinedSchemaDiff.recursiveCheck((CombinedSchema) original, (CombinedSchema) update, state);
        } else if (original instanceof ObjectSchema) {
            ObjectSchemaDiff.recursiveCheck((ObjectSchema) original, (ObjectSchema) update, state);
        } else if (original instanceof ArraySchema) {
            ArraySchemaDiff.recursiveCheck((ArraySchema) original, (ArraySchema) update, state);
        } else if (original instanceof ReferenceSchema) {
            ReferenceSchemaDiff.recursiveCheck((ReferenceSchema) original, (ReferenceSchema) update, state);
        }
    });
}
 
Example #20
Source File: ReferenceLookupTest.java    From json-schema with Apache License 2.0 4 votes vote down vote up
private Schema performLookup(String pointerToRef) {
    ReferenceSchema ref = obtainReferenceSchema(pointerToRef);
    return ref.getReferredSchema();
}
 
Example #21
Source File: ReferenceSchemaWrapper.java    From apicurio-registry with Apache License 2.0 4 votes vote down vote up
public ReferenceSchemaWrapper(ReferenceSchema wrapped) {
    this.wrapped = wrapped;
}
 
Example #22
Source File: ReferenceLookupTest.java    From json-schema with Apache License 2.0 4 votes vote down vote up
@Test
public void referenceSchemaLocationIsSet() {
    when(schemaClient.get("http://localhost/child-ref")).thenReturn(asStream(v4Subschema));
    ReferenceSchema ref = obtainReferenceSchema("#/properties/definitionInRemote");
    assertEquals("http://localhost/child-ref#/definitions/SubSchema", ref.getSchemaLocation());
}