use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project candlepin by candlepin.
the class CandlepinSwaggerModelConverter method resolve.
public Model resolve(Type rawType, ModelConverterContext context, Iterator<ModelConverter> next) {
if (this.shouldIgnoreClass(rawType)) {
return null;
}
/**
* See java doc of NestedComplexType. This unwrapping makes sure that a
* real type of field used throughout the method. At the same time flag
* 'isNested' helps to indicate later in the method that this type may
* be introspected as Hateoas enabled field
*/
boolean isNested = false;
if (rawType instanceof NestedComplexType) {
isNested = true;
NestedComplexType nested = (NestedComplexType) rawType;
rawType = nested.getOriginalType();
}
JavaType type = pMapper.constructType(rawType);
if (type.isEnumType() || PrimitiveType.fromType(type) != null) {
// We don't build models for primitive types
return null;
}
final BeanDescription beanDesc = pMapper.getSerializationConfig().introspect(type);
// Couple of possibilities for defining
String name = isNested ? "Nested" : "";
name += pTypeName(type, beanDesc);
if ("Object".equals(name)) {
return new ModelImpl();
}
final ModelImpl model = new ModelImpl().type(ModelImpl.OBJECT).name(name).description(pDescription(beanDesc.getClassInfo()));
if (!type.isContainerType()) {
// define the model here to support self/cyclic referencing of
// models
context.defineModel(name, model, type, null);
}
if (type.isContainerType()) {
// We treat collections as primitive types, just need to add models
// for values (if any)
context.resolve(type.getContentType());
return null;
}
// if XmlRootElement annotation, construct an Xml object and attach it
// to the model
XmlRootElement rootAnnotation = beanDesc.getClassAnnotations().get(XmlRootElement.class);
if (rootAnnotation != null && !"".equals(rootAnnotation.name()) && !"##default".equals(rootAnnotation.name())) {
log.debug(rootAnnotation.toString());
Xml xml = new Xml().name(rootAnnotation.name());
if (rootAnnotation.namespace() != null && !"".equals(rootAnnotation.namespace()) && !"##default".equals(rootAnnotation.namespace())) {
xml.namespace(rootAnnotation.namespace());
}
model.xml(xml);
}
// see if @JsonIgnoreProperties exist
Set<String> propertiesToIgnore = new HashSet<>();
JsonIgnoreProperties ignoreProperties = beanDesc.getClassAnnotations().get(JsonIgnoreProperties.class);
if (ignoreProperties != null) {
propertiesToIgnore.addAll(Arrays.asList(ignoreProperties.value()));
}
final ApiModel apiModel = beanDesc.getClassAnnotations().get(ApiModel.class);
String disc = (apiModel == null) ? "" : apiModel.discriminator();
if (apiModel != null && StringUtils.isNotEmpty(apiModel.reference())) {
model.setReference(apiModel.reference());
}
if (disc.isEmpty()) {
// longer method would involve
// AnnotationIntrospector.findTypeResolver(...) but:
JsonTypeInfo typeInfo = beanDesc.getClassAnnotations().get(JsonTypeInfo.class);
if (typeInfo != null) {
disc = typeInfo.property();
}
}
if (!disc.isEmpty()) {
model.setDiscriminator(disc);
}
List<Property> props = new ArrayList<>();
for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
parseProperty(context, isNested, beanDesc, propertiesToIgnore, props, propDef);
}
Collections.sort(props, getPropertyComparator());
Map<String, Property> modelProps = new LinkedHashMap<>();
for (Property prop : props) {
modelProps.put(prop.getName(), prop);
}
model.setProperties(modelProps);
/**
* This must be done after model.setProperties so that the model's set
* of properties is available to filter from any subtypes
*/
if (!resolveSubtypes(model, beanDesc, context)) {
model.setDiscriminator(null);
}
return model;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project candlepin by candlepin.
the class CandlepinSwaggerModelConverter method resolveProperty.
public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> next) {
JavaType propType = null;
/**
* See java doc of NestedComplexType. This unwrapping makes sure that a
* real type of field is passed to _mapper
*/
if (type instanceof NestedComplexType) {
propType = pMapper.constructType(((NestedComplexType) type).getOriginalType());
} else {
propType = pMapper.constructType(type);
}
log.debug("resolveProperty {}", propType);
Property property = null;
if (propType.isContainerType()) {
JavaType keyType = propType.getKeyType();
JavaType valueType = propType.getContentType();
if (keyType != null && valueType != null) {
property = new MapProperty().additionalProperties(context.resolveProperty(valueType, new Annotation[] {}));
} else if (valueType != null) {
ArrayProperty arrayProperty = new ArrayProperty().items(context.resolveProperty(valueType, new Annotation[] {}));
if (pIsSetType(propType.getRawClass())) {
arrayProperty.setUniqueItems(true);
}
property = arrayProperty;
}
} else {
property = PrimitiveType.createProperty(propType);
}
if (property == null) {
if (propType.isEnumType()) {
property = new StringProperty();
pAddEnumProps(propType.getRawClass(), property);
} else if (pIsOptionalType(propType)) {
property = context.resolveProperty(propType.containedType(0), null);
} else {
// complex type
Model innerModel = context.resolve(type);
if (innerModel instanceof ModelImpl) {
ModelImpl mi = (ModelImpl) innerModel;
property = new RefProperty(StringUtils.isNotEmpty(mi.getReference()) ? mi.getReference() : mi.getName());
}
}
}
return property;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project candlepin by candlepin.
the class CandlepinSwaggerModelConverter method resolveApiAnnotated.
private Property resolveApiAnnotated(ModelConverterContext context, Property property, Annotation[] annotations, ApiModelProperty mp, JavaType propType) {
String or = mp.dataType();
JavaType innerJavaType = null;
log.debug("overriding datatype from {} to {}", propType, or);
if (or.toLowerCase().startsWith("list[")) {
String innerType = or.substring(5, or.length() - 1);
ArrayProperty p = new ArrayProperty();
Property primitiveProperty = PrimitiveType.createProperty(innerType);
if (primitiveProperty != null) {
p.setItems(primitiveProperty);
} else {
innerJavaType = getInnerType(innerType);
p.setItems(context.resolveProperty(innerJavaType, annotations));
}
property = p;
} else if (or.toLowerCase().startsWith("map[")) {
int pos = or.indexOf(",");
if (pos > 0) {
String innerType = or.substring(pos + 1, or.length() - 1);
MapProperty p = new MapProperty();
Property primitiveProperty = PrimitiveType.createProperty(innerType);
if (primitiveProperty != null) {
p.setAdditionalProperties(primitiveProperty);
} else {
innerJavaType = getInnerType(innerType);
p.setAdditionalProperties(context.resolveProperty(innerJavaType, annotations));
}
property = p;
}
} else {
Property primitiveProperty = PrimitiveType.createProperty(or);
if (primitiveProperty != null) {
property = primitiveProperty;
} else {
innerJavaType = getInnerType(or);
property = context.resolveProperty(innerJavaType, annotations);
}
}
if (innerJavaType != null) {
context.resolve(innerJavaType);
}
return property;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project candlepin by candlepin.
the class CandlepinSwaggerModelConverter method parseProperty.
private void parseProperty(ModelConverterContext context, boolean isNested, final BeanDescription beanDesc, Set<String> propertiesToIgnore, List<Property> props, BeanPropertyDefinition propDef) {
Property property = null;
String propName = propDef.getName();
Annotation[] annotations = null;
propName = getPropName(propDef, propName);
PropertyMetadata md = propDef.getMetadata();
boolean hasSetter = false, hasGetter = false;
if (propDef.getSetter() == null) {
hasSetter = false;
} else {
hasSetter = true;
}
if (propDef.getGetter() != null) {
JsonProperty pd = propDef.getGetter().getAnnotation(JsonProperty.class);
if (pd != null) {
hasGetter = true;
}
}
Boolean isReadOnly = null;
if (!hasSetter & hasGetter) {
isReadOnly = Boolean.TRUE;
} else {
isReadOnly = Boolean.FALSE;
}
final AnnotatedMember member = propDef.getPrimaryMember();
if (member != null && !propertiesToIgnore.contains(propName) && /**
* If the owning type is nested than we should include only those
* fields that have the Hateoas annotation.
*/
!(isNested && !member.hasAnnotation(HateoasInclude.class))) {
List<Annotation> annotationList = new ArrayList<>();
for (Annotation a : member.annotations()) {
annotationList.add(a);
}
annotations = annotationList.toArray(new Annotation[annotationList.size()]);
ApiModelProperty mp = member.getAnnotation(ApiModelProperty.class);
if (mp != null && mp.readOnly()) {
isReadOnly = mp.readOnly();
}
Type nested = null;
JavaType propType = member.getType(beanDesc.bindingsForBeanType());
JsonFilter jsonFilter = propType.getRawClass().getAnnotation(JsonFilter.class);
/**
* At this point the propType is a type of some nested field of the
* type that is being processed. The condition checks if this
* particular type should have Hateoas serialization enabled. In
* other words, if we should create a new Nested* model.
*/
if (jsonFilter != null && (jsonFilter.value().equals("ConsumerFilter") || jsonFilter.value().equals("EntitlementFilter") || jsonFilter.value().equals("OwnerFilter") || jsonFilter.value().equals("GuestFilter"))) {
if (!nestedJavaTypes.containsKey(propType)) {
nestedJavaTypes.put(propType, new NestedComplexType(propType));
}
nested = nestedJavaTypes.get(propType);
} else {
nested = propType;
}
// allow override of name from annotation
if (mp != null && !mp.name().isEmpty()) {
propName = mp.name();
}
if (mp != null && !mp.dataType().isEmpty()) {
property = resolveApiAnnotated(context, property, annotations, mp, propType);
}
// no property from override, construct from propType
if (property == null) {
if (mp != null && StringUtils.isNotEmpty(mp.reference())) {
property = new RefProperty(mp.reference());
} else if (member.getAnnotation(JsonIdentityInfo.class) != null) {
property = GeneratorWrapper.processJsonIdentity(propType, context, pMapper, member.getAnnotation(JsonIdentityInfo.class), member.getAnnotation(JsonIdentityReference.class));
}
if (property == null) {
property = context.resolveProperty(nested, annotations);
}
}
if (property != null) {
addMetadataToProperty(property, propName, md, isReadOnly, member, mp);
applyBeanValidatorAnnotations(property, annotations);
props.add(property);
}
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project core-ng-project by neowu.
the class JSONReader method of.
public static <T> JSONReader<T> of(Type instanceType) {
ObjectMapper objectMapper = JSONMapper.OBJECT_MAPPER;
JavaType type = objectMapper.getTypeFactory().constructType(instanceType);
return new JSONReader<>(objectMapper.readerFor(type));
}
Aggregations