org.everit.json.schema.ObjectSchema Java Examples

The following examples show how to use org.everit.json.schema.ObjectSchema. 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: JsonSchemaFromFieldDescriptorsGenerator.java    From restdocs-raml with MIT License 6 votes vote down vote up
private Schema traverse(List<String> traversedSegments, List<JsonFieldPath> jsonFieldPaths, ObjectSchema.Builder builder) {

        Map<String, List<JsonFieldPath>> groupedFields = groupFieldsByFirstRemainingPathSegment(traversedSegments, jsonFieldPaths);
        groupedFields.forEach((propertyName, fieldList) -> {

            List<String> newTraversedSegments = new ArrayList<>(traversedSegments);
            newTraversedSegments.add(propertyName);
            fieldList.stream()
                    .filter(isDirectMatch(newTraversedSegments))
                    .findFirst()
                    .map(directMatch -> {
                        if (fieldList.size() == 1) {
                            handleEndOfPath(builder, propertyName, directMatch.getFieldDescriptor());
                        } else {
                            List<JsonFieldPath> newFields = new ArrayList<>(fieldList);
                            newFields.remove(directMatch);
                            processRemainingSegments(builder, propertyName, newTraversedSegments, newFields, (String) directMatch.getFieldDescriptor().getDescription());
                        }
                        return true;
                    }).orElseGet(() -> {
                        processRemainingSegments(builder, propertyName, newTraversedSegments, fieldList, null);
                        return true;
                    });
        });
        return builder.build();
    }
 
Example #2
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 #3
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 6 votes vote down vote up
@Test
public void unprocessedPropertiesAreLoaded() {
    SchemaLoader loader = SchemaLoader.builder()
            .draftV7Support()
            .useDefaults(true)
            .schemaJson(get("schemaWithUnprocessedProperties"))
            .build();
    ObjectSchema actual = (ObjectSchema) loader.load().build();

    assertEquals(ImmutableMap.of(
            "unproc0", 1,
            "unproc1", "asdasd",
            "nullable", false
    ), actual.getUnprocessedProperties());
    assertEquals(emptyMap(), actual.getPropertySchemas().get("prop").getUnprocessedProperties());
    assertEquals(ImmutableMap.of(
            "unproc4", true,
            "unproc5", JSONObject.NULL
    ), actual.getPropertySchemas().get("prop2").getUnprocessedProperties());
    assertEquals(ImmutableMap.of(
            "unproc6", false
    ), actual.getPropertySchemas().get("prop3").getUnprocessedProperties());
}
 
Example #4
Source File: ObjectSchemaDiff.java    From nakadi with MIT License 6 votes vote down vote up
private static void compareAttributes(
        final ObjectSchema original, final ObjectSchema update, final SchemaDiffState state) {

    if (update.getRequiredProperties().containsAll(original.getRequiredProperties())) {
        if (original.getRequiredProperties().size() != update.getRequiredProperties().size()) {
            state.addChange("required", REQUIRED_ARRAY_EXTENDED);
        }
    } else {
        state.addChange("required", REQUIRED_ARRAY_CHANGED);
    }

    if (!Objects.equals(original.getMaxProperties(), update.getMaxProperties())) {
        state.addChange("maxProperties", ATTRIBUTE_VALUE_CHANGED);
    }

    if (!Objects.equals(original.getMinProperties(), update.getMinProperties())) {
        state.addChange("minProperties", ATTRIBUTE_VALUE_CHANGED);
    }
}
 
Example #5
Source File: ObjectSchemaDiff.java    From nakadi with MIT License 6 votes vote down vote up
private static void compareProperties(
        final ObjectSchema original, final ObjectSchema update, final SchemaDiffState state) {
    state.runOnPath("properties", () -> {
        for (final Map.Entry<String, Schema> property : original.getPropertySchemas().entrySet()) {
            state.runOnPath(property.getKey(), () -> {
                if (!update.getPropertySchemas().containsKey(property.getKey())) {
                    state.addChange(PROPERTY_REMOVED);
                } else {
                    SchemaDiff.recursiveCheck(
                            property.getValue(), update.getPropertySchemas().get(property.getKey()), state);
                }
            });
        }
        if (update.getPropertySchemas().size() > original.getPropertySchemas().size()) {
            state.addChange(PROPERTIES_ADDED);
        }
    });
}
 
Example #6
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 #7
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 #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
public void schemaPointerIsPopulated() {
    JSONObject rawSchema = ResourceLoader.DEFAULT.readObj("objecttestschemas.json")
            .getJSONObject("objectWithSchemaDep");
    ObjectSchema schema = (ObjectSchema) SchemaLoader.load(rawSchema);

    String actualSchemaPointer = schema.getSchemaDependencies().get("a").getSchemaLocation();
    String expectedSchemaPointer = new JSONPointer(asList("dependencies", "a")).toURIFragment();
    assertEquals(expectedSchemaPointer, actualSchemaPointer);
}
 
