use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class AnnotationsUtils method getExtensions.
public static Map<String, Object> getExtensions(Extension... extensions) {
final Map<String, Object> map = new HashMap<>();
for (Extension extension : extensions) {
final String name = extension.name();
final String key = name.length() > 0 ? StringUtils.prependIfMissing(name, "x-") : name;
for (ExtensionProperty property : extension.properties()) {
final String propertyName = property.name();
final String propertyValue = property.value();
JsonNode processedValue = null;
final boolean propertyAsJson = property.parseValue();
if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
if (key.isEmpty()) {
if (propertyAsJson) {
try {
processedValue = Json.mapper().readTree(propertyValue);
map.put(StringUtils.prependIfMissing(propertyName, "x-"), processedValue);
} catch (Exception e) {
map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
}
} else {
map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
}
} else {
Object value = map.get(key);
if (!(value instanceof Map)) {
value = new HashMap<String, Object>();
map.put(key, value);
}
@SuppressWarnings("unchecked") final Map<String, Object> mapValue = (Map<String, Object>) value;
if (propertyAsJson) {
try {
processedValue = Json.mapper().readTree(propertyValue);
mapValue.put(propertyName, processedValue);
} catch (Exception e) {
mapValue.put(propertyName, propertyValue);
}
} else {
mapValue.put(propertyName, propertyValue);
}
}
}
}
}
return map;
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class ApiResponsesDeserializer method deserialize.
@Override
public ApiResponses deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
final ObjectMapper mapper;
if (openapi31) {
mapper = Json31.mapper();
} else {
mapper = Json.mapper();
}
ApiResponses result = new ApiResponses();
JsonNode node = jp.getCodec().readTree(jp);
ObjectNode objectNode = (ObjectNode) node;
Map<String, Object> extensions = new LinkedHashMap<>();
for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
String childName = it.next();
JsonNode child = objectNode.get(childName);
// if name start with `x-` consider it an extension
if (childName.startsWith("x-")) {
extensions.put(childName, mapper.convertValue(child, Object.class));
} else {
result.put(childName, mapper.convertValue(child, ApiResponse.class));
}
}
if (!extensions.isEmpty()) {
result.setExtensions(extensions);
}
return result;
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class CallbackDeserializer method deserialize.
@Override
public Callback deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
final ObjectMapper mapper;
if (openapi31) {
mapper = Json31.mapper();
} else {
mapper = Json.mapper();
}
Callback result = new Callback();
JsonNode node = jp.getCodec().readTree(jp);
ObjectNode objectNode = (ObjectNode) node;
Map<String, Object> extensions = new LinkedHashMap<>();
for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
String childName = it.next();
JsonNode child = objectNode.get(childName);
// if name start with `x-` consider it an extension
if (childName.startsWith("x-")) {
extensions.put(childName, mapper.convertValue(child, Object.class));
} else if (childName.equals("$ref")) {
result.$ref(child.asText());
} else {
result.put(childName, mapper.convertValue(child, PathItem.class));
}
}
if (!extensions.isEmpty()) {
result.setExtensions(extensions);
}
return result;
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class ModelConverterContextImpl method resolve.
@Override
public Schema resolve(AnnotatedType type) {
AnnotatedType aType = OptionalUtils.unwrapOptional(type);
if (aType != null) {
return resolve(aType);
}
if (processedTypes.contains(type)) {
return modelByType.get(type);
} else {
processedTypes.add(type);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("resolve %s", type.getType()));
}
Iterator<ModelConverter> converters = this.getConverters();
Schema resolved = null;
if (converters.hasNext()) {
ModelConverter converter = converters.next();
LOGGER.trace("trying extension {}", converter);
resolved = converter.resolve(type, this, converters);
}
if (resolved != null) {
modelByType.put(type, resolved);
Schema resolvedImpl = resolved;
if (resolvedImpl.getName() != null) {
modelByName.put(resolvedImpl.getName(), resolved);
}
} else {
processedTypes.remove(type);
}
return resolved;
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class ReaderTest method testRequestBodyExtensions.
@Test(description = "RequestBody Tests")
public void testRequestBodyExtensions() {
Reader reader = new Reader(new OpenAPI());
OpenAPI openAPI = reader.read(RequestBodyExtensionsResource.class);
assertNotNull(openAPI);
Map<String, Object> extensions = openAPI.getPaths().get("/user").getGet().getRequestBody().getExtensions();
assertNotNull(extensions);
assertEquals(extensions.size(), 2);
assertNotNull(extensions.get("x-extension"));
assertNotNull(extensions.get("x-extension2"));
}
Aggregations