@Test(description = "it should deserialize a response per #1349") public void testMapDeserialization() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); assertTrue(responseSchema instanceof MapSchema); MapSchema mp = (MapSchema) responseSchema; assertTrue(mp.getAdditionalProperties() instanceof IntegerSchema); }
@Test(description = "it should serialize a string MapProperty") public void serializeStringMapProperty() throws IOException { final Schema p = new MapSchema().additionalProperties(new StringSchema()); final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"string\"}}"; assertEquals(m.writeValueAsString(p), json); }
private Schema deserializeObjectSchema(JsonNode node) { JsonNode additionalProperties = node.get("additionalProperties"); Schema schema = null; if (additionalProperties != null) { // try first to convert to Schema, if it fails it must be a boolean try { Schema innerSchema = Json.mapper().convertValue(additionalProperties, Schema.class); ((ObjectNode)node).remove("additionalProperties"); MapSchema ms = Json.mapper().convertValue(node, MapSchema.class); ms.setAdditionalProperties(innerSchema); schema = ms; } catch (Exception e) { schema = Json.mapper().convertValue(node, ObjectSchema.class); Boolean additionalPropsBoolean = Json.mapper().convertValue(additionalProperties, Boolean.class); schema.setAdditionalProperties(additionalPropsBoolean); } } else { schema = Json.mapper().convertValue(node, ObjectSchema.class); } return schema; } }
@Override public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) { final JavaType javaType = _mapper.constructType(type.getType()); if (Pair.class.isAssignableFrom(javaType.getRawClass())) { final JavaType left = javaType.containedType(0); final String name = "MapOf" + WordUtils.capitalize(_typeName(left)); return new MapSchema() .name(name) .additionalProperties(context.resolve(new AnnotatedType(left))); } return super.resolve(type, context, chain); } }
private static Object generateExampleForMapProperty(MapSchema property, MarkupDocBuilder markupDocBuilder) { if (property.getExample() != null) { return property.getExample(); } Map<String, Object> exampleMap = new LinkedHashMap<>(); Schema valueProperty = (Schema) property.getAdditionalProperties(); if (valueProperty.getExample() != null) { return valueProperty.getExample(); } exampleMap.put("string", PropertyAdapter.generateExample(valueProperty, markupDocBuilder)); return exampleMap; }
} else if (property instanceof MapSchema) { MapSchema mapProperty = (MapSchema) property; Schema additionalProperties = (Schema) mapProperty.getAdditionalProperties(); if (additionalProperties == null) type = new MapType(mapProperty.getTitle(), new ObjectType(null, null)); // FIXME : Workaround for Swagger parser issue with composed models (https://github.com/Swagger2Markup/swagger2markup/issues/150) else type = new MapType(mapProperty.getTitle(), new PropertyAdapter(additionalProperties).getType(definitionDocumentResolver)); } else if (property instanceof StringSchema) { StringSchema stringProperty = (StringSchema) property;
@SuppressWarnings("static-method") public Schema modelFromProperty(MapSchema object, @SuppressWarnings("unused") String path) { String description = object.getDescription(); String example = null; Object obj = object.getExample(); if (obj != null) { example = obj.toString(); } ArraySchema model = new ArraySchema(); model.setDescription(description); model.setName(object.getName()); model.setExample(example); model.setItems(ModelUtils.getAdditionalProperties(object)); return model; }
@Test(description = "vendor extensions should be included with object type") public void testMapDeserializationVendorExtensions() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); MapSchema mp = (MapSchema) responseSchema; assertTrue(mp.getExtensions().size() > 0); assertNotNull(mp.getExtensions().get("x-foo")); assertEquals(mp.getExtensions().get("x-foo"), "vendor x"); }
assertNotNull(items); assertEquals(items.getClass(), MapSchema.class); Schema stringProperty = (Schema)((MapSchema) items).getAdditionalProperties(); assertNotNull(stringProperty); assertEquals(stringProperty.getClass(), StringSchema.class); assertNotNull(items); assertEquals(items.getClass(), MapSchema.class); Schema additionalProperty = (Schema)((MapSchema) items).getAdditionalProperties(); assertNotNull(additionalProperty); assertNotNull(additionalProperty.get$ref());
@Test(description = "it should serialize a integer MapProperty") public void serializeIntegerMapProperty() throws IOException { final Schema p = new MapSchema().additionalProperties(new IntegerSchema()); final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; assertEquals(m.writeValueAsString(p), json); }
private Schema deserializeObjectSchema(JsonNode node) { JsonNode additionalProperties = node.get("additionalProperties"); Schema schema = null; if (additionalProperties != null) { // try first to convert to Schema, if it fails it must be a boolean try { Schema innerSchema = Json.mapper().convertValue(additionalProperties, Schema.class); ((ObjectNode)node).remove("additionalProperties"); MapSchema ms = Json.mapper().convertValue(node, MapSchema.class); ms.setAdditionalProperties(innerSchema); schema = ms; } catch (Exception e) { schema = Json.mapper().convertValue(node, ObjectSchema.class); Boolean additionalPropsBoolean = Json.mapper().convertValue(additionalProperties, Boolean.class); schema.setAdditionalProperties(additionalPropsBoolean); } } else { schema = Json.mapper().convertValue(node, ObjectSchema.class); } return schema; } }
@Override public String toInstantiationType(Schema schema) { if (schema instanceof MapSchema) { MapSchema mapSchema = (MapSchema) schema; String inner = getSchemaType((Schema) mapSchema.getAdditionalProperties()); return inner; } else if (schema instanceof ArraySchema) { ArraySchema arraySchema = (ArraySchema) schema; String inner = getSchemaType(arraySchema.getItems()); return "[" + inner + "]"; } return null; }
@Test(description = "it should serialize a long MapProperty") public void serializeLongMapProperty() throws IOException { final Schema p = new MapSchema().additionalProperties(new IntegerSchema().format("int64")); final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; assertEquals(m.writeValueAsString(p), json); }
Schema schema = new Schema().$ref(existing); schema.setRequired(op.getRequired()); mp.setAdditionalProperties(schema); } else { Schema schema = new Schema().$ref(modelName); schema.setRequired(op.getRequired()); mp.setAdditionalProperties(schema); addGenerated(modelName, innerModel); openapi.getComponents().addSchemas(modelName, innerModel);
@Override public String toInstantiationType(Schema schema) { if (schema instanceof MapSchema) { MapSchema mapSchema = (MapSchema) schema; String inner = getSchemaType((Schema) mapSchema.getAdditionalProperties()); return inner; } else if (schema instanceof ArraySchema) { ArraySchema arraySchema = (ArraySchema) schema; String inner = getSchemaType(arraySchema.getItems()); return "[" + inner + "]"; } return null; }
schema = new Schema().$ref(StringUtils.isNotEmpty(schema.get$ref()) ? schema.get$ref() : schema.getName()); return new MapSchema().additionalProperties(schema);
@Override public String getTypeDeclaration(Schema propertySchema) { if (propertySchema instanceof ArraySchema) { ArraySchema arraySchema = (ArraySchema) propertySchema; Schema inner = arraySchema.getItems(); return "[" + getTypeDeclaration(inner) + "]"; } else if (propertySchema instanceof MapSchema) { MapSchema mapSchema = (MapSchema) propertySchema; Schema inner = (Schema) mapSchema.getAdditionalProperties(); return "[String:" + getTypeDeclaration(inner) + "]"; } return super.getTypeDeclaration(propertySchema); }
Schema mapModel = new MapSchema().additionalProperties(addPropertiesSchema); mapModel.name(name); model = mapModel;
@Override public String getTypeDeclaration(Schema propertySchema) { if (propertySchema instanceof ArraySchema) { ArraySchema arraySchema = (ArraySchema) propertySchema; Schema inner = arraySchema.getItems(); return "[" + getTypeDeclaration(inner) + "]"; } else if (propertySchema instanceof MapSchema) { MapSchema mapSchema = (MapSchema) propertySchema; Schema inner = (Schema) mapSchema.getAdditionalProperties(); return "[String:" + getTypeDeclaration(inner) + "]"; } return super.getTypeDeclaration(propertySchema); }
Schema mapModel = new MapSchema().additionalProperties(addPropertiesSchema); mapModel.name(name); model = mapModel;