Java Code Examples for org.eclipse.microprofile.openapi.models.media.Schema#setNullable()

The following examples show how to use org.eclipse.microprofile.openapi.models.media.Schema#setNullable() . These examples are extracted from open source projects. 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
/**
 * 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();
        addEncoding(encodings, paramName, paramTarget);
        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 2
/**
 * 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 3
void notBlank(AnnotationTarget target, Schema schema) {
    AnnotationInstance constraint = getConstraint(target, BV_NOT_BLANK);

    if (constraint != null) {
        if (schema.getNullable() == null) {
            schema.setNullable(Boolean.FALSE);
        }
        if (schema.getPattern() == null) {
            schema.setPattern("\\S");
        }
    }
}
 
Example 4
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 5
void notNull(AnnotationTarget target, Schema schema, String propertyKey, RequirementHandler handler) {
    AnnotationInstance constraint = getConstraint(target, BV_NOT_NULL);

    if (constraint != null) {
        if (schema.getNullable() == null) {
            schema.setNullable(Boolean.FALSE);
        }

        if (handler != null && propertyKey != null) {
            handler.setRequired(target, propertyKey);
        }
    }
}
 
Example 6
/**
 * This method will generate a schema for the {@link #annotationTarget} containing one
 * of the following :
 * 
 * <ol>
 * <li>A schema composed (using {@link Schema#allOf(List) allOf}) of a <code>$ref</code> to the schema of the
 * {@link #entityType}
 * and the schema attributes scanned or derived from the {@link #annotationTarget} itself that do not
 * generally apply to the {@link #entityType}'s schema such as a field-specific <code>description</code>.
 * </li>
 * <li>A schema containing a <code>$ref</code> to the schema of the {@link #entityType}. This occurs when
 * the field does not contribute any additional or different attributes that are not defined by the base
 * schema of the {@link #entityType}.
 * </li>
 * <li>A schema containing only the attributes scanned or derived from the {@link #annotationTarget} which will include
 * attributes
 * of the {@link #entityType} if it is not able to be registered via
 * {@link SchemaRegistry#checkRegistration(Type, TypeResolver, Schema) checkRegistration}.
 * </li>
 * </ol>
 * 
 * @return the individual or composite schema for the annotationTarget used to create this {@link AnnotationTargetProcessor}
 */
Schema processField() {
    final AnnotationInstance schemaAnnotation = TypeUtil.getSchemaAnnotation(annotationTarget);
    final String propertyKey = typeResolver.getPropertyName();

    final Schema typeSchema;
    final Schema registeredTypeSchema;
    final Type fieldType;

    if (schemaAnnotation != null && JandexUtil.hasImplementation(schemaAnnotation)) {
        typeSchema = null;
        registeredTypeSchema = null;
        fieldType = JandexUtil.value(schemaAnnotation, SchemaConstant.PROP_IMPLEMENTATION);
    } else {
        // Process the type of the field to derive the typeSchema
        TypeProcessor typeProcessor = new TypeProcessor(index, objectStack, parentPathEntry, typeResolver, entityType,
                new SchemaImpl(), annotationTarget);

        // Type could be replaced (e.g. generics)
        fieldType = typeProcessor.processType();

        typeSchema = typeProcessor.getSchema();

        // Set any default values that apply to the type schema as a result of the TypeProcessor
        TypeUtil.applyTypeAttributes(fieldType, typeSchema);

        // The registeredTypeSchema will be a reference to typeSchema if registration occurs
        Type registrationType = TypeUtil.isOptional(entityType) ? fieldType : entityType;
        registeredTypeSchema = SchemaRegistry.checkRegistration(registrationType, typeResolver, typeSchema);
    }

    Schema fieldSchema;

    if (schemaAnnotation != null) {
        // Handle field annotated with @Schema.
        fieldSchema = readSchemaAnnotatedField(propertyKey, schemaAnnotation, fieldType);
    } else {
        // Use the type's schema for the field as a starting point (poor man's clone)
        fieldSchema = MergeUtil.mergeObjects(new SchemaImpl(), typeSchema);
    }

    BeanValidationScanner.applyConstraints(annotationTarget, fieldSchema, propertyKey, this);

    if (fieldSchema.getNullable() == null && TypeUtil.isOptional(entityType)) {
        fieldSchema.setNullable(Boolean.TRUE);
    }

    // Only when registration was successful (ref is present and the registered type is a different instance)
    if (typeSchema != registeredTypeSchema && registeredTypeSchema.getRef() != null) {
        // Check if the field specifies something additional or different from the type's schema
        if (fieldOverridesType(fieldSchema, typeSchema)) {
            TypeUtil.clearMatchingDefaultAttributes(fieldSchema, typeSchema); // Remove duplicates
            Schema composition = new SchemaImpl();
            composition.addAllOf(registeredTypeSchema); // Reference to the type schema
            composition.addAllOf(fieldSchema);
            fieldSchema = composition;
        } else {
            fieldSchema = registeredTypeSchema; // Reference to the type schema
        }
    } else {
        // Registration did not occur, overlay anything defined by the field on the type's schema
        fieldSchema = MergeUtil.mergeObjects(typeSchema, fieldSchema);
    }

    parentPathEntry.getSchema().addProperty(propertyKey, fieldSchema);
    return fieldSchema;
}
 
Example 7
Source Project: smallrye-open-api   File: SchemaReader.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Reads a {@link Schema} OpenAPI node.
 * 
 * @param node json node
 * @return Schema model
 */
