use of io.swagger.v3.oas.models.media.StringSchema in project flow by vaadin.
the class SchemaResolverTest method should_ReturnArraySchema_When_GivenTypeIsAnArray.
@Test
public void should_ReturnArraySchema_When_GivenTypeIsAnArray() {
ResolvedType arrayType = mock(ResolvedType.class);
ResolvedArrayType arrayResolvedType = mock(ResolvedArrayType.class);
ResolvedType stringType = mockReferencedTypeOf(String.class);
when(arrayType.isArray()).thenReturn(true);
when(arrayType.asArrayType()).thenReturn(arrayResolvedType);
when(arrayResolvedType.getComponentType()).thenReturn(stringType);
Map<String, GeneratorType> usedTypes = new HashMap<>();
SchemaResolver schemaResolver = new SchemaResolver(new GeneratorType(arrayType), usedTypes);
Schema schema = schemaResolver.resolve();
Assert.assertTrue(schema instanceof ArraySchema);
Assert.assertTrue(schema.getNullable());
Assert.assertTrue(((ArraySchema) schema).getItems() instanceof StringSchema);
Assert.assertTrue(usedTypes.isEmpty());
}
use of io.swagger.v3.oas.models.media.StringSchema in project flow by vaadin.
the class SchemaResolverTest method should_ReturnNullableStringSchema_When_GivenTypeIsAString.
@Test
public void should_ReturnNullableStringSchema_When_GivenTypeIsAString() {
ResolvedType resolvedType = mockReferencedTypeOf(String.class);
Map<String, GeneratorType> usedTypes = new HashMap<>();
SchemaResolver schemaResolver = new SchemaResolver(new GeneratorType(resolvedType), usedTypes);
Schema schema = schemaResolver.resolve();
Assert.assertTrue(schema instanceof StringSchema);
Assert.assertTrue(schema.getNullable());
Assert.assertTrue(usedTypes.isEmpty());
}
use of io.swagger.v3.oas.models.media.StringSchema in project flow by vaadin.
the class SchemaGenerator method createSingleSchemaFromResolvedType.
Schema createSingleSchemaFromResolvedType(GeneratorType type) {
ResolvedReferenceType resolvedReferenceType = type.asResolvedType().asReferenceType();
if (type.isEnum()) {
List<String> entries = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).asEnum().getEnumConstants().stream().map(ResolvedEnumConstantDeclaration::getName).collect(Collectors.toList());
StringSchema schema = new StringSchema();
schema.name(resolvedReferenceType.getQualifiedName());
schema.setEnum(entries);
return schema;
}
Schema schema = new ObjectSchema().name(resolvedReferenceType.getQualifiedName());
Map<String, Boolean> fieldsOptionalMap = getFieldsAndOptionalMap(type);
List<ResolvedFieldDeclaration> serializableFields = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).getDeclaredFields().stream().filter(resolvedFieldDeclaration -> fieldsOptionalMap.containsKey(resolvedFieldDeclaration.getName())).collect(Collectors.toList());
// Make sure the order is consistent in properties map
schema.setProperties(new LinkedHashMap<>());
for (ResolvedFieldDeclaration resolvedFieldDeclaration : serializableFields) {
String name = resolvedFieldDeclaration.getName();
ResolvedType fieldType = resolvedFieldDeclaration.getType();
ResolvedType mappedType = openApiObjectGenerator.toMappedType(fieldType);
if (mappedType != null) {
fieldType = mappedType;
}
Schema subtype = openApiObjectGenerator.parseResolvedTypeToSchema(new GeneratorType(fieldType)).nullable(null);
if (!fieldsOptionalMap.get(name)) {
schema.addRequiredItem(name);
}
schema.addProperties(name, subtype);
}
return schema;
}
use of io.swagger.v3.oas.models.media.StringSchema in project flow by vaadin.
the class SchemaResolver method createEnumTypeSchema.
private Schema createEnumTypeSchema() {
ResolvedReferenceType resolvedReferenceType = type.asResolvedType().asReferenceType();
List<String> entries = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).asEnum().getEnumConstants().stream().map(ResolvedEnumConstantDeclaration::getName).collect(Collectors.toList());
String qualifiedName = resolvedReferenceType.getQualifiedName();
usedTypes.put(qualifiedName, type);
StringSchema schema = new StringSchema();
schema.name(qualifiedName);
schema.setEnum(entries);
schema.$ref(getFullQualifiedNameRef(qualifiedName));
return schema;
}
use of io.swagger.v3.oas.models.media.StringSchema in project flow by vaadin.
the class SchemaResolverTest method should_ReturnNullableArray_When_GivenTypeIsAListString.
@Test
public void should_ReturnNullableArray_When_GivenTypeIsAListString() {
ResolvedType resolvedType = mockReferencedTypeOf(Collection.class);
ResolvedReferenceType resolvedReferenceType = resolvedType.asReferenceType();
List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> pairs = Collections.singletonList(new Pair<>(null, mockReferencedTypeOf(String.class)));
when(resolvedReferenceType.getTypeParametersMap()).thenReturn(pairs);
Map<String, GeneratorType> usedTypes = new HashMap<>();
SchemaResolver schemaResolver = new SchemaResolver(new GeneratorType(resolvedType), usedTypes);
Schema schema = schemaResolver.resolve();
Assert.assertTrue(schema instanceof ArraySchema);
Assert.assertTrue(schema.getNullable());
Assert.assertTrue(((ArraySchema) schema).getItems() instanceof StringSchema);
Assert.assertTrue(usedTypes.isEmpty());
}
Aggregations