All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.networknt.openapi.parameter.ParameterDeserializer Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.1.37
Show newest version
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> getAttachmentKey(){
		return null;
	}
	
	default void attach(HttpServerExchange exchange, String key, Object value) {
		AttachmentKey> paramType = getAttachmentKey();
		
		if (null!=paramType) {
			attach(exchange, paramType, key, value);
		}
	}
	
	default void attach(HttpServerExchange exchange, AttachmentKey> paramType, String key, Object value) {
		Map paramMap = exchange.getAttachment(paramType);
		
		if (null == paramMap) {
			exchange.putAttachment(paramType, paramMap=new HashMap<>());
		}
		
		paramMap.put(key, value);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy