com.networknt.openapi.parameter.ParameterDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-meta Show documentation
Show all versions of openapi-meta Show documentation
An OpenAPI Specification 3.0 meta handler module that cache the spec and attached context related operation to the request based on uri and method.
package com.networknt.openapi.parameter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.networknt.oas.model.Parameter;
import com.networknt.openapi.OpenApiOperation;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.AttachmentKey;
public interface ParameterDeserializer {
static Set getCandidateQueryParams(HttpServerExchange exchange){
Set candidateQueryParams = new HashSet<>();
exchange.getQueryParameters().keySet().forEach(key->{
if (!key.contains(Delimiters.LEFT_BRACKET)) {
candidateQueryParams.add(key);
}else {// for deepObject serialization
candidateQueryParams.add(key.substring(0, key.indexOf(Delimiters.LEFT_BRACKET)));
}
});
return candidateQueryParams;
}
static void deserialize(HttpServerExchange exchange, OpenApiOperation openApiOperation) {
Set candidateQueryParams = getCandidateQueryParams(exchange);
Set candidatePathParams = exchange.getPathParameters().keySet();
Set candidateHeaderParams = exchange.getRequestHeaders().getHeaderNames().stream().map(name->name.toString()).collect(Collectors.toSet());
Set candidateCookieParams = exchange.getRequestCookies().keySet();
openApiOperation.getOperation().getParameters().forEach(p->{
ParameterType type = ParameterType.of(p.getIn());
if (null!=type) {
ParameterDeserializer deserializer = type.getDeserializer();
switch(type){
case QUERY:
deserializer.deserialize(exchange, p, candidateQueryParams);
break;
case PATH:
deserializer.deserialize(exchange, p, candidatePathParams);
break;
case HEADER:
deserializer.deserialize(exchange, p, candidateHeaderParams);
break;
case COOKIE:
deserializer.deserialize(exchange, p, candidateCookieParams);
break;
}
}
});
}
default boolean isApplicable(HttpServerExchange exchange, Parameter parameter, Set candidateParams) {
// HTTP header names are case insensitive (RFC 7230, https://tools.ietf.org/html/rfc7230#section-3.2)
if(ParameterType.of(parameter.getIn()) == ParameterType.HEADER)
return candidateParams.stream().anyMatch(s->parameter.getName().equalsIgnoreCase(s));
else
return candidateParams.contains(parameter.getName());
}
default void deserialize(HttpServerExchange exchange, Parameter parameter, Set candidateParams) {
if (!isApplicable(exchange, parameter, candidateParams)) {
return;
}
StyleParameterDeserializer deserializer = getStyleDeserializer(parameter.getStyle());
if (null==deserializer) {
return;
}
Object valueObj = deserializer.deserialize(exchange, parameter);
if (null!=valueObj) {
attach(exchange, parameter.getName(), valueObj);
}
}
StyleParameterDeserializer getStyleDeserializer(String style);
default AttachmentKey