DefaultRefStrategy implements RefStrategy { @Override public String toPointer(ShapeId id) { if (id.getMember().isPresent()) { MemberShape member = model.expectShape(id, MemberShape.class); return createMemberPointer(member); } return rootPointer + stripNonAlphaNumericCharsIfNecessary(id.getName()); } DefaultRefStrategy(Model model, JsonSchemaConfig config, PropertyNamingStrategy propertyNamingStrategy); @Override String toPointer(ShapeId id); @Override boolean isInlined(Shape shape);  }
@Test public void usesDefaultPointer() { RefStrategy ref = RefStrategy.createDefaultStrategy( Model.builder().build(), new JsonSchemaConfig(), propertyNamingStrategy); String pointer = ref.toPointer(ShapeId.from("smithy.example#Foo")); assertThat(pointer, equalTo("#/definitions/Foo")); }
@Test public void usesCustomPointerAndAppendsSlashWhenNecessary() { JsonSchemaConfig config = new JsonSchemaConfig(); config.setDefinitionPointer("#/components/schemas"); RefStrategy ref = RefStrategy .createDefaultStrategy(Model.builder().build(), config, propertyNamingStrategy); String pointer = ref.toPointer(ShapeId.from("smithy.example#Foo")); assertThat(pointer, equalTo("#/components/schemas/Foo")); }
@Test public void usesCustomPointerAndOmitsSlashWhenNecessary() { JsonSchemaConfig config = new JsonSchemaConfig(); config.setDefinitionPointer("#/components/schemas"); RefStrategy ref = RefStrategy .createDefaultStrategy(Model.builder().build(), config, propertyNamingStrategy); String pointer = ref.toPointer(ShapeId.from("smithy.example#Foo")); assertThat(pointer, equalTo("#/components/schemas/Foo")); }
@Test public void stripsNonAlphanumericCharactersWhenRequested() { JsonSchemaConfig config = new JsonSchemaConfig(); config.setAlphanumericOnlyRefs(true); RefStrategy ref = RefStrategy .createDefaultStrategy(Model.builder().build(), config, propertyNamingStrategy); String pointer = ref.toPointer(ShapeId.from("smithy.example#Foo_Bar")); assertThat(pointer, equalTo("#/definitions/FooBar")); }
@Test public void addsListAndSetMembers() { StringShape string = StringShape.builder().id("foo.bar#String").build(); MemberShape member = MemberShape.builder() .id("foo.bar#Scripts$member") .target("foo.bar#String") .build(); ListShape list = ListShape.builder() .id("foo.bar#Scripts") .member(member) .build(); Model model = Model.builder().addShapes(string, list, member).build(); RefStrategy ref = RefStrategy .createDefaultStrategy(model, new JsonSchemaConfig(), propertyNamingStrategy); String pointer = ref.toPointer(member.getId()); assertThat(pointer, equalTo("#/definitions/Scripts/items")); }
@Test public void addsMapMembers() { StringShape string = StringShape.builder().id("foo.bar#String").build(); MemberShape key = MemberShape.builder() .id("foo.bar#Scripts$key") .target("foo.bar#String") .build(); MemberShape value = MemberShape.builder() .id("foo.bar#Scripts$value") .target("foo.bar#String") .build(); MapShape map = MapShape.builder() .id("foo.bar#Scripts") .key(key) .value(value) .build(); Model model = Model.builder().addShapes(string, map, key, value).build(); RefStrategy ref = RefStrategy .createDefaultStrategy(model, new JsonSchemaConfig(), propertyNamingStrategy); assertThat(ref.toPointer(key.getId()), equalTo("#/definitions/Scripts/propertyNames")); assertThat(ref.toPointer(value.getId()), equalTo("#/definitions/Scripts/additionalProperties")); }
@Test public void addsStructureMembers() { StringShape string = StringShape.builder().id("foo.bar#String").build(); MemberShape member = MemberShape.builder() .id("foo.bar#Scripts$pages") .target("foo.bar#String") .build(); StructureShape struct = StructureShape.builder() .id("foo.bar#Scripts") .addMember(member) .build(); Model model = Model.builder().addShapes(string, struct, member).build(); RefStrategy ref = RefStrategy .createDefaultStrategy(model, new JsonSchemaConfig(), propertyNamingStrategy); assertThat(ref.toPointer(struct.getId()), equalTo("#/definitions/Scripts")); assertThat(ref.toPointer(member.getId()), equalTo("#/definitions/Scripts/properties/pages")); }
@Test public void usesRefForStructureMembers() { StructureShape baz = StructureShape.builder() .id("foo.bar#Baz") .addMember("bam", ShapeId.from("foo.bar#Bam")) .build(); StructureShape bam = StructureShape.builder() .id("foo.bar#Bam") .build(); Model model = Model.builder().addShapes(baz, bam).build(); RefStrategy ref = RefStrategy .createDefaultStrategy(model, new JsonSchemaConfig(), propertyNamingStrategy); assertThat(ref.toPointer(baz.getMember("bam").get().getId()), equalTo("#/definitions/Bam")); }