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

The following examples show how to use org.eclipse.microprofile.openapi.models.media.Schema#setPattern() . 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: JaxRsAnnotationScannerTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@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 2
Source File: TypeUtil.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the default schema attributes for the given type on the provided schema
 * instance.
 * 
 * @param classType the type
 * @param schema a writable schema to be updated with the type's default schema attributes
 */
public static void applyTypeAttributes(Type classType, Schema schema) {
    Map<String, Object> properties = getTypeAttributes(classType);

    schema.setType((SchemaType) properties.get(SchemaConstant.PROP_TYPE));
    schema.setFormat((String) properties.get(SchemaConstant.PROP_FORMAT));
    schema.setPattern((String) properties.get(SchemaConstant.PROP_PATTERN));
    schema.setExample(properties.get(SchemaConstant.PROP_EXAMPLE));
    schema.setExternalDocs((ExternalDocumentation) properties.get(ExternalDocsConstant.PROP_EXTERNAL_DOCS));
}
 
Example 3
Source File: BeanValidationScanner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
void digits(AnnotationTarget target, Schema schema) {
    AnnotationInstance constraint = getConstraint(target, BV_DIGITS);

    if (constraint != null && schema.getPattern() == null) {
        // Both attributes are required - safe to use primitives.
        final int integerPart = intValue(constraint, "integer");
        final int fractionPart = intValue(constraint, "fraction");
        final StringBuilder pattern = new StringBuilder(50);

        pattern.append('^');

        if (integerPart > 0) {
            pattern.append("\\d");

            if (integerPart > 1) {
                pattern.append("{1,").append(integerPart).append('}');
            }
        }

        if (fractionPart > 0) {
            pattern.append("([.]\\d");

            if (fractionPart > 1) {
                pattern.append("{1,").append(fractionPart).append("}");
            }

            pattern.append(")?");
        }

        pattern.append('$');
        schema.setPattern(pattern.toString());
    }
}
 
Example 4
Source File: BeanValidationScanner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
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 5
Source File: SchemaReader.java    From smallrye-open-api with 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;
}