io.swagger.v3.core.util.PathsDeserializer 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.PathItem;
import io.swagger.v3.oas.models.Paths;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class PathsDeserializer extends JsonDeserializer {
@Override
public Paths deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
Paths result = new Paths();
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, PathItem.class));
}
}
if (!extensions.isEmpty()) {
result.setExtensions(extensions);
}
return result;
}
}