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

com.networknt.openapi.parameter.StyleParameterDeserializer 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.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.networknt.oas.model.Parameter;
import com.networknt.oas.model.Schema;
import com.networknt.utility.StringUtils;

import io.undertow.server.HttpServerExchange;

public interface StyleParameterDeserializer {
	static ValueType getValueType(Parameter parameter) {
		Schema schema = parameter.getSchema();
		
		if (null!=schema) {
			return ValueType.of(schema.getType());
		}
		
		return null;
	}
	
	default Object deserialize(HttpServerExchange exchange, Parameter parameter) {
		ValueType valueType = getValueType(parameter);
		boolean exploade = parameter.isExplode();
		
		if (!isApplicable(valueType, exploade)) {
			return null;
		}
		
		return deserialize(exchange, parameter, valueType, exploade);
	}
	
	Object deserialize(HttpServerExchange exchange, Parameter parameter, ValueType valueType, boolean exploade);
	
	default boolean isApplicable(ValueType valueType, boolean exploade) {
		if (null == valueType || ValueType.PRIMITIVE == valueType) {
			return false;
		}
		
		return true;
	}
	
	default String getFirst(Deque values, String key) {
		if (null!=values) {
			return StringUtils.trimToEmpty(values.peekFirst());
		}
		
		return StringUtils.EMPTY;
	}
	
	default List asList(String str, String delimiter) {
		List valueList = new ArrayList<>();
		
		if (!str.contains(delimiter)) {
			valueList.add(str);
		}else {
			String[] items = str.split("\\"+delimiter);
			
			for (String item: items) {
				valueList.add(item);
			}
		}
		
		return valueList;
	}
	
	default Map asExploadeMap(String str, String delimiter) {
		Map valueMap = new HashMap<>();
		String[] items = str.split("\\"+delimiter);
		
		for (String item: items) {
			String[] tokens = item.split(Delimiters.EQUAL);
			
			String key=null;
			String value=null;
			
			if (tokens.length>0) {
				key = tokens[0];
			}
			
			if (tokens.length>1) {
				value = tokens[1];
			}
			
			if (StringUtils.isNotBlank(key)) {
				valueMap.put(key, StringUtils.trimToEmpty(value));
			}
		}
		
		return valueMap;
	}
	
	default Map asMap(String str, String delimiter) {
		if (StringUtils.isBlank(str)) {
			return Collections.emptyMap();
		}
		
		Map valueMap = new HashMap<>();
		
		if (!str.contains(delimiter)) {
			valueMap.put(str, StringUtils.EMPTY);
		}else {
			String[] items = str.split("\\"+delimiter);
			
			int len = items.length/2;
			
			int keyIndex = 0;
			int valueIndex = 0;
			
			for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy