org.eclipse.microprofile.openapi.models.media.Schema Java Examples
The following examples show how to use
org.eclipse.microprofile.openapi.models.media.Schema.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelUtil.java From smallrye-open-api with Apache License 2.0 | 6 votes |
/** * Returns the list of {@link Schema}s defined for the given {@link Parameter}. * A schema can be defined either via the parameter's "schema" property, or any * "content.*.schema" property. * * @param parameter Parameter * @return list of schemas, never null */ public static List<Schema> getParameterSchemas(Parameter parameter) { if (parameter.getSchema() != null) { return Arrays.asList(parameter.getSchema()); } Map<String, MediaType> mediaTypes = getMediaTypesOrEmpty(parameter.getContent()); if (!mediaTypes.isEmpty()) { List<Schema> schemas = new ArrayList<>(mediaTypes.size()); for (MediaType mediaType : mediaTypes.values()) { if (mediaType.getSchema() != null) { schemas.add(mediaType.getSchema()); } } } return Collections.emptyList(); }
Example #2
Source File: DataObjectDeque.java From smallrye-open-api with Apache License 2.0 | 6 votes |
/** * Create new entry and push to stack. Performs cycle detection. * * @param annotationTarget annotation target * @param parentPathEntry parent path entry * @param type the annotated type * @param schema the schema corresponding to this position */ public void push(AnnotationTarget annotationTarget, PathEntry parentPathEntry, Type type, Schema schema) { validateInput(parentPathEntry, type, schema); PathEntry entry = leafNode(parentPathEntry, annotationTarget, type, schema); ClassInfo klazzInfo = entry.getClazz(); if (parentPathEntry.hasParent(entry)) { // Cycle detected, don't push path. DataObjectLogging.log.possibleCycle(klazzInfo); DataObjectLogging.log.path(entry.toStringWithGraph()); if (schema.getDescription() == null) { schema.description("Cyclic reference to " + klazzInfo.name()); } } else { // Push path to be inspected later. DataObjectLogging.log.addingChildNode(klazzInfo); path.push(entry); } }
Example #3
Source File: TypeProcessor.java From smallrye-open-api with Apache License 2.0 | 6 votes |
private Type resolveTypeVariable(Schema schema, Type fieldType) { // Type variable (e.g. A in Foo<A>) Type resolvedType = typeResolver.getResolvedType(fieldType); DataObjectLogging.log.resolvedType(fieldType, resolvedType); if (isTerminalType(resolvedType) || !index.containsClass(resolvedType)) { DataObjectLogging.log.terminalType(resolvedType); TypeUtil.applyTypeAttributes(resolvedType, schema); } else { DataObjectLogging.log.typeVarSubstitution(fieldType, resolvedType); if (index.containsClass(resolvedType)) { // Add resolved type to stack. objectStack.push(annotationTarget, parentPathEntry, resolvedType, schema); } else { DataObjectLogging.log.classNotAvailable(resolvedType); } } return resolvedType; }
Example #4
Source File: BeanValidationScanner.java From smallrye-open-api with Apache License 2.0 | 6 votes |
void sizeString(AnnotationTarget target, Schema schema) { AnnotationInstance constraint = getConstraint(target, BV_SIZE); if (constraint != null) { Integer min = intValue(constraint, "min"); Integer max = intValue(constraint, "max"); if (min != null && schema.getMinLength() == null) { schema.setMinLength(min); } if (max != null && schema.getMaxLength() == null) { schema.setMaxLength(max); } } }
Example #5
Source File: SchemaFactory.java From smallrye-open-api with Apache License 2.0 | 6 votes |
/** * Reads a Schema annotation into a model. * * @param index the index * @param annotation the annotation instance * @return Schema model */ public static Schema readSchema(IndexView index, AnnotationInstance annotation) { if (annotation == null) { return null; } IoLogging.log.singleAnnotation("@Schema"); // Schemas can be hidden. Skip if that's the case. Optional<Boolean> isHidden = JandexUtil.booleanValue(annotation, SchemaConstant.PROP_HIDDEN); if (isHidden.isPresent() && isHidden.get()) { return null; } return readSchema(index, new SchemaImpl(), annotation, Collections.emptyMap()); }
Example #6
Source File: ParameterProcessor.java From smallrye-open-api with Apache License 2.0 | 6 votes |
/** * Create a {@link Content} and use the scanned {@link javax.ws.rs.FormParam}s * as the properties. The media type will be defaulted to * 'application/x-www-form-urlencoded' * * @return generated form content */ private Content getFormBodyContent() { if (formParams.isEmpty()) { return null; } Content content = new ContentImpl(); MediaType mediaType = new MediaTypeImpl(); Schema schema = new SchemaImpl(); Map<String, Encoding> encodings = new HashMap<>(); schema.setType(SchemaType.OBJECT); mediaType.setSchema(schema); setSchemaProperties(schema, encodings, formParams); if (encodings.size() > 0) { mediaType.setEncoding(encodings); } // TODO: Do this for Spring ? //String mediaTypeName = formMediaType != null ? formMediaType : APPLICATION_FORM_URLENCODED; //content.addMediaType(mediaTypeName, mediaType); return content; }
Example #7
Source File: ParameterProcessor.java From smallrye-open-api with Apache License 2.0 | 6 votes |
/** * Create a {@link Content} and use the scanned {@link javax.ws.rs.FormParam}s * as the properties. The media type will be defaulted to * 'application/x-www-form-urlencoded' or set to 'multipart/form-data' if a * RESTEasy {@link org.jboss.resteasy.annotations.providers.multipart.MultipartForm MultipartForm} * annotation was used to wrap the {@link javax.ws.rs.FormParam}s. The encoding values * for the {@link Content} will be set to the value of any * {@link org.jboss.resteasy.annotations.providers.multipart.PartType PartType} * annotations found for each parameter. * * @return generated form content */ private Content getFormBodyContent() { if (formParams.isEmpty()) { return null; } Content content = new ContentImpl(); MediaType mediaType = new MediaTypeImpl(); Schema schema = new SchemaImpl(); Map<String, Encoding> encodings = new HashMap<>(); schema.setType(SchemaType.OBJECT); mediaType.setSchema(schema); setSchemaProperties(schema, encodings, formParams); if (encodings.size() > 0) { mediaType.setEncoding(encodings); } String mediaTypeName = formMediaType != null ? formMediaType : APPLICATION_FORM_URLENCODED; content.addMediaType(mediaTypeName, mediaType); return content; }
Example #8
Source File: BeanValidationScanner.java From smallrye-open-api with Apache License 2.0 | 6 votes |
void decimalMax(AnnotationTarget target, Schema schema) { AnnotationInstance constraint = getConstraint(target, BV_DECIMAL_MAX); if (constraint != null && schema.getMaximum() == null) { String decimalValue = stringValue(constraint, VALUE); try { BigDecimal decimal = new BigDecimal(decimalValue); schema.setMaximum(decimal); Optional<Boolean> inclusive = booleanValue(constraint, INCLUSIVE); if (schema.getExclusiveMaximum() == null && inclusive.isPresent() && !inclusive.get()) { schema.setExclusiveMaximum(Boolean.TRUE); } } catch (@SuppressWarnings("unused") NumberFormatException e) { DataObjectLogging.log.invalidAnnotationFormat(decimalValue); } } }
Example #9
Source File: AirlinesOASFilter.java From microprofile-open-api with Apache License 2.0 | 6 votes |
@Override public APIResponse filterAPIResponse(APIResponse apiResponse) { if("subscription successfully created".equals(apiResponse.getDescription())){ apiResponse.setDescription("filterAPIResponse - subscription successfully created"); } // testing child before parent filtering Content content = apiResponse.getContent(); if (content != null) { if (content.hasMediaType("application/json")) { Schema schema = content.getMediaType("application/json").getSchema(); if ("child - id of the new review".equals(schema.getDescription())) { schema.setDescription("parent - id of the new review"); } } } return apiResponse; }
Example #10
Source File: BeanValidationScannerTest.java From smallrye-open-api with Apache License 2.0 | 6 votes |
/**********************************************************************/ @Test public void testArrayListNotNullAndNotEmptyAndMaxItems() { FieldInfo targetField = targetClass.field("arrayListNotNullAndNotEmptyAndMaxItems"); Schema parentSchema = new SchemaImpl(); String propertyKey = "TESTKEY"; testTarget.notNull(targetField, schema, propertyKey, (target, name) -> { parentSchema.addRequired(name); }); testTarget.sizeArray(targetField, schema); testTarget.notEmptyArray(targetField, schema); assertEquals(Boolean.FALSE, schema.getNullable()); assertEquals(Integer.valueOf(1), schema.getMinItems()); assertEquals(Integer.valueOf(20), schema.getMaxItems()); assertEquals(Arrays.asList(propertyKey), parentSchema.getRequired()); }
Example #11
Source File: BeanValidationScanner.java From smallrye-open-api with Apache License 2.0 | 6 votes |
void sizeArray(AnnotationTarget target, Schema schema) { AnnotationInstance constraint = getConstraint(target, BV_SIZE); if (constraint != null) { Integer min = intValue(constraint, "min"); Integer max = intValue(constraint, "max"); if (min != null && schema.getMinItems() == null) { schema.setMinItems(min); } if (max != null && schema.getMaxItems() == null) { schema.setMaxItems(max); } } }
Example #12
Source File: SchemaFactory.java From smallrye-open-api with Apache License 2.0 | 6 votes |
/** * Introspect into the given Class to generate a Schema model. The boolean indicates * whether this class type should be turned into a reference. * * @param index the index of classes being scanned * @param type the implementation type of the item to scan * @param schemaReferenceSupported */ static Schema readClassSchema(IndexView index, Type type, boolean schemaReferenceSupported) { if (type == null) { return null; } Schema schema; if (type.kind() == Type.Kind.ARRAY) { schema = new SchemaImpl().type(SchemaType.ARRAY); ArrayType array = type.asArrayType(); int dimensions = array.dimensions(); Type componentType = array.component(); if (dimensions > 1) { // Recurse using a new array type with dimensions decremented schema.items(readClassSchema(index, ArrayType.create(componentType, dimensions - 1), schemaReferenceSupported)); } else { // Recurse using the type of the array elements schema.items(readClassSchema(index, componentType, schemaReferenceSupported)); } } else if (type.kind() == Type.Kind.PRIMITIVE) { schema = OpenApiDataObjectScanner.process(type.asPrimitiveType()); } else { schema = introspectClassToSchema(index, type.asClassType(), schemaReferenceSupported); } return schema; }
Example #13
Source File: SchemaFactory.java From smallrye-open-api with Apache License 2.0 | 5 votes |
/** * Convert a Jandex enum class type to a {@link Schema} model.Adds each enum constant name to the list of the given schema's * enumeration list. * * The given type must be found in the index. * * @param index Jandex index containing the ClassInfo for the given enum type * @param enumType type containing Java Enum constants * @return Schema model * * @see java.lang.reflect.Field#isEnumConstant() */ public static Schema enumToSchema(IndexView index, Type enumType) { IoLogging.log.enumProcessing(enumType); final int ENUM = 0x00004000; // see java.lang.reflect.Modifier#ENUM ClassInfo enumKlazz = index.getClassByName(TypeUtil.getName(enumType)); AnnotationInstance schemaAnnotation = enumKlazz.classAnnotation(SchemaConstant.DOTNAME_SCHEMA); Schema enumSchema = new SchemaImpl(); List<Object> enumeration = enumKlazz.fields() .stream() .filter(field -> (field.flags() & ENUM) != 0) .map(FieldInfo::name) .sorted() // Make the order determinate .collect(Collectors.toList()); if (schemaAnnotation != null) { Map<String, Object> defaults = new HashMap<>(2); defaults.put(SchemaConstant.PROP_TYPE, SchemaType.STRING); defaults.put(SchemaConstant.PROP_ENUMERATION, enumeration); enumSchema = readSchema(index, enumSchema, schemaAnnotation, enumKlazz, defaults); } else { enumSchema.setType(SchemaType.STRING); enumSchema.setEnumeration(enumeration); } return enumSchema; }
Example #14
Source File: SchemaWriter.java From smallrye-open-api with Apache License 2.0 | 5 votes |
/** * Writes a {@link Schema} to the JSON tree. * * @param parent the parent json node * @param model Schema model * @param name name of the node */ public static void writeSchema(ObjectNode parent, Schema model, String name) { if (model == null) { return; } ObjectNode node = parent.putObject(name); writeSchema(node, model); }
Example #15
Source File: FilterUtil.java From smallrye-open-api with Apache License 2.0 | 5 votes |
/** * Filters the given models. * * @param filter * @param models */ private static void filterSchemaList(OASFilter filter, List<Schema> models) { if (models != null) { ListIterator<Schema> iterator = models.listIterator(); while (iterator.hasNext()) { Schema model = iterator.next(); filterSchema(filter, model); if (filter.filterSchema(model) == null) { iterator.remove(); } } } }
Example #16
Source File: SchemaWriter.java From smallrye-open-api with Apache License 2.0 | 5 votes |
/** * Writes a map of {@link Schema} to the JSON tree. * * @param parent * @param schemas */ private static void writeSchemas(ObjectNode parent, Map<String, Schema> schemas, String propertyName) { if (schemas == null) { return; } ObjectNode schemasNode = parent.putObject(propertyName); for (Map.Entry<String, Schema> entry : schemas.entrySet()) { writeSchema(schemasNode, entry.getValue(), entry.getKey()); } }
Example #17
Source File: DataObjectDeque.java From smallrye-open-api with Apache License 2.0 | 5 votes |
private PathEntry(PathEntry enclosing, AnnotationTarget annotationTarget, ClassInfo clazz, Type clazzType, Schema schema) { validateInput(clazz, clazzType, schema); this.enclosing = enclosing; this.annotationTarget = annotationTarget; this.clazz = clazz; this.clazzType = clazzType; this.schema = schema; }
Example #18
Source File: BeanValidationScanner.java From smallrye-open-api with Apache License 2.0 | 5 votes |
void notEmptyObject(AnnotationTarget target, Schema schema) { if (!allowsAdditionalProperties(schema)) { return; } AnnotationInstance constraint = getConstraint(target, BV_NOT_EMPTY); if (constraint != null && schema.getMinProperties() == null) { schema.setMinProperties(1); } }
Example #19
Source File: BeanValidationScanner.java From smallrye-open-api with Apache License 2.0 | 5 votes |
void notEmptyString(AnnotationTarget target, Schema schema) { AnnotationInstance constraint = getConstraint(target, BV_NOT_EMPTY); if (constraint != null) { if (schema.getNullable() == null) { schema.setNullable(Boolean.FALSE); } if (schema.getMinLength() == null) { schema.setMinLength(1); } } }
Example #20
Source File: BeanValidationScanner.java From smallrye-open-api with Apache License 2.0 | 5 votes |
private static void applyNumberConstraints(AnnotationTarget target, Schema schema, String propertyKey, RequirementHandler handler) { INSTANCE.decimalMax(target, schema); INSTANCE.decimalMin(target, schema); INSTANCE.digits(target, schema); INSTANCE.max(target, schema); INSTANCE.min(target, schema); INSTANCE.negative(target, schema); INSTANCE.negativeOrZero(target, schema); INSTANCE.notNull(target, schema, propertyKey, handler); INSTANCE.positive(target, schema); INSTANCE.positiveOrZero(target, schema); }
Example #21
Source File: BeanValidationScanner.java From smallrye-open-api with Apache License 2.0 | 5 votes |
void min(AnnotationTarget target, Schema schema) { AnnotationInstance constraint = getConstraint(target, BV_MIN); if (constraint != null && schema.getMinimum() == null) { AnnotationValue value = constraint.value(VALUE); schema.setMinimum(new BigDecimal(value.asLong())); } }
Example #22
Source File: IndexScannerTestBase.java From smallrye-open-api with Apache License 2.0 | 5 votes |
public static String schemaToString(String entityName, Schema schema) throws IOException { Map<String, Schema> map = new HashMap<>(); map.put(entityName, schema); OpenAPIImpl oai = new OpenAPIImpl(); ComponentsImpl comp = new ComponentsImpl(); comp.setSchemas(map); oai.setComponents(comp); return OpenApiSerializer.serialize(oai, Format.JSON); }
Example #23
Source File: TypeProcessor.java From smallrye-open-api with Apache License 2.0 | 5 votes |
public TypeProcessor(AugmentedIndexView index, DataObjectDeque objectStack, DataObjectDeque.PathEntry parentPathEntry, TypeResolver typeResolver, Type type, Schema schema, AnnotationTarget annotationTarget) { this.objectStack = objectStack; this.typeResolver = typeResolver; this.parentPathEntry = parentPathEntry; this.type = type; this.schema = schema; this.index = index; this.annotationTarget = annotationTarget; }
Example #24
Source File: OpenApiDataObjectScanner.java From smallrye-open-api with Apache License 2.0 | 5 votes |
private void depthFirstGraphSearch() { DataObjectDeque.PathEntry currentPathEntry; while (!objectStack.isEmpty()) { currentPathEntry = objectStack.pop(); ClassInfo currentClass = currentPathEntry.getClazz(); Schema currentSchema = currentPathEntry.getSchema(); Type currentType = currentPathEntry.getClazzType(); // First, handle class annotations (re-assign since readKlass may return new schema) currentSchema = readKlass(currentClass, currentSchema); currentPathEntry.setSchema(currentSchema); if (currentSchema.getType() == null) { // If not schema has yet been set, consider this an "object" currentSchema.setType(Schema.SchemaType.OBJECT); } if (currentSchema.getType() != Schema.SchemaType.OBJECT) { // Only 'object' type schemas should have properties of their own continue; } ScannerLogging.log.gettingFields(currentType, currentClass); // Get all fields *including* inherited. Map<String, TypeResolver> properties = TypeResolver.getAllFields(index, currentType, currentClass); // Handle fields for (Map.Entry<String, TypeResolver> entry : properties.entrySet()) { TypeResolver resolver = entry.getValue(); // Ignore static fields and fields annotated with ignore. if (!ignoreResolver.isIgnore(resolver.getAnnotationTarget(), currentPathEntry)) { AnnotationTargetProcessor.process(index, objectStack, resolver, currentPathEntry); } } } }
Example #25
Source File: AirlinesOASFilter.java From microprofile-open-api with Apache License 2.0 | 5 votes |
@Override public Schema filterSchema(Schema schema) { if("subscription information".equals(schema.getDescription())){ schema.setDescription("filterSchema - subscription information"); } // testing child before parent filtering if ("id of the new review".equals(schema.getDescription())) { schema.setDescription("child - id of the new review"); } return schema; }
Example #26
Source File: SchemaFactory.java From smallrye-open-api with Apache License 2.0 | 5 votes |
private static Schema otherTypeToSchema(IndexView index, Type type, List<AnnotationScannerExtension> extensions) { if (TypeUtil.isA(index, type, MutinyConstants.MULTI_TYPE)) { // Treat as an Array Schema schema = new SchemaImpl().type(SchemaType.ARRAY); Type componentType = type.asParameterizedType().arguments().get(0); // Recurse using the type of the array elements schema.items(typeToSchema(index, componentType, extensions)); return schema; } else { Type asyncType = resolveAsyncType(index, type, extensions); return schemaRegistration(index, asyncType, OpenApiDataObjectScanner.process(index, asyncType)); } }
Example #27
Source File: ParameterProcessor.java From smallrye-open-api with Apache License 2.0 | 5 votes |
/** * Converts the collection of parameter annotations to properties set on the * given schema. * * @param schema the {@link Schema} on which the properties will be set * @param encodings map of encodings applicable to the current {@link MediaType} being processed * @param params the name/value pairs of annotations for conversion to schema properties */ void setSchemaProperties(Schema schema, Map<String, Encoding> encodings, Map<String, AnnotationInstance> params) { for (Entry<String, AnnotationInstance> param : params.entrySet()) { String paramName = param.getKey(); AnnotationTarget paramTarget = param.getValue().target(); Type paramType = getType(paramTarget); Schema paramSchema = SchemaFactory.typeToSchema(index, paramType, extensions); Object defaultValue = getDefaultValue(paramTarget); if (paramSchema.getDefaultValue() == null) { paramSchema.setDefaultValue(defaultValue); } BeanValidationScanner.applyConstraints(paramTarget, paramSchema, paramName, (target, name) -> { List<String> requiredProperties = schema.getRequired(); if (requiredProperties == null || !requiredProperties.contains(name)) { schema.addRequired(name); } }); if (paramSchema.getNullable() == null && TypeUtil.isOptional(paramType)) { paramSchema.setNullable(Boolean.TRUE); } if (schema.getProperties() != null) { paramSchema = mergeObjects(schema.getProperties().get(paramName), paramSchema); } schema.addProperty(paramName, paramSchema); } }
Example #28
Source File: JaxRsAnnotationScannerTest.java From smallrye-open-api with Apache License 2.0 | 5 votes |
@Override public void registerCustomSchemas(SchemaRegistry schemaRegistry) { Type uuidType = Type.create(componentize(UUID.class.getName()), Kind.CLASS); Schema schema = new SchemaImpl(); schema.setType(Schema.SchemaType.STRING); schema.setFormat("uuid"); schema.setPattern("^[a-f0-9]{8}-?[a-f0-9]{4}-?[1-5][a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$"); schema.setTitle("UUID"); schema.setDescription("Universally Unique Identifier"); schema.setExample("de8681db-b4d6-4c47-a428-4b959c1c8e9a"); schemaRegistry.register(uuidType, schema); }
Example #29
Source File: SchemaReader.java From smallrye-open-api with Apache License 2.0 | 5 votes |
/** * Reads a map of Schema annotations. * * @param context the scanner context * @param annotationValue map of {@literal @}Schema annotations * @return Map of Schema models */ public static Map<String, Schema> readSchemas(final AnnotationScannerContext context, final AnnotationValue annotationValue) { if (annotationValue == null) { return null; } IoLogging.log.annotationsMap("@Schema"); Map<String, Schema> map = new LinkedHashMap<>(); AnnotationInstance[] nestedArray = annotationValue.asNestedArray(); for (AnnotationInstance nested : nestedArray) { String name = JandexUtil.stringValue(nested, SchemaConstant.PROP_NAME); if (name == null && JandexUtil.isRef(nested)) { name = JandexUtil.nameFromRef(nested); } /* * The name is REQUIRED when the schema is defined within * {@link org.eclipse.microprofile.openapi.annotations.Components}. */ if (name != null) { map.put(name, SchemaFactory.readSchema(context.getIndex(), nested)); } /*- //For consideration - be more lenient and attempt to use the name from the implementation's @Schema? else { if (JandexUtil.isSimpleClassSchema(nested)) { Schema schema = SchemaFactory.readClassSchema(index, nested.value(OpenApiConstants.PROP_IMPLEMENTATION), false); if (schema instanceof SchemaImpl) { name = ((SchemaImpl) schema).getName(); if (name != null) { map.put(name, schema); } } } }*/ } return map; }
Example #30
Source File: OpenApiDataObjectScanner.java From smallrye-open-api with Apache License 2.0 | 5 votes |
private Schema readKlass(ClassInfo currentClass, Schema currentSchema) { AnnotationInstance annotation = TypeUtil.getSchemaAnnotation(currentClass); if (annotation != null) { // Because of implementation= field, *may* return a new schema rather than modify. return SchemaFactory.readSchema(index, currentSchema, annotation, currentClass); } return currentSchema; }