11package io .vertx .json .schema ;
22
3+ import io .vertx .core .buffer .Buffer ;
34import io .vertx .core .json .JsonObject ;
4- import io .vertx .junit5 .Timeout ;
55import org .junit .jupiter .api .Test ;
66
7- import java .util .concurrent .TimeUnit ;
7+ import java .io .IOException ;
8+ import java .nio .file .Files ;
9+ import java .nio .file .Paths ;
810
911import static org .assertj .core .api .Assertions .assertThat ;
12+ import static org .assertj .core .api .Assertions .fail ;
1013
1114public class ResolverTest {
1215
1316 @ Test
14- @ Timeout (value = 10 , timeUnit = TimeUnit .SECONDS )
1517 public void testResolves () {
16- JsonObject res =
17- JsonSchema
18- .of (new JsonObject ("{\" $id\" :\" http://www.example.com/\" ,\" $schema\" :\" http://json-schema.org/draft-07/schema#\" ,\" definitions\" :{\" address\" :{\" type\" :\" object\" ,\" properties\" :{\" street_address\" :{\" type\" :\" string\" },\" city\" :{\" type\" :\" string\" },\" state\" :{\" type\" :\" string\" },\" subAddress\" :{\" $ref\" :\" http://www.example.com/#/definitions/address\" }}},\" req\" :{\" required\" :[\" billing_address\" ]}},\" type\" :\" object\" ,\" properties\" :{\" billing_address\" :{\" $ref\" :\" #/definitions/address\" },\" shipping_address\" :{\" $ref\" :\" #/definitions/address\" }},\" $ref\" :\" #/definitions/req\" }" ))
19- .resolve ();
18+
19+ SchemaRepository repo = SchemaRepository .create (new JsonSchemaOptions ().setBaseUri ("http://vertx.io" ));
20+
21+ JsonSchema schema = JsonSchema
22+ .of (new JsonObject ("{\" $id\" :\" http://www.example.com/\" ,\" $schema\" :\" http://json-schema.org/draft-07/schema#\" ,\" definitions\" :{\" address\" :{\" type\" :\" object\" ,\" properties\" :{\" street_address\" :{\" type\" :\" string\" },\" city\" :{\" type\" :\" string\" },\" state\" :{\" type\" :\" string\" },\" subAddress\" :{\" $ref\" :\" http://www.example.com/#/definitions/address\" }}},\" req\" :{\" required\" :[\" billing_address\" ]}},\" type\" :\" object\" ,\" properties\" :{\" billing_address\" :{\" $ref\" :\" #/definitions/address\" },\" shipping_address\" :{\" $ref\" :\" #/definitions/address\" }},\" $ref\" :\" #/definitions/req\" }" ));
23+
24+ repo .dereference (schema );
25+
26+ String before = schema .toString ();
27+
28+ JsonObject res = schema .resolve ();
2029
2130 JsonObject ptr = res .getJsonObject ("properties" ).getJsonObject ("billing_address" ).getJsonObject ("properties" );
2231 assertThat (ptr .getJsonObject ("city" ).getString ("type" ))
@@ -30,5 +39,29 @@ public void testResolves() {
3039 // array checks
3140 assertThat (res .getJsonArray ("required" ).getString (0 ))
3241 .isEqualTo ("billing_address" );
42+
43+ assertThat (res .encodePrettily ().contains ("__absolute_uri" )).isFalse ();
44+ assertThat (res .encodePrettily ().contains ("__absolute_ref" )).isFalse ();
45+ assertThat (res .encodePrettily ().contains ("__absolute_recursive_ref" )).isFalse ();
46+
47+ String after = schema .toString ();
48+
49+ // ensure that the clean up operation doesn't affect the source object
50+ assertThat (before ).isEqualTo (after );
51+
52+ }
53+
54+ @ Test
55+ public void testRefResolverFail () throws IOException {
56+ try {
57+ JsonObject res =
58+ JsonSchema
59+ .of (new JsonObject (Buffer .buffer (Files .readAllBytes (Paths .get ("src" , "test" , "resources" , "ref_test" , "person_draft201909.json" )))))
60+ .resolve ();
61+
62+ fail ("Should not reach here" );
63+ } catch (SchemaException e ) {
64+ // OK
65+ }
3366 }
3467}
0 commit comments