Example #10
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDefaultNumberTest() {
    JSONObject rawSchema = ALL_SCHEMAS.getJSONObject("defaultsTest");
    ObjectSchema schema = (ObjectSchema) SchemaLoader
            .builder()
            .useDefaults(true)
            .schemaJson(rawSchema)
            .build().load().build();

    JSONObject obj = new JSONObject();
    schema.validate(obj);

    assertEquals(10, obj.getNumber("numberDefault"));
}
 
Example #11
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDefaultStringTest() {
    JSONObject rawSchema = ALL_SCHEMAS.getJSONObject("defaultsTest");
    ObjectSchema schema = (ObjectSchema) SchemaLoader
            .builder()
            .useDefaults(true)
            .schemaJson(rawSchema)
            .build().load().build();

    JSONObject obj = new JSONObject();
    schema.validate(obj);

    assertEquals("yee", obj.getString("stringDefault"));
}
 
Example #12
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDefaultBooleanTest() {
    JSONObject rawSchema = ALL_SCHEMAS.getJSONObject("defaultsTest");
    ObjectSchema schema = (ObjectSchema) SchemaLoader
            .builder()
            .useDefaults(true)
            .schemaJson(rawSchema)
            .build().load().build();

    JSONObject obj = new JSONObject();
    schema.validate(obj);

    assertEquals(true, obj.getBoolean("booleanDefault"));
}
 
Example #13
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDefaultObjectTest() {
    JSONObject rawSchema = ALL_SCHEMAS.getJSONObject("defaultsTest");
    ObjectSchema schema = (ObjectSchema) SchemaLoader
            .builder()
            .useDefaults(true)
            .schemaJson(rawSchema)
            .build().load().build();

    JSONObject obj = new JSONObject();
    schema.validate(obj);

    assertEquals(new JSONObject("{\"hello\": \"world\"}").toString(), obj.getJSONObject("objectDefault").toString());
}
 
Example #14
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDefaultArrayTest() {
    JSONObject rawSchema = ALL_SCHEMAS.getJSONObject("defaultsTest");
    ObjectSchema schema = (ObjectSchema) SchemaLoader
            .builder()
            .useDefaults(true)
            .schemaJson(rawSchema)
            .build().load().build();

    JSONObject obj = new JSONObject();
    schema.validate(obj);

    assertEquals(new JSONArray("[\"a\",\"b\",\"c\"]").toString(), obj.getJSONArray("arrayDefault").toString());
}
 
Example #15
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDefaultNullTest() {
    JSONObject rawSchema = ALL_SCHEMAS.getJSONObject("defaultsTest");
    ObjectSchema schema = (ObjectSchema) SchemaLoader
            .builder()
            .useDefaults(true)
            .schemaJson(rawSchema)
            .build().load().build();

    JSONObject obj = new JSONObject();
    schema.validate(obj);

    assertEquals(JSONObject.NULL, obj.get("nullDefault"));
}
 
Example #16
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void nullableBooleansAre_Loaded_withNullableSupport() {
    SchemaLoader loader = SchemaLoader.builder()
            .nullableSupport(true)
            .schemaJson(get("nullableSupport"))
            .build();
    ObjectSchema actual = (ObjectSchema) loader.load().build();
    Schema nullableSchema = actual.getPropertySchemas().get("isNullable");
    Schema nonNullableSchema = actual.getPropertySchemas().get("nonNullable");
    Schema implicitNonNullable = actual.getPropertySchemas().get("implicitNonNullable");

    assertTrue(nullableSchema.isNullable());
    assertFalse(nonNullableSchema.isNullable());
    assertFalse(implicitNonNullable.isNullable());
}
 
Example #17
Source File: SchemaLoaderTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void nullableBooleansAre_NotLoaded_withoutNullableSupport() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("nullableSupport"));
    Schema nullableSchema = actual.getPropertySchemas().get("isNullable");
    Schema nonNullableSchema = actual.getPropertySchemas().get("nonNullable");
    Schema implicitNonNullable = actual.getPropertySchemas().get("implicitNonNullable");

    assertNull(nullableSchema.isNullable());
    assertNull(nonNullableSchema.isNullable());
    assertNull(implicitNonNullable.isNullable());
}
 
