@Override public Schema createProperty() { return new Schema().type("object"); } };
@Test(description = "it should serialize a RefProperty") public void serializeRefProperty() throws IOException { final Schema p = new Schema().$ref("#/definitions/Dog"); final String json = "{\"$ref\":\"#/definitions/Dog\"}"; assertEquals(m.writeValueAsString(p), json); }
schema = new Schema().$ref(node.get("$ref").asText()); } else { // assume object schema = deserializeObjectSchema(node);
@Test(description = "it should serialize an array model") public void serializeArrayModel() throws IOException { final ArraySchema model = new ArraySchema(); model.setItems(new Schema().$ref("Pet")); assertEquals(m.writeValueAsString(model), "{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Pet\"}}"); }
public static Schema resolveSchemaFromType(Class<?> schemaImplementation, Components components, JsonView jsonViewAnnotation) { Schema schemaObject = new Schema(); if (schemaImplementation.getName().startsWith("java.lang")) { schemaObject.setType(schemaImplementation.getSimpleName().toLowerCase()); } else { ResolvedSchema resolvedSchema = ModelConverters.getInstance().readAllAsResolvedSchema(new AnnotatedType().type(schemaImplementation).jsonViewAnnotation(jsonViewAnnotation)); Map<String, Schema> schemaMap; if (resolvedSchema != null) { schemaMap = resolvedSchema.referencedSchemas; schemaMap.forEach((key, referencedSchema) -> { if (components != null) { components.addSchemas(key, referencedSchema); } }); schemaObject.set$ref(COMPONENTS_REF + resolvedSchema.schema.getName()); } } if (StringUtils.isBlank(schemaObject.get$ref()) && StringUtils.isBlank(schemaObject.getType())) { // default to string schemaObject.setType("string"); } return schemaObject; }
private static Schema process(Schema id, String propertyName, AnnotatedType type, ModelConverterContext context) { Schema model = context.resolve(removeJsonIdentityAnnotations(type)); Schema mi = model; mi.addProperties(propertyName, id); return new Schema().$ref(StringUtils.isNotEmpty(mi.get$ref()) ? mi.get$ref() : mi.getName()); } private static AnnotatedType removeJsonIdentityAnnotations(AnnotatedType type) {
@Test public void testByteProperty() { Schema model = new Schema() .addProperties("byteProperty", new ByteArraySchema()); assertEquals(Json.pretty(model), "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteProperty\" : {" + NEWLINE + " \"type\" : \"string\"," + NEWLINE + " \"format\" : \"byte\"" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + "}"); }
@Test(description = "it should not create an xml object for $ref") public void shouldNotCreateXmlObjectForRef() throws IOException { final Schema model = new Schema().$ref("Monster"); model.setDescription("oops"); model.setExternalDocs(new ExternalDocumentation() .description("external docs") .url("http://swagger.io")); assertEquals(Json.mapper().writeValueAsString(model), "{\"$ref\":\"#/components/schemas/Monster\"}"); }
@Test public void testByteArray() { Schema model = new Schema() .addProperties("byteArray", new ArraySchema().items(new BinarySchema())); assertEquals(Json.pretty(model), "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteArray\" : {" + NEWLINE + " \"type\" : \"array\"," + NEWLINE + " \"items\" : {" + NEWLINE + " \"type\" : \"string\"," + NEWLINE + " \"format\" : \"binary\"" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + "}"); }
@Override public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> next) { if (type.getType() instanceof Class<?>) { Class<?> cls = (Class<?>) type.getType(); if (GenericModel.class.isAssignableFrom(cls)) { Schema impl = new Schema(); impl.title(cls.getSimpleName()); for (Entry<String, Class<?>> entry : GenericModel.getDeclaredProperties().entrySet()) { impl.addProperties(entry.getKey(), context.resolve(new AnnotatedType(entry.getValue()))); } context.defineModel(impl.getTitle(), impl); return impl; } } if (next.hasNext()) { return next.next().resolve(type, context, next); } else { return null; } } }
@Test(description = "it should convert a model") public void convertModel() throws JsonProcessingException { final Schema pet = new Schema(); final Map<String, Schema> props = new LinkedHashMap<String, Schema>(); props.put("intValue", new IntegerSchema());
@Test public void testReadOnlyByteArray() { Schema model = new Schema() .addProperties("byteArray", new ArraySchema().items(new BinarySchema()).readOnly(true)); assertEquals(Json.pretty(model), "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteArray\" : {" + NEWLINE + " \"type\" : \"array\"," + NEWLINE + " \"readOnly\" : true," + NEWLINE + " \"items\" : {" + NEWLINE + " \"type\" : \"string\"," + NEWLINE + " \"format\" : \"binary\"" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + "}"); }
@Test(description = "should serialize string value") public void testStringValue() { final QueryParameter param = (QueryParameter) new QueryParameter().required(false); Schema schema = new Schema() .type("string"); schema.setDefault("false"); param.setSchema(schema); final String json = "{" + " \"in\":\"query\"," + " \"required\":false," + " \"schema\":{" + " \"type\":\"string\"," + " \"default\":\"false\"" + " }" + "}"; SerializationMatchers.assertEqualsToJson(param, json); }
@Test(description = "should serialize incorrect boolean value as string") public void testIncorrectBoolean() { final QueryParameter param = (QueryParameter) new QueryParameter().required(false); Schema schema = new Schema() .type("boolean"); schema.setDefault("test"); param.setSchema(schema); final String json = "{" + " \"in\":\"query\"," + " \"required\":false," + " \"schema\":{" + " \"type\":\"boolean\"," + " \"default\":\"test\"" + " }" + "}"; SerializationMatchers.assertEqualsToJson(param, json); }
@Test(description = "should serialize boolean value") public void testBooleanValue() { final QueryParameter param = (QueryParameter) new QueryParameter().required(false); Schema schema = new Schema() .type("boolean"); schema.setDefault("false"); param.setSchema(schema); final String json = "{" + " \"in\":\"query\"," + " \"required\":false," + " \"schema\":{" + " \"type\":\"boolean\"," + " \"default\":\"false\"" + " }" + "}"; SerializationMatchers.assertEqualsToJson(param, json); }
@Test(description = "it should serialize a ref BodyParameter") public void serializeRefBodyParameter() { final Schema model = new Schema().$ref("#/definitions/Cat"); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String json = "{\"content\":{\"*/*\":{\"schema\":{\"$ref\":\"#/definitions/Cat\"}}}}"; SerializationMatchers.assertEqualsToJson(p, json); }
@Test(description = "it should serialize an array BodyParameter") public void serializeArrayBodyParameter() { final Schema model = new ArraySchema().items(new Schema().$ref("#/definitions/Cat")); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String json = "{\"content\":{\"*/*\":{\"schema\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Cat\"}}}}}"; SerializationMatchers.assertEqualsToJson(p, json); }
@Test(description = "it should serialize a BodyParameter") public void serializeBodyParameter() { final Schema model = new Schema() .title("Cat") .addProperties("name", new StringSchema()); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String json = "{\"content\":{\"*/*\":{\"schema\":{\"title\":\"Cat\",\"properties\":{\"name\":{\"type\":\"string\"}}}}}}"; SerializationMatchers.assertEqualsToJson(p, json); }
@Test(description = "it should serialize a BodyParameter to yaml") public void serializeBodyParameterToYaml() { final Schema model = new Schema() .title("Cat") .addProperties("name", new StringSchema()); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String yaml = "---\n" + "content:\n" + " '*/*':\n" + " schema:\n" + " title: Cat\n" + " properties:\n" + " name:\n" + " type: string"; SerializationMatchers.assertEqualsToYaml(p, yaml); }