Search in sources :

Example 1 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.

the class AnnotationsUtils method getExtensions.

public static Map<String, Object> getExtensions(Extension... extensions) {
    final Map<String, Object> map = new HashMap<>();
    for (Extension extension : extensions) {
        final String name = extension.name();
        final String key = name.length() > 0 ? StringUtils.prependIfMissing(name, "x-") : name;
        for (ExtensionProperty property : extension.properties()) {
            final String propertyName = property.name();
            final String propertyValue = property.value();
            JsonNode processedValue = null;
            final boolean propertyAsJson = property.parseValue();
            if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
                if (key.isEmpty()) {
                    if (propertyAsJson) {
                        try {
                            processedValue = Json.mapper().readTree(propertyValue);
                            map.put(StringUtils.prependIfMissing(propertyName, "x-"), processedValue);
                        } catch (Exception e) {
                            map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
                        }
                    } else {
                        map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
                    }
                } else {
                    Object value = map.get(key);
                    if (!(value instanceof Map)) {
                        value = new HashMap<String, Object>();
                        map.put(key, value);
                    }
                    @SuppressWarnings("unchecked") final Map<String, Object> mapValue = (Map<String, Object>) value;
                    if (propertyAsJson) {
                        try {
                            processedValue = Json.mapper().readTree(propertyValue);
                            mapValue.put(propertyName, processedValue);
                        } catch (Exception e) {
                            mapValue.put(propertyName, propertyValue);
                        }
                    } else {
                        mapValue.put(propertyName, propertyValue);
                    }
                }
            }
        }
    }
    return map;
}
Also used : Extension(io.swagger.v3.oas.annotations.extensions.Extension) ExtensionProperty(io.swagger.v3.oas.annotations.extensions.ExtensionProperty) HashMap(java.util.HashMap) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException)

Example 2 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.

the class ApiResponsesDeserializer method deserialize.

@Override
public ApiResponses deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    final ObjectMapper mapper;
    if (openapi31) {
        mapper = Json31.mapper();
    } else {
        mapper = Json.mapper();
    }
    ApiResponses result = new ApiResponses();
    JsonNode node = jp.getCodec().readTree(jp);
    ObjectNode objectNode = (ObjectNode) node;
    Map<String, Object> extensions = new LinkedHashMap<>();
    for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
        String childName = it.next();
        JsonNode child = objectNode.get(childName);
        // if name start with `x-` consider it an extension
        if (childName.startsWith("x-")) {
            extensions.put(childName, mapper.convertValue(child, Object.class));
        } else {
            result.put(childName, mapper.convertValue(child, ApiResponse.class));
        }
    }
    if (!extensions.isEmpty()) {
        result.setExtensions(extensions);
    }
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.

the class CallbackDeserializer method deserialize.

@Override
public Callback deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    final ObjectMapper mapper;
    if (openapi31) {
        mapper = Json31.mapper();
    } else {
        mapper = Json.mapper();
    }
    Callback result = new Callback();
    JsonNode node = jp.getCodec().readTree(jp);
    ObjectNode objectNode = (ObjectNode) node;
    Map<String, Object> extensions = new LinkedHashMap<>();
    for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
        String childName = it.next();
        JsonNode child = objectNode.get(childName);
        // if name start with `x-` consider it an extension
        if (childName.startsWith("x-")) {
            extensions.put(childName, mapper.convertValue(child, Object.class));
        } else if (childName.equals("$ref")) {
            result.$ref(child.asText());
        } else {
            result.put(childName, mapper.convertValue(child, PathItem.class));
        }
    }
    if (!extensions.isEmpty()) {
        result.setExtensions(extensions);
    }
    return result;
}
Also used : Callback(io.swagger.v3.oas.models.callbacks.Callback) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.

the class ModelConverterContextImpl method resolve.

@Override
public Schema resolve(AnnotatedType type) {
    AnnotatedType aType = OptionalUtils.unwrapOptional(type);
    if (aType != null) {
        return resolve(aType);
    }
    if (processedTypes.contains(type)) {
        return modelByType.get(type);
    } else {
        processedTypes.add(type);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("resolve %s", type.getType()));
    }
    Iterator<ModelConverter> converters = this.getConverters();
    Schema resolved = null;
    if (converters.hasNext()) {
        ModelConverter converter = converters.next();
        LOGGER.trace("trying extension {}", converter);
        resolved = converter.resolve(type, this, converters);
    }
    if (resolved != null) {
        modelByType.put(type, resolved);
        Schema resolvedImpl = resolved;
        if (resolvedImpl.getName() != null) {
            modelByName.put(resolvedImpl.getName(), resolved);
        }
    } else {
        processedTypes.remove(type);
    }
    return resolved;
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema)

Example 5 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.

the class ReaderTest method testRequestBodyExtensions.

@Test(description = "RequestBody Tests")
public void testRequestBodyExtensions() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(RequestBodyExtensionsResource.class);
    assertNotNull(openAPI);
    Map<String, Object> extensions = openAPI.getPaths().get("/user").getGet().getRequestBody().getExtensions();
    assertNotNull(extensions);
    assertEquals(extensions.size(), 2);
    assertNotNull(extensions.get("x-extension"));
    assertNotNull(extensions.get("x-extension2"));
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)17 Test (org.testng.annotations.Test)11 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)9 Operation (io.swagger.v3.oas.models.Operation)8 HashMap (java.util.HashMap)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)8 PathItem (io.swagger.v3.oas.models.PathItem)7 Schema (io.swagger.v3.oas.models.media.Schema)6 Paths (io.swagger.v3.oas.models.Paths)5 ArrayList (java.util.ArrayList)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 HttpMethod (io.swagger.models.HttpMethod)4 Components (io.swagger.v3.oas.models.Components)4 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)4 JSONObject (org.json.simple.JSONObject)4 Info (io.swagger.v3.oas.models.info.Info)3 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)3 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)3 MediaType (io.swagger.v3.oas.models.media.MediaType)3