Example #18
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 #19
Source File: ReadWriteContextLoadingTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void worksOnlyInV7Mode() {
    ObjectSchema rootSchema = (ObjectSchema) loadAsV6(LOADER.readObj("read-write-context.json"));
    Schema readOnlyProp = rootSchema.getPropertySchemas().get("readOnlyProp");
    Schema writeOnlyProp = rootSchema.getPropertySchemas().get("writeOnlyProp");
    assertNull(readOnlyProp.isReadOnly());
    assertNull(writeOnlyProp.isWriteOnly());
}
 
Example #20
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void objectSchemaHasField() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution"));
    Assert.assertTrue(actual.definesProperty("#/rectangle"));
    Assert.assertTrue(actual.definesProperty("#/rectangle/a"));
    Assert.assertTrue(actual.definesProperty("#/rectangle/b"));

    assertFalse(actual.definesProperty("#/rectangle/c"));
    assertFalse(actual.definesProperty("#/rectangle/"));
    assertFalse(actual.definesProperty("#/"));
    assertFalse(actual.definesProperty("#/a"));
    assertFalse(actual.definesProperty("#"));
    assertFalse(actual.definesProperty("#/rectangle/a/d"));
}
 
Example #21
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void patternPropertiesHasField() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("patternProperties"));
    Assert.assertTrue(actual.definesProperty("#/a"));
    Assert.assertTrue(actual.definesProperty("#/aa"));
    Assert.assertTrue(actual.definesProperty("#/aaa"));
    Assert.assertTrue(actual.definesProperty("#/aaaa"));
    Assert.assertTrue(actual.definesProperty("#/aaaaa"));
    
    assertFalse(actual.definesProperty("b"));
}
 
Example #22
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void objectWithSchemaDep() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectWithSchemaDep"));
    Assert.assertTrue(actual.definesProperty("#/a"));
    Assert.assertTrue(actual.definesProperty("#/b"));

    assertFalse(actual.definesProperty("#/c"));
}
 
Example #23
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void objectWithSchemaRectangleDep() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectWithSchemaRectangleDep"));
    Assert.assertTrue(actual.definesProperty("#/d"));
    Assert.assertTrue(actual.definesProperty("#/rectangle/a"));
    Assert.assertTrue(actual.definesProperty("#/rectangle/b"));

    assertFalse(actual.definesProperty("#/c"));
    assertFalse(actual.definesProperty("#/d/c"));
    assertFalse(actual.definesProperty("#/rectangle/c"));
}
 
Example #24
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void objectEscape() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectEscape"));
    Assert.assertTrue(actual.definesProperty("#/a~0b"));
    Assert.assertTrue(actual.definesProperty("#/a~0b/c~1d"));

    assertFalse(actual.definesProperty("#/a~0b/c/d"));
}
 
Example #25
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void definesPropertyIfSubschemaMatchCountIsAcceptedByCriterion() {
    CombinedSchema subject = CombinedSchema.builder()
            .subschema(ObjectSchema.builder().addPropertySchema("a", BooleanSchema.INSTANCE).build())
            .subschema(ObjectSchema.builder().addPropertySchema("b", BooleanSchema.INSTANCE).build())
            .criterion((subschemaCount, matchingSubschemaCount) -> {
                if (matchingSubschemaCount == 1 && subschemaCount == 2) {
                    // dummy exception
                    throw new ValidationException(Object.class, new Object());
                }
            })
            .build();
    assertFalse(subject.definesProperty("a"));
}
 
Example #26
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void testOfTest() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("patternPropsAndSchemaDeps"));
    JSONObject input = ResourceLoader.DEFAULT
            .readObj("objecttestcases.json")
            .getJSONObject("validOfPatternPropsAndSchemaDeps");
    actual.validate(input);
}
 
Example #27
Source File: DefinesPropertyTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Test
public void patternPropsAndSchemaDefs() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("patternPropsAndSchemaDeps"));
    // Assert.assertTrue(actual.definesProperty("#/1stLevel"));
    // Assert.assertTrue(actual.definesProperty("#/1stLevel/2ndLevel"));
    Assert.assertTrue(actual.definesProperty("#/1stLevel/2ndLevel/3rdLev"));
    // Assert.assertTrue(actual.definesProperty("#/1stLevel/2ndLevel/3rdLevel/4thLevel"));
}
 
Example #28
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 #29
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 #30
Source File: JsonSchemaFromFieldDescriptorsGenerator.java    From restdocs-raml with MIT License 5 votes vote down vote up
public String generateSchema(List<FieldDescriptor> fieldDescriptors, String title) {
    List<JsonFieldPath> jsonFieldPaths = distinct(fieldDescriptors).stream()
            .map(JsonFieldPath::compile)
            .collect(toList());

    Schema schema = traverse(emptyList(), jsonFieldPaths, (ObjectSchema.Builder) ObjectSchema.builder().title(title));

    return toFormattedString(unWrapRootArray(jsonFieldPaths, schema));
}