Java Code Examples for io.swagger.v3.oas.models.media.Schema#getFormat()

The following examples show how to use io.swagger.v3.oas.models.media.Schema#getFormat() . 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: OpenApiClientGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private FieldSpec.Builder createNumberBasedFieldWithFormat(String fieldName, Schema innerSchema, TypeSpec.Builder typeSpecBuilder) {
	if (innerSchema.getFormat() == null || equalsIgnoreCase(innerSchema.getFormat(), "int32")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Integer", fieldName, typeSpecBuilder);
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "int64")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Long", fieldName, typeSpecBuilder);
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "float")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Float", fieldName, typeSpecBuilder);
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "double")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Double", fieldName, typeSpecBuilder);
	} else {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Integer", fieldName, typeSpecBuilder);
	}
}
 
Example 2
Source File: OpenApiClientGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private FieldSpec.Builder getStringBasedSchemaField(String fieldName, Schema innerSchema, TypeSpec.Builder typeSpecBuilder) {
	if (innerSchema.getFormat() == null) {
		ClassName stringClassName = ClassName.get(JAVA_LANG_PKG, "String");
		FieldSpec.Builder fieldBuilder = FieldSpec.builder(stringClassName, fieldName, Modifier.PRIVATE);
		if (innerSchema.getPattern() != null) {
			fieldBuilder.addAnnotation(AnnotationSpec.builder(Pattern.class)
					.addMember("regexp", "$S", innerSchema.getPattern())
					.build()
			);
		}
		if (innerSchema.getMinLength() != null || innerSchema.getMaxLength() != null) {
			AnnotationSpec.Builder sizeAnnotationBuilder = AnnotationSpec.builder(Size.class);
			if (innerSchema.getMinLength() != null) {
				sizeAnnotationBuilder.addMember("min", "$L", innerSchema.getMinLength());
			}
			if (innerSchema.getMaxLength() != null) {
				sizeAnnotationBuilder.addMember("max", "$L", innerSchema.getMaxLength());
			}
			fieldBuilder.addAnnotation(sizeAnnotationBuilder.build());
		}
		enrichWithGetSet(typeSpecBuilder, stringClassName, fieldName);
		return fieldBuilder;
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "date")) {
		ClassName localDateClassName = ClassName.get(JAVA_TIME_PKG, "LocalDate");
		enrichWithGetSet(typeSpecBuilder, localDateClassName, fieldName);
		return FieldSpec.builder(localDateClassName, fieldName, Modifier.PRIVATE);
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "date-time")) {
		ClassName localDateTimeClassName = ClassName.get(JAVA_TIME_PKG, "LocalDateTime");
		enrichWithGetSet(typeSpecBuilder, localDateTimeClassName, fieldName);
		return FieldSpec.builder(localDateTimeClassName, fieldName, Modifier.PRIVATE);
	}
	throw new IllegalArgumentException(String.format("Error parsing string based property [%s]", fieldName));
}
 
Example 3
Source File: ClientGeneratorUtils.java    From spring-openapi with MIT License 5 votes vote down vote up
public static ClassName getStringGenericClassName(Schema<?> genericSchema) {
	if (genericSchema.getFormat() == null) {
		return ClassName.get(JAVA_LANG_PKG, "String");
	} else if (equalsIgnoreCase(genericSchema.getFormat(), "date")) {
		return ClassName.get(JAVA_TIME_PKG, "LocalDate");
	} else if (equalsIgnoreCase(genericSchema.getFormat(), "date-time")) {
		return ClassName.get(JAVA_TIME_PKG, "LocalDateTime");
	}
	throw new IllegalArgumentException("Error parsing string based property");
}
 
Example 4
Source File: ClientGeneratorUtils.java    From spring-openapi with MIT License 5 votes vote down vote up
public static ClassName getNumberGenericClassName(Schema<?> genericSchema) {
	if (genericSchema.getFormat() == null || StringUtils.equalsIgnoreCase(genericSchema.getFormat(), "int32")) {
		return ClassName.get(JAVA_LANG_PKG, "Integer");
	} else if (StringUtils.equalsIgnoreCase(genericSchema.getFormat(), "int64")) {
		return ClassName.get(JAVA_LANG_PKG, "Long");
	} else if (StringUtils.equalsIgnoreCase(genericSchema.getFormat(), "float")) {
		return ClassName.get(JAVA_LANG_PKG, "Float");
	} else if (StringUtils.equalsIgnoreCase(genericSchema.getFormat(), "double")) {
		return ClassName.get(JAVA_LANG_PKG, "Double");
	} else {
		return ClassName.get(JAVA_LANG_PKG, "Integer");
	}
}
 
