
io.github.emm035.openapi.immutables.v3.responses.ResponsesDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-immutables Show documentation
Show all versions of openapi-immutables Show documentation
An implementation of the OpenAPI 3.0 specification with Java Immutables
The newest version!
package io.github.emm035.openapi.immutables.v3.responses;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.primitives.Ints;
import io.github.emm035.openapi.immutables.v3.references.Referenceable;
import java.io.IOException;
import java.util.Iterator;
public class ResponsesDeserializer extends JsonDeserializer {
@Override
public Responses deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) p.getCodec();
JsonNode node = mapper.readTree(p);
Responses.Builder builder = Responses.builder();
for (Iterator iter = node.fieldNames(); iter.hasNext();) {
String fieldName = iter.next();
if (fieldName.startsWith("x-")) {
builder.putExtensions(fieldName, mapper.treeToValue(node.get(fieldName), Object.class));
} else if (fieldName.equals("default")) {
Referenceable defaultResponse = mapper.readValue(mapper.treeAsTokens(node.get(fieldName)), new TypeReference>() {});
builder.setDefault(defaultResponse);
} else {
Referenceable response = mapper.readValue(mapper.treeAsTokens(node.get(fieldName)), new TypeReference>() {});
builder.putResponses(Ints.tryParse(fieldName), response);
}
}
return builder.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy