io.swagger.v3.core.util.ApiResponsesDeserializer Maven / Gradle / Ivy
package io.swagger.v3.core.util;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class ApiResponsesDeserializer extends JsonDeserializer {
@Override
public ApiResponses deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
ApiResponses result = new ApiResponses();
JsonNode node = jp.getCodec().readTree(jp);
ObjectNode objectNode = (ObjectNode)node;
Map extensions = new LinkedHashMap<>();
for (Iterator it = objectNode.fieldNames(); it.hasNext(); ) {
String childName = it.next();
JsonNode child = objectNode.get(childName);
// if name start with `x-` consider it an extesion
if (childName.startsWith("x-")) {
extensions.put(childName, Json.mapper().convertValue(child, Object.class));
} else {
result.put(childName, Json.mapper().convertValue(child, ApiResponse.class));
}
}
if (!extensions.isEmpty()) {
result.setExtensions(extensions);
}
return result;
}
}