Search in sources :

Example 46 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getHeader.

public Header getHeader(ObjectNode headerNode, String location, ParseResult result) {
    if (headerNode == null) {
        return null;
    }
    Header header = new Header();
    JsonNode ref = headerNode.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                header.set$ref(mungedRef);
            } else {
                header.set$ref(ref.textValue());
            }
            return header;
        } else {
            result.invalidType(location, "$ref", "string", headerNode);
            return null;
        }
    }
    String value = getString("description", headerNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        header.setDescription(value);
    }
    Boolean required = getBoolean("required", headerNode, false, location, result);
    if (required != null) {
        header.setRequired(required);
    }
    Boolean deprecated = getBoolean("deprecated", headerNode, false, location, result);
    if (deprecated != null) {
        header.setDeprecated(deprecated);
    }
    Boolean explode = getBoolean("explode", headerNode, false, location, result);
    if (explode != null) {
        header.setExplode(explode);
    } else {
        header.setExplode(Boolean.FALSE);
    }
    header.setStyle(Header.StyleEnum.SIMPLE);
    ObjectNode headerObject = getObject("schema", headerNode, false, location, result);
    if (headerObject != null) {
        header.setSchema(getSchema(headerObject, location, result));
    }
    ObjectNode examplesObject = getObject("examples", headerNode, false, location, result);
    if (examplesObject != null) {
        header.setExamples(getExamples(examplesObject, location, result, false));
    }
    Object example = getAnyExample("example", headerNode, location, result);
    if (example != null) {
        if (examplesObject != null) {
            result.warning(location, "examples already defined -- ignoring \"example\" field");
        } else {
            header.setExample(example instanceof NullNode ? null : example);
        }
    }
    ObjectNode contentNode = getObject("content", headerNode, false, location, result);
    if (contentNode != null) {
        header.setContent(getContent(contentNode, String.format("%s.%s", location, "content"), result));
    }
    Map<String, Object> extensions = getExtensions(headerNode);
    if (extensions != null && extensions.size() > 0) {
        header.setExtensions(extensions);
    }
    Set<String> oAuthFlowKeys = getKeys(headerNode);
    for (String key : oAuthFlowKeys) {
        if (!HEADER_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, headerNode.get(key));
        }
    }
    return header;
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) NullNode(com.fasterxml.jackson.databind.node.NullNode)

Example 47 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class HeaderProcessor method processHeader.

public void processHeader(Header header) {
    if (header.get$ref() != null) {
        RefFormat refFormat = computeRefFormat(header.get$ref());
        String $ref = header.get$ref();
        if (isAnExternalRefFormat(refFormat)) {
            final String newRef = externalRefProcessor.processRefToExternalHeader($ref, refFormat);
            if (newRef != null) {
                header.set$ref(newRef);
            }
        }
    }
    if (header.getSchema() != null) {
        schemaProcessor.processSchema(header.getSchema());
    }
    if (header.getExamples() != null) {
        if (header.getExamples() != null) {
            Map<String, Example> examples = header.getExamples();
            for (String key : examples.keySet()) {
                exampleProcessor.processExample(header.getExamples().get(key));
            }
        }
    }
    Schema schema = null;
    if (header.getContent() != null) {
        Map<String, MediaType> content = header.getContent();
        for (String mediaName : content.keySet()) {
            MediaType mediaType = content.get(mediaName);
            if (mediaType.getSchema() != null) {
                schema = mediaType.getSchema();
                if (schema != null) {
                    schemaProcessor.processSchema(schema);
                }
            }
        }
    }
}
Also used : Example(io.swagger.v3.oas.models.examples.Example) Schema(io.swagger.v3.oas.models.media.Schema) RefFormat(io.swagger.v3.parser.models.RefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) MediaType(io.swagger.v3.oas.models.media.MediaType)

Example 48 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processRefToExternalHeader.

