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

The following examples show how to use io.swagger.v3.oas.models.media.Schema#setMaximum() . 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: SchemaGeneratorHelper.java    From spring-openapi with MIT License 5 votes vote down vote up
protected void applyNumberAnnotation(Schema<?> schema, Annotation annotation) {
    if (annotation instanceof DecimalMin) {
        schema.setMinimum(new BigDecimal(((DecimalMin) annotation).value()));
    } else if (annotation instanceof DecimalMax) {
        schema.setMaximum(new BigDecimal(((DecimalMax) annotation).value()));
    } else if (annotation instanceof Min) {
        schema.setMinimum(BigDecimal.valueOf(((Min) annotation).value()));
    } else if (annotation instanceof Max) {
        schema.setMaximum(BigDecimal.valueOf(((Max) annotation).value()));
    }
}
 
Example 2
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidIntegerMaximum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new Schema();
    schema.setMaximum(new BigDecimal("10.0"));
    parameter.setSchema(schema);

    converter.validate(new Integer(9), parameter);
}
 
Example 3
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testInvalidIntegerMaximum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new Schema();
    schema.setMaximum(new BigDecimal("10.0"));
    parameter.setSchema(schema);

    converter.validate(new Integer(11), parameter);
}
 
Example 4
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidIntegerExclusiveMaximum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new Schema();
    schema.setMaximum(new BigDecimal("10.0"));
    schema.setExclusiveMaximum(true);
    parameter.setSchema(schema);

    InputConverter.getInstance().validate(new Integer(9), parameter);
}
 
Example 5
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testInvalidIntegerExclusiveMaximum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new Schema();
    schema.setMaximum(new BigDecimal("10"));
    schema.setExclusiveMaximum(true);
    parameter.setSchema(schema);

    InputConverter.getInstance().validate(new Integer(11), parameter);
}
 
Example 6
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testInvalidIntegerExclusiveMaximumEquality() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new Schema();
    schema.setMaximum(new BigDecimal("10.0"));
    schema.setExclusiveMaximum(true);
    parameter.setSchema(schema);

    InputConverter.getInstance().validate(new Integer(10), parameter);
}
 
Example 7
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testIssue127_b() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new Schema();
    schema.setMaximum(new BigDecimal("10"));
    schema.setExclusiveMaximum(true);
    parameter.setSchema(schema);

    InputConverter.getInstance().validate("value 1", parameter);
}
 
Example 8
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 9
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<?> combineNumericSchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
  {
  Schema combined = combineGenericSchemas( context, base, additional);

  // Combine maximum
  combined.setMaximum(
    base.getMaximum() == null?
    additional.getMaximum() :

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

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

    additional.getMaximum());
  
  // Combine minimum
  combined.setMinimum(
    base.getMinimum() == null?
    additional.getMinimum() :

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

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

    additional.getMinimum());
  
  // Combine exclusiveMaximum
  combined.setExclusiveMaximum(
    base.getExclusiveMaximum() == null?
    additional.getExclusiveMaximum() :

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

    combineAssertions( "exclusiveMaximum: %s", base.getExclusiveMaximum(), additional.getExclusiveMaximum()));

    
  // Combine exclusiveMinimum
  combined.setExclusiveMinimum(
    base.getExclusiveMinimum() == null?
    additional.getExclusiveMinimum() :

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

    combineAssertions( "exclusiveMinimum: %s", base.getExclusiveMinimum(), additional.getExclusiveMinimum()));

  // Combine multipleOf
  combined.setMultipleOf(
    base.getMultipleOf() == null?
    additional.getMultipleOf() :

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

    combineMultipleOf( base.getMultipleOf(), additional.getMultipleOf())); 

  // Combine not multipleOfs
  setNotMultipleOfs( combined, getNotMultipleOfs( base));
  addNotMultipleOfs( combined, getNotMultipleOfs( additional));

  Optional.ofNullable( combined.getMultipleOf())
    .flatMap( m -> Optional.ofNullable( getNotMultipleOfs( combined)).flatMap( nms -> nms.stream().filter( nm -> isMultipleOf( m, nm)).findFirst()))
    .ifPresent( nm -> {
      throw inconsistentNotAssertion( "multipleOf: %s", combined.getMultipleOf(), nm);
      });

  return combined;
  }