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;
}
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);
}
}
}
}
}
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;
}
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);
}
}
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() {
{
}
};
}
Aggregations