public String processRefToExternalHeader(String $ref, RefFormat refFormat) {
    String renamedRef = cache.getRenamedRef($ref);
    if (renamedRef != null) {
        return renamedRef;
    }
    final Header header = cache.loadRef($ref, refFormat, Header.class);
    if (header == null) {
        // stop!  There's a problem.  retain the original ref
        LOGGER.warn("unable to load model reference from `" + $ref + "`.  It may not be available " + "or the reference isn't a valid model schema");
        return $ref;
    }
    String newRef;
    if (openAPI.getComponents() == null) {
        openAPI.setComponents(new Components());
    }
    Map<String, Header> headers = openAPI.getComponents().getHeaders();
    if (headers == null) {
        headers = new LinkedHashMap<>();
    }
    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
    Header existingHeader = headers.get(possiblyConflictingDefinitionName);
    if (existingHeader != null) {
        LOGGER.debug("A model for " + existingHeader + " already exists");
        if (existingHeader.get$ref() != null) {
            // use the new model
            existingHeader = null;
        }
    }
    newRef = possiblyConflictingDefinitionName;
    cache.putRenamedRef($ref, newRef);
    if (existingHeader == null) {
        // don't overwrite existing model reference
        openAPI.getComponents().addHeaders(newRef, header);
        cache.addReferencedKey(newRef);
        String file = $ref.split("#/")[0];
        if (header.get$ref() != null) {
            RefFormat format = computeRefFormat(header.get$ref());
            if (isAnExternalRefFormat(format)) {
                header.set$ref(processRefToExternalHeader(header.get$ref(), format));
            } else {
                processRefToExternalHeader(file + header.get$ref(), RefFormat.RELATIVE);
            }
        }
    }
    if (header != null) {
        if (header.getContent() != null) {
            processRefContent(header.getContent(), $ref);
        }
        if (header.getSchema() != null) {
            processRefSchemaObject(header.getSchema(), $ref);
        }
    }
    return newRef;
}
Also used : Components(io.swagger.v3.oas.models.Components) Header(io.swagger.v3.oas.models.headers.Header) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

Example 49 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class ResolverFully method resolveHeaders.

private void resolveHeaders(Map<String, Header> headers) {
    if (headers == null || headers.isEmpty()) {
        return;
    }
    for (Map.Entry<String, Header> header : headers.entrySet()) {
        Header value = header.getValue();
        Header resolvedValue = value.get$ref() != null ? resolveHeader(value) : value;
        Map<String, Example> examples = resolvedValue.getExamples();
        if (examples != null) {
            Map<String, Example> resolved = resolveExample(examples);
            resolvedValue.setExamples(resolved);
        }
        Schema schema = resolvedValue.getSchema();
        if (schema != null) {
            Schema resolvedSchema = resolveSchema(schema);
            if (resolvedSchema != null) {
                resolvedValue.setSchema(resolvedSchema);
            }
        }
        header.setValue(resolvedValue);
    }
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) Example(io.swagger.v3.oas.models.examples.Example) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 50 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class ResponseProcessorTest method testProcessResponse.

@Test
public void testProcessResponse(@Injectable final Schema responseSchema, @Injectable final Header responseHeader) throws Exception {
    new StrictExpectations() {

        {
            new SchemaProcessor(cache, swagger);
            times = 1;
            result = propertyProcessor;
            new HeaderProcessor(cache, swagger);
            times = 1;
            result = headerProcessor;
            new LinkProcessor(cache, swagger);
            times = 1;
            result = linkProcessor;
            propertyProcessor.processSchema(responseSchema);
            times = 1;
            headerProcessor.processHeader(responseHeader);
            times = 1;
        }
    };
    ApiResponse response = new ApiResponse();
    response.content(new Content().addMediaType("*/*", new MediaType().schema(responseSchema)));
    response.addHeaderObject("foo", responseHeader);
    new ResponseProcessor(cache, swagger).processResponse(response);
    new FullVerifications() {

        {
        }
    };
}
Also used : StrictExpectations(mockit.StrictExpectations) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) FullVerifications(mockit.FullVerifications) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)32 OpenAPI (io.swagger.v3.oas.models.OpenAPI)26 Header (io.swagger.v3.oas.models.headers.Header)25 Parameter (io.swagger.v3.oas.models.parameters.Parameter)16 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)13 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)11 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)11 Schema (io.swagger.v3.oas.models.media.Schema)10 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)10 MediaType (io.swagger.v3.oas.models.media.MediaType)8 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)8 Components (io.swagger.v3.oas.models.Components)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 StringSchema (io.swagger.v3.oas.models.media.StringSchema)6 AuthorizationValue (io.swagger.v3.parser.core.models.AuthorizationValue)6 PathItem (io.swagger.v3.oas.models.PathItem)5 Example (io.swagger.v3.oas.models.examples.Example)5 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)5 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)5 RefFormat (io.swagger.v3.parser.models.RefFormat)5