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

The following examples show how to use io.swagger.v3.oas.models.media.Schema#setMaxItems() . 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: AbstractRequestBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate size.
 *
 * @param annos the annos
 * @param schema the schema
 */
private void calculateSize(Map<String, Annotation> annos, Schema<?> schema) {
	if (annos.containsKey(Size.class.getName())) {
		Size size = (Size) annos.get(Size.class.getName());
		if (OPENAPI_ARRAY_TYPE.equals(schema.getType())) {
			schema.setMinItems(size.min());
			schema.setMaxItems(size.max());
		}
		else if (OPENAPI_STRING_TYPE.equals(schema.getType())) {
			schema.setMinLength(size.min());
			schema.setMaxLength(size.max());
		}
	}
}
 
Example 2
Source File: InputModeller.java    From tcases with MIT License 4 votes vote down vote up
/**
 * Returns the {@link IVarDef input variables} defined by the given array instance.
 */
private IVarDef arrayValueVar( String instanceVarTag, Schema<?> instanceSchema, boolean instanceItem)
  {
  Schema<?> itemSchema =
    Optional.ofNullable( asArraySchema( instanceSchema))
    .map( array -> array.getItems())
    .orElse( null);

  Optional<IVarDef> itemsVar = arrayItemsVar( instanceVarTag, instanceSchema, itemSchema);

  // If the item schema defines a maximum number of possible values...
  Optional<Integer> maxUniqueItems =
    Optional.ofNullable( itemSchema)
    .flatMap( s -> itemsVar)
    .flatMap( items -> Optional.ofNullable( getMaxAlternativeValues( itemSchema)));

  // ... and the items must be unique...
  boolean uniqueRequired = Boolean.TRUE.equals( instanceSchema.getUniqueItems());
  boolean failBelowMinItems = true;
  boolean failAboveMaxItems = true;
  if( uniqueRequired)
    {
    // ... then, if necessary, adjust "minItems"
    if( maxUniqueItems
        .map( maxUnique -> Optional.ofNullable( instanceSchema.getMinItems()).map( minItems -> minItems > maxUnique).orElse( true))
        .orElse( false))
      {
      notifyError(
        String.format( "minItems=%s can exceed the number of unique item values possible", instanceSchema.getMinItems()),
        String.format( "Adjusting to unique minItems=%s", maxUniqueItems.get()));

      instanceSchema.setMinItems( maxUniqueItems.get());
      failBelowMinItems = false;
      }

    // ... and, if necessary, adjust "maxItems"
    if( maxUniqueItems
        .map( maxUnique -> Optional.ofNullable( instanceSchema.getMaxItems()).map( maxItems -> maxItems > maxUnique).orElse( true))
        .orElse( false))
      {
      notifyError(
        String.format( "maxItems=%s can exceed the number of unique item values possible", instanceSchema.getMaxItems()),
        String.format( "Adjusting to unique maxItems=%s", maxUniqueItems.get()));

      instanceSchema.setMaxItems( maxUniqueItems.get());
      failAboveMaxItems = false;
      }
    }

  return
    VarSetBuilder.with( "Items")
    .when( has( instanceValueProperty( instanceVarTag)))
    .members( arraySizeVar( instanceVarTag, instanceSchema, instanceItem, failBelowMinItems, failAboveMaxItems))
    .members( iterableOf( itemsVar))
    .members( iterableOf( arrayUniqueItemsVar( instanceVarTag, instanceSchema)))
    .build();
  }