public Schema createProperty() { return new StringSchema(); } },
@Test(description = "convert a model with Guava optionals") public void convertModelWithGuavaOptionals() { final Map<String, Schema> schemas = ModelConverters.getInstance().read(ModelWithJaxBDefaultValues.class); final Map<String, Schema> properties = schemas.get("ModelWithJaxBDefaultValues").getProperties(); assertEquals(properties.size(), 2); assertEquals(((StringSchema) properties.get("name")).getDefault(), "Tony"); assertEquals((int) ((IntegerSchema) properties.get("age")).getDefault(), 100); } }
@Override public Schema createProperty() { return new StringSchema().format("uri"); } },
@Test(description = "it should serialize a string property with readOnly unset") public void deserializeNotReadOnlyStringProperty() throws IOException { final StringSchema p = new StringSchema(); p.setReadOnly(false); final String json = "{\"type\":\"string\",\"readOnly\":false}"; assertEquals(m.writeValueAsString(p), json); }
@Test(description = "it should serialize a string property with readOnly set") public void serializeReadOnlyStringProperty() throws IOException { final Schema p = new StringSchema().readOnly(true); final String json = "{\"type\":\"string\",\"readOnly\":true}"; assertEquals(m.writeValueAsString(p), json); }
@Override public String getTypeDeclaration(Schema p) { if (ModelUtils.isArraySchema(p)) { ArraySchema ap = (ArraySchema) p; Schema inner = ap.getItems(); if (inner == null) { LOGGER.warn(ap.getName() + "(array property) does not have a proper inner type defined.Default to string"); inner = new StringSchema().description("TODO default missing array inner type to string"); } return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">"; } else if (ModelUtils.isMapSchema(p)) { Schema inner = ModelUtils.getAdditionalProperties(p); if (inner == null) { LOGGER.warn(p.getName() + "(map property) does not have a proper inner type defined. Default to string"); inner = new StringSchema().description("TODO default missing array inner type to string"); } return getSchemaType(p) + "<String, " + getTypeDeclaration(inner) + ">"; } return super.getTypeDeclaration(p); }
@Test(description = "it should create a model") public void createModel() { ObjectSchema model = new ObjectSchema(); model.addProperties("name", new StringSchema().example("Tony")); model.addProperties("id", new IntegerSchema().example(123)); assertNotNull(model); }
@Test(description = "it should serialize a StringProperty with enums") public void serializeEnumStringProperty() throws IOException { final StringSchema p = new StringSchema(); p._enum(new ArrayList<String>() {{ this.add("a"); this.add("b"); }}); final String json = "{\"type\":\"string\",\"enum\":[\"a\",\"b\"]}"; assertEquals(m.writeValueAsString(p), json); }
@Test(description = "it should serialize a StringProperty") public void serializeStringProperty() throws IOException { final StringSchema p = new StringSchema() ._default("Bob"); final String json = "{\"type\":\"string\",\"default\":\"Bob\"}"; assertEquals(m.writeValueAsString(p), json); }
switch (queryParameter.getSchema().getType()) { case "string": StringSchema stringSchema = new StringSchema(); stringSchema.setMinLength(queryParameter.getSchema().getMinLength()); stringSchema.setMaxLength(queryParameter.getSchema().getMaxLength()); stringSchema.setPattern(queryParameter.getSchema().getPattern()); stringSchema.setEnum(queryParameter.getSchema().getEnum()); property = stringSchema; break;
@Test(description = "it should deserialize a StringProperty with enums") public void deserializeEnumStringProperty() throws IOException { final String json = "{\"type\":\"string\",\"enum\":[\"a\",\"b\"]}"; final Schema p = m.readValue(json, Schema.class); assertEquals(p.getType(), "string"); List<String> _enum = ((StringSchema) p).getEnum(); assertNotNull(_enum); assertEquals(_enum, Arrays.asList("a", "b")); assertEquals(p.getClass(), StringSchema.class); assertEquals(m.writeValueAsString(p), json); }
} else if (property instanceof StringSchema) { StringSchema stringProperty = (StringSchema) property; List<String> enums = stringProperty.getEnum(); if (CollectionUtils.isNotEmpty(enums)) { type = new EnumType(stringProperty.getTitle(), enums); } else if (isNotBlank(stringProperty.getFormat())) { type = new BasicType(stringProperty.getType(), stringProperty.getTitle(), stringProperty.getFormat()); } else { type = new BasicType(stringProperty.getType(), stringProperty.getTitle());
if (inner == null) { LOGGER.warn(ap.getName() + "(array property) does not have a proper inner type defined.Default to string"); inner = new StringSchema().description("TODO default missing array inner type to string"); if (inner == null) { LOGGER.warn(p.getName() + "(map property) does not have a proper inner type defined. Default to string"); inner = new StringSchema().description("TODO default missing map inner type to string");
@Test(description = "it should create a model with example") public void createModelWithExample() { ObjectSchema model = new ObjectSchema(); model.addProperties("name", new StringSchema().example("Tony")); model.addProperties("id", new IntegerSchema().example(123)); model.example("{\"name\":\"Fred\",\"id\":123456\"}"); assertEquals(model.getExample(), "{\"name\":\"Fred\",\"id\":123456\"}"); } }
@Test(description = "it should serialize a path parameter with enum") public void serializeEnumPathParameter() { List<String> values = new ArrayList<>(); values.add("a"); values.add("b"); values.add("c"); Parameter p = new PathParameter() .schema(new StringSchema()._enum(values)); final String json = "{" + " \"in\":\"path\"," + " \"required\":true," + " \"schema\":{" + " \"type\":\"string\"," + " \"enum\":[\"a\",\"b\",\"c\"]" + " }" + "}"; SerializationMatchers.assertEqualsToJson(p, json); }
@Test(description = "should mark a parameter as readOnly") public void testReadOnlyParameter() throws Exception { final QueryParameter qp = new QueryParameter(); qp.setSchema(new StringSchema().readOnly(true)); final String json = "{" + " \"in\":\"query\"," + " \"schema\":{" + " \"type\":\"string\"," + " \"readOnly\":true" + " }" + "}"; SerializationMatchers.assertEqualsToJson(qp, json); }
@Test(description = "it retains enums per ") public void testEnumParser() throws IOException { String json = "{\n" + " \"properties\": {\n" + " \"AdvStateType\": {\n" + " \"description\": \"Advertising State\",\n" + " \"enum\": [\n" + " \"off\",\n" + " \"on\"\n" + " ],\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + "}"; final Schema model = Json.mapper().readValue(json, Schema.class); StringSchema p = (StringSchema) model.getProperties().get("AdvStateType"); assertNotNull(p.getEnum()); assertEquals(p.getEnum().get(0), "off"); assertEquals(p.getEnum().get(1), "on"); }
@Test(description = "it should serialize a string array property") public void serializeArrayStringProperty() throws IOException { final Schema p = new ArraySchema().items(new StringSchema()); final String json = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; assertEquals(m.writeValueAsString(p), json); }
if (inner == null) { LOGGER.warn("warning! No inner type supplied for array parameter \"" + s.getName() + "\", using String"); inner = new StringSchema().description("//TODO automatically added by openapi-generator due to missing iner type definition in the spec"); arraySchema.setItems(inner);
@Override public Schema createProperty() { return new StringSchema().format("url"); } },