use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.
the class ModelResolver method resolvePatternProperties.
protected Map<String, Schema> resolvePatternProperties(JavaType a, Annotation[] annotations, ModelConverterContext context) {
final Map<String, PatternProperty> propList = new LinkedHashMap<>();
PatternProperties props = a.getRawClass().getAnnotation(PatternProperties.class);
if (props != null && props.value().length > 0) {
for (PatternProperty prop : props.value()) {
propList.put(prop.regex(), prop);
}
}
PatternProperty singleProp = a.getRawClass().getAnnotation(PatternProperty.class);
if (singleProp != null) {
propList.put(singleProp.regex(), singleProp);
}
props = AnnotationsUtils.getAnnotation(PatternProperties.class, annotations);
if (props != null && props.value().length > 0) {
for (PatternProperty prop : props.value()) {
propList.put(prop.regex(), prop);
}
}
singleProp = AnnotationsUtils.getAnnotation(PatternProperty.class, annotations);
if (singleProp != null) {
propList.put(singleProp.regex(), singleProp);
}
if (propList.isEmpty()) {
return null;
}
Map<String, Schema> patternProperties = new LinkedHashMap<>();
for (PatternProperty prop : propList.values()) {
String key = prop.regex();
if (StringUtils.isBlank(key)) {
continue;
}
Annotation[] propAnnotations = new Annotation[] { prop.schema(), prop.array() };
AnnotatedType propType = new AnnotatedType().type(String.class).ctxAnnotations(propAnnotations).resolveAsRef(true);
Schema resolvedPropSchema = context.resolve(propType);
if (resolvedPropSchema != null) {
patternProperties.put(key, resolvedPropSchema);
}
}
return patternProperties;
}
use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.
the class ModelResolver method resolveSubtypes.
private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context) {
final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
if (types == null) {
return false;
}
/**
* Remove the current class from the child classes. This happens if @JsonSubTypes references
* the annotated class as a subtype.
*/
removeSelfFromSubTypes(types, bean);
/**
* As the introspector will find @JsonSubTypes for a child class that are present on its super classes, the
* code segment below will also run the introspector on the parent class, and then remove any sub-types that are
* found for the parent from the sub-types found for the child. The same logic all applies to implemented
* interfaces, and is accounted for below.
*/
removeSuperClassAndInterfaceSubTypes(types, bean);
int count = 0;
final Class<?> beanClass = bean.getClassInfo().getAnnotated();
for (NamedType subtype : types) {
final Class<?> subtypeType = subtype.getType();
if (!beanClass.isAssignableFrom(subtypeType)) {
continue;
}
final Schema subtypeModel = context.resolve(new AnnotatedType().type(subtypeType));
if (StringUtils.isBlank(subtypeModel.getName()) || subtypeModel.getName().equals(model.getName())) {
subtypeModel.setName(_typeNameResolver.nameForType(_mapper.constructType(subtypeType), TypeNameResolver.Options.SKIP_API_MODEL));
}
// here schema could be not composed, but we want it to be composed, doing same work as done
// in resolve method??
ComposedSchema composedSchema = null;
if (!(subtypeModel instanceof ComposedSchema)) {
// create composed schema
// TODO #2312 - smarter way needs clone implemented in #2227
composedSchema = (ComposedSchema) new ComposedSchema().title(subtypeModel.getTitle()).name(subtypeModel.getName()).deprecated(subtypeModel.getDeprecated()).additionalProperties(subtypeModel.getAdditionalProperties()).description(subtypeModel.getDescription()).discriminator(subtypeModel.getDiscriminator()).exclusiveMaximum(subtypeModel.getExclusiveMaximum()).exclusiveMinimum(subtypeModel.getExclusiveMinimum()).externalDocs(subtypeModel.getExternalDocs()).format(subtypeModel.getFormat()).maximum(subtypeModel.getMaximum()).maxItems(subtypeModel.getMaxItems()).maxLength(subtypeModel.getMaxLength()).maxProperties(subtypeModel.getMaxProperties()).minimum(subtypeModel.getMinimum()).minItems(subtypeModel.getMinItems()).minLength(subtypeModel.getMinLength()).minProperties(subtypeModel.getMinProperties()).multipleOf(subtypeModel.getMultipleOf()).not(subtypeModel.getNot()).nullable(subtypeModel.getNullable()).pattern(subtypeModel.getPattern()).properties(subtypeModel.getProperties()).readOnly(subtypeModel.getReadOnly()).required(subtypeModel.getRequired()).type(subtypeModel.getType()).uniqueItems(subtypeModel.getUniqueItems()).writeOnly(subtypeModel.getWriteOnly()).xml(subtypeModel.getXml()).extensions(subtypeModel.getExtensions());
if (subtypeModel.getExample() != null || subtypeModel.getExampleSetFlag()) {
composedSchema.example(subtypeModel.getExample());
}
composedSchema.setEnum(subtypeModel.getEnum());
} else {
composedSchema = (ComposedSchema) subtypeModel;
}
Schema refSchema = new Schema().$ref(model.getName());
// allOf could have already being added during type resolving when @Schema(allOf..) is declared
if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) {
composedSchema.addAllOfItem(refSchema);
}
removeParentProperties(composedSchema, model);
if (!composedModelPropertiesAsSibling) {
if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) {
if (composedSchema.getProperties() != null && !composedSchema.getProperties().isEmpty()) {
ObjectSchema propSchema = new ObjectSchema();
propSchema.properties(composedSchema.getProperties());
composedSchema.setProperties(null);
composedSchema.addAllOfItem(propSchema);
}
}
}
// replace previous schema..
Class<?> currentType = subtype.getType();
if (StringUtils.isNotBlank(composedSchema.getName())) {
context.defineModel(composedSchema.getName(), composedSchema, new AnnotatedType().type(currentType), null);
}
}
return count != 0;
}
use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.
the class ModelResolver method resolveSchemaMembers.
protected void resolveSchemaMembers(Schema schema, AnnotatedType annotatedType) {
final JavaType type;
if (annotatedType.getType() instanceof JavaType) {
type = (JavaType) annotatedType.getType();
} else {
type = _mapper.constructType(annotatedType.getType());
}
final Annotation resolvedSchemaOrArrayAnnotation = AnnotationsUtils.mergeSchemaAnnotations(annotatedType.getCtxAnnotations(), type);
final io.swagger.v3.oas.annotations.media.Schema schemaAnnotation = resolvedSchemaOrArrayAnnotation == null ? null : resolvedSchemaOrArrayAnnotation instanceof io.swagger.v3.oas.annotations.media.ArraySchema ? ((io.swagger.v3.oas.annotations.media.ArraySchema) resolvedSchemaOrArrayAnnotation).schema() : (io.swagger.v3.oas.annotations.media.Schema) resolvedSchemaOrArrayAnnotation;
final BeanDescription beanDesc = _mapper.getSerializationConfig().introspect(type);
Annotated a = beanDesc.getClassInfo();
Annotation[] annotations = annotatedType.getCtxAnnotations();
resolveSchemaMembers(schema, a, annotations, schemaAnnotation);
}
use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.
the class AnnotationsUtils method resolveSchemaFromType.
public static Schema resolveSchemaFromType(Class<?> schemaImplementation, Components components, JsonView jsonViewAnnotation) {
Schema schemaObject;
PrimitiveType primitiveType = PrimitiveType.fromType(schemaImplementation);
if (primitiveType != null) {
schemaObject = primitiveType.createProperty();
} else {
schemaObject = new Schema();
ResolvedSchema resolvedSchema = ModelConverters.getInstance().readAllAsResolvedSchema(new AnnotatedType().type(schemaImplementation).jsonViewAnnotation(jsonViewAnnotation));
Map<String, Schema> schemaMap;
if (resolvedSchema != null) {
schemaMap = resolvedSchema.referencedSchemas;
if (schemaMap != null) {
schemaMap.forEach((key, referencedSchema) -> {
if (components != null) {
components.addSchemas(key, referencedSchema);
}
});
}
if (resolvedSchema.schema != null) {
if (StringUtils.isNotBlank(resolvedSchema.schema.getName())) {
schemaObject.set$ref(COMPONENTS_REF + resolvedSchema.schema.getName());
} else {
schemaObject = resolvedSchema.schema;
}
}
}
}
if (StringUtils.isBlank(schemaObject.get$ref()) && StringUtils.isBlank(schemaObject.getType())) {
// default to string
schemaObject.setType("string");
}
return schemaObject;
}
use of io.swagger.v3.core.converter.AnnotatedType in project swagger-core by swagger-api.
the class EnumPropertyTest method testEnumRefPropertyGlobal.
@Test(description = "it should read a model with an enum property as a reference, set via static var or sys prop")
public void testEnumRefPropertyGlobal() {
ModelResolver.enumsAsRef = true;
Schema schema = context.resolve(new AnnotatedType(ModelWithEnumProperty.class));
final Map<String, Schema> models = context.getDefinedModels();
final String yaml = "ModelWithEnumProperty:\n" + " type: object\n" + " properties:\n" + " enumValue:\n" + " $ref: '#/components/schemas/TestEnum'\n" + "TestEnum:\n" + " type: string\n" + " enum:\n" + " - PRIVATE\n" + " - PUBLIC\n" + " - SYSTEM\n" + " - INVITE_ONLY\n";
SerializationMatchers.assertEqualsToYaml(models, yaml);
ModelResolver.enumsAsRef = false;
}
Aggregations