Example 5
Source File: ResourceInterfaceGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private ClassName getStringGenericClassName(Schema<?> genericSchema) {
	if (genericSchema.getFormat() == null) {
		return ClassName.get(JAVA_LANG_PKG, "String");
	} else if (equalsIgnoreCase(genericSchema.getFormat(), "date")) {
		return ClassName.get(JAVA_TIME_PKG, "LocalDate");
	} else if (equalsIgnoreCase(genericSchema.getFormat(), "date-time")) {
		return ClassName.get(JAVA_TIME_PKG, "LocalDateTime");
	}
	throw new IllegalArgumentException("Error parsing string based property");
}
 
Example 6
Source File: ResourceInterfaceGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private ParameterSpec.Builder createNumberBasedParameterWithFormat(String fieldName, Schema innerSchema) {
	if (innerSchema.getFormat() == null || StringUtils.equalsIgnoreCase(innerSchema.getFormat(), "int32")) {
		return createSimpleParameterSpec(JAVA_LANG_PKG, "Integer", fieldName);
	} else if (StringUtils.equalsIgnoreCase(innerSchema.getFormat(), "int64")) {
		return createSimpleParameterSpec(JAVA_LANG_PKG, "Long", fieldName);
	} else if (StringUtils.equalsIgnoreCase(innerSchema.getFormat(), "float")) {
		return createSimpleParameterSpec(JAVA_LANG_PKG, "Float", fieldName);
	} else if (StringUtils.equalsIgnoreCase(innerSchema.getFormat(), "double")) {
		return createSimpleParameterSpec(JAVA_LANG_PKG, "Double", fieldName);
	} else {
		return createSimpleParameterSpec(JAVA_LANG_PKG, "Integer", fieldName);
	}
}
 
Example 7
Source File: OpenApiHelpers.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
public static String getSchemaTypeAsString(Schema schema) {
    StringBuilder stringBuilder = new StringBuilder();
    if (schema instanceof ArraySchema) {
        stringBuilder.append("< ");
        Schema<?> items = ((ArraySchema) schema).getItems();
        stringBuilder.append(getSchemaType(items));
        stringBuilder.append(" > ");
        stringBuilder.append(schema.getType());
    } else {
        List enumList = schema.getEnum();
        if (enumList != null) {
            stringBuilder.append("enum (");
            for (Object value : enumList) {
                stringBuilder.append(value.toString());
                stringBuilder.append(",");
            }
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            stringBuilder.append(')');
        } else {
            stringBuilder.append(getSchemaType(schema));
            String format = schema.getFormat();
            if (format != null) {
                stringBuilder.append(' ');
                stringBuilder.append('(');
                stringBuilder.append(format);
                stringBuilder.append(')');
            }
        }
    }
    return stringBuilder.toString();
}
 
Example 8
Source File: TypeFormat.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
public TypeFormat(Schema schema) {
  this.type = schema.getType();
  this.format = schema.getFormat();
}
 