public static Schema readSchema(final JsonNode node) {
    if (node == null || !node.isObject()) {
        return null;
    }
    IoLogging.log.singleJsonObject("Schema");
    String name = JsonUtil.stringProperty(node, SchemaConstant.PROP_NAME);

    Schema schema = new SchemaImpl(name);
    schema.setRef(JsonUtil.stringProperty(node, Referenceable.PROP_$REF));
    schema.setFormat(JsonUtil.stringProperty(node, SchemaConstant.PROP_FORMAT));
    schema.setTitle(JsonUtil.stringProperty(node, SchemaConstant.PROP_TITLE));
    schema.setDescription(JsonUtil.stringProperty(node, SchemaConstant.PROP_DESCRIPTION));
    schema.setDefaultValue(readObject(node.get(SchemaConstant.PROP_DEFAULT)));
    schema.setMultipleOf(JsonUtil.bigDecimalProperty(node, SchemaConstant.PROP_MULTIPLE_OF));
    schema.setMaximum(JsonUtil.bigDecimalProperty(node, SchemaConstant.PROP_MAXIMUM));
    schema.setExclusiveMaximum(JsonUtil.booleanProperty(node, SchemaConstant.PROP_EXCLUSIVE_MAXIMUM).orElse(null));
    schema.setMinimum(JsonUtil.bigDecimalProperty(node, SchemaConstant.PROP_MINIMUM));
    schema.setExclusiveMinimum(JsonUtil.booleanProperty(node, SchemaConstant.PROP_EXCLUSIVE_MINIMUM).orElse(null));
    schema.setMaxLength(JsonUtil.intProperty(node, SchemaConstant.PROP_MAX_LENGTH));
    schema.setMinLength(JsonUtil.intProperty(node, SchemaConstant.PROP_MIN_LENGTH));
    schema.setPattern(JsonUtil.stringProperty(node, SchemaConstant.PROP_PATTERN));
    schema.setMaxItems(JsonUtil.intProperty(node, SchemaConstant.PROP_MAX_ITEMS));
    schema.setMinItems(JsonUtil.intProperty(node, SchemaConstant.PROP_MIN_ITEMS));
    schema.setUniqueItems(JsonUtil.booleanProperty(node, SchemaConstant.PROP_UNIQUE_ITEMS).orElse(null));
    schema.setMaxProperties(JsonUtil.intProperty(node, SchemaConstant.PROP_MAX_PROPERTIES));
    schema.setMinProperties(JsonUtil.intProperty(node, SchemaConstant.PROP_MIN_PROPERTIES));
    schema.setRequired(JsonUtil.readStringArray(node.get(SchemaConstant.PROP_REQUIRED)).orElse(null));
    schema.setEnumeration(JsonUtil.readObjectArray(node.get(SchemaConstant.PROP_ENUM)).orElse(null));
    schema.setType(readSchemaType(node.get(SchemaConstant.PROP_TYPE)));
    schema.setItems(readSchema(node.get(SchemaConstant.PROP_ITEMS)));
    schema.setNot(readSchema(node.get(SchemaConstant.PROP_NOT)));
    schema.setAllOf(readSchemaArray(node.get(SchemaConstant.PROP_ALL_OF)).orElse(null));
    schema.setProperties(readSchemas(node.get(SchemaConstant.PROP_PROPERTIES)).orElse(null));
    if (node.has(SchemaConstant.PROP_ADDITIONAL_PROPERTIES)
            && node.get(SchemaConstant.PROP_ADDITIONAL_PROPERTIES).isObject()) {
        schema.setAdditionalPropertiesSchema(readSchema(node.get(SchemaConstant.PROP_ADDITIONAL_PROPERTIES)));
    } else {
        schema.setAdditionalPropertiesBoolean(
                JsonUtil.booleanProperty(node, SchemaConstant.PROP_ADDITIONAL_PROPERTIES).orElse(null));
    }
    schema.setReadOnly(JsonUtil.booleanProperty(node, SchemaConstant.PROP_READ_ONLY).orElse(null));
    schema.setXml(XmlReader.readXML(node.get(SchemaConstant.PROP_XML)));
    schema.setExternalDocs(ExternalDocsReader.readExternalDocs(node.get(ExternalDocsConstant.PROP_EXTERNAL_DOCS)));
    schema.setExample(readObject(node.get(SchemaConstant.PROP_EXAMPLE)));
    schema.setOneOf(readSchemaArray(node.get(SchemaConstant.PROP_ONE_OF)).orElse(null));
    schema.setAnyOf(readSchemaArray(node.get(SchemaConstant.PROP_ANY_OF)).orElse(null));
    schema.setNot(readSchema(node.get(SchemaConstant.PROP_NOT)));
    schema.setDiscriminator(DiscriminatorReader.readDiscriminator(node.get(SchemaConstant.PROP_DISCRIMINATOR)));
    schema.setNullable(JsonUtil.booleanProperty(node, SchemaConstant.PROP_NULLABLE).orElse(null));
    schema.setWriteOnly(JsonUtil.booleanProperty(node, SchemaConstant.PROP_WRITE_ONLY).orElse(null));
    schema.setDeprecated(JsonUtil.booleanProperty(node, SchemaConstant.PROP_DEPRECATED).orElse(null));
    ExtensionReader.readExtensions(node, schema);
    return schema;
}