Example 9
Source File: OASMergeUtil.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
public static Schema mergeSchema(Schema thisSchema, Schema thatSchema) {
  if (thatSchema == null) {
    return thisSchema;
  }
  // Overwriting `implementation` is explicitly disallowed
  // Overwriting `not` is explicitly disallowed
  // Overwriting `oneOf` is explicitly disallowed
  // Overwriting `anyOf` is explicitly disallowed
  // Overwriting `allOf` is explicitly disallowed
  // Overwriting `name` is explicitly disallowed
  if (thatSchema.getTitle() != null) {
    thisSchema.setTitle(thatSchema.getTitle());
  }
  // Overwriting `multipleOf` is explicitly disallowed
  if (thatSchema.getMaximum() != null) {
    thisSchema.setMaximum(thatSchema.getMaximum());
  }
  if (thatSchema.getExclusiveMaximum() != null) {
    thisSchema.setExclusiveMaximum(thatSchema.getExclusiveMaximum());
  }

  if (thatSchema.getMinimum() != null) {
    thisSchema.setMinimum(thatSchema.getMinimum());
  }
  if (thatSchema.getExclusiveMinimum() != null) {
    thisSchema.setExclusiveMinimum(thatSchema.getExclusiveMinimum());
  }
  if (thatSchema.getMaxLength() != null) {
    thisSchema.setMaxLength(thatSchema.getMaxLength());
  }
  if (thatSchema.getMinLength() != null) {
    thisSchema.setMinLength(thatSchema.getMinLength());
  }
  if (thatSchema.getPattern() != null) {
    thisSchema.setPattern(thatSchema.getPattern());
  }
  if (thatSchema.getMaxProperties() != null) {
    thisSchema.setMaxProperties(thatSchema.getMaxProperties());
  }
  if (thatSchema.getMinProperties() != null) {
    thisSchema.setMinProperties(thatSchema.getMinProperties());
  }
  // RequiredProperties
  if (thatSchema.getRequired() != null) {
    thisSchema.setRequired(thatSchema.getRequired());
  }
  // Overwriting `name` is explicitly disallowed
  if (thatSchema.getDescription() != null) {
    thisSchema.setDescription(thatSchema.getDescription());
  }
  if (thatSchema.getFormat() != null) {
    thisSchema.setFormat(thatSchema.getFormat());
  }
  // Overwriting `ref` is explicitly disallowed
  if (thatSchema.getNullable() != null) {
    thisSchema.setNullable(thatSchema.getNullable());
  }
  // Overwriting `AccessMode` is explicitly disallowed
  if (thatSchema.getExample() != null) {
    thisSchema.setExample(thatSchema.getExample());
  }
  if (thatSchema.getExternalDocs() != null) {
    thisSchema.setExternalDocs(thatSchema.getExternalDocs());
  }
  if (thatSchema.getDeprecated() != null) {
    thisSchema.setDeprecated(thatSchema.getDeprecated());
  }
  if (thatSchema.getType() != null) {
    thisSchema.setType(thatSchema.getType());
  }
  if (thatSchema.getEnum() != null) {
    thisSchema.setEnum(thatSchema.getEnum());
  }
  if (thatSchema.getDefault() != null) {
    thisSchema.setDefault(thatSchema.getDefault());
  }
  // Overwriting `discriminator` is explicitly disallowed
  // Overwriting `hidden` is explicitly disallowed
  // Overwriting `subTypes` is explicitly disallowed
  if (thatSchema.getExtensions() != null) {
    thisSchema.setExtensions(thatSchema.getExtensions());
  }
  return thisSchema;
}
 
Example 10
Source File: SchemaUtils.java    From tcases with MIT License 4 votes vote down vote up
/**
 * Returns a new schema that validates any instance that satisfies both the base schema and the additional schema.
 * Throws an exception if a consistent combination is not possible.
 */
@SuppressWarnings("rawtypes")
private static Schema<?> combineStringSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
  {
  Schema combined = combineGenericSchemas( context, base, additional);

  // Combine format
  if( base.getFormat() != null && additional.getFormat() != null && !base.getFormat().equals( additional.getFormat()))
    {
    throw inconsistentAssertions( "format: %s", additional.getFormat(), base.getFormat());
    }
  
  // Combine maxLength
  combined.setMaxLength(
    base.getMaxLength() == null?
    additional.getMaxLength() :

    additional.getMaxLength() == null?
    base.getMaxLength() :

    base.getMaxLength().compareTo( additional.getMaxLength()) < 0?
    base.getMaxLength() :

    additional.getMaxLength());

  // Combine minLength
  combined.setMinLength(
    base.getMinLength() == null?
    additional.getMinLength() :

    additional.getMinLength() == null?
    base.getMinLength() :

    base.getMinLength().compareTo( additional.getMinLength()) > 0?
    base.getMinLength() :

    additional.getMinLength());

  // Combine pattern
  setPatterns( combined, getPatterns( base));
  addPatterns( combined, getPatterns( additional));

  // Combine not patterns
  setNotPatterns( combined, getNotPatterns( base));
  addNotPatterns( combined, getNotPatterns( additional));

  Optional.ofNullable( getPatterns( combined))
    .flatMap( ps -> Optional.ofNullable( getNotPatterns( combined)).flatMap( nps -> ps.stream().filter( p -> nps.contains( p)).findFirst()))
    .ifPresent( p -> {
      throw inconsistentNotAssertion( "pattern: '%s'", p);
      });
    
  return combined;
  }