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

The following examples show how to use io.swagger.v3.oas.models.media.Schema#setMinLength() . 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: ExampleBuilderTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplexArray() throws Exception {
    Map<String, Schema> definitions = new HashMap<>();

    Schema address = new Schema()
            .xml(new XML().name("address"));

    Schema propertyDefinition1 = new StringSchema();
    propertyDefinition1.setName("street");
    propertyDefinition1.setExample("12345 El Monte Blvd");

    Schema propertyDefinition2 = new StringSchema();
    propertyDefinition2.setName("city");
    propertyDefinition2.setExample("Los Altos Hills");

    Schema propertyDefinition3 = new StringSchema();
    propertyDefinition3.setName("state");
    propertyDefinition3.setExample("CA");
    propertyDefinition3.setMinLength(2);
    propertyDefinition3.setMaxLength(2);

    Schema propertyDefinition4 = new StringSchema();
    propertyDefinition4.setName("zip");
    propertyDefinition4.setExample("94022");

    address.addProperties("street",propertyDefinition1);
    address.addProperties("city",propertyDefinition2);
    address.addProperties("state",propertyDefinition3);
    address.addProperties("zip",propertyDefinition4);

    definitions.put("Address", address);

    Example rep = (Example) ExampleBuilder.fromSchema(new ArraySchema().$ref("Address"), definitions);

    String json = Json.pretty(rep);

    assertEqualsIgnoreLineEnding(json,"{\n  \"street\" : \"12345 El Monte Blvd\",\n  \"city\" : \"Los Altos Hills\",\n  \"state\" : \"CA\",\n  \"zip\" : \"94022\"\n}");
}
 
Example 3
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 4
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;
  }
 
Example 5
Source File: ExampleBuilderTest.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
@Test
public void testXmlJackson() throws Exception {
    Schema model = new Schema()
            .xml(new XML()
                    .name("user"));

    Schema property1 = new StringSchema();
    property1.setName("username");
    property1.setExample("fehguy");
    property1.setXml(new XML()
            .name("userName"));

    ArraySchema property2 = new ArraySchema();
    property2.setName("addresses");
    property2.setXml(new XML().wrapped(true));
    property2.setItems(new Schema().$ref("Address"));
    Schema property3 = new Schema();
    property3.setName("managers");
    property3.setAdditionalProperties(new StringSchema().example("SVP Engineering"));
    ArraySchema property4 = new ArraySchema();
    property4.setName("kidsAges");
    property4.setItems(new IntegerSchema().example(9));

    model.addProperties("username",property1);
    model.addProperties("addresses",property2);
    model.addProperties("managers",property3);
    model.addProperties("kidsAges",property4);

    Map<String, Schema> definitions = new HashMap<>();
    definitions.put("User", model);

    Schema address = new Schema()
            .xml(new XML().name("address"));

    Schema propertyDefinition1 = new StringSchema();
    propertyDefinition1.setName("street");
    propertyDefinition1.setExample("12345 El Monte Blvd");

    Schema propertyDefinition2 = new StringSchema();
    propertyDefinition2.setName("city");
    propertyDefinition2.setExample("Los Altos Hills");

    Schema propertyDefinition3 = new StringSchema();
    propertyDefinition3.setName("state");
    propertyDefinition3.setExample("CA");
    propertyDefinition3.setMinLength(2);
    propertyDefinition3.setMaxLength(2);

    Schema propertyDefinition4 = new StringSchema();
    propertyDefinition4.setName("zip");
    propertyDefinition4.setExample("94022");

    address.addProperties("street",propertyDefinition1);
    address.addProperties("city",propertyDefinition2);
    address.addProperties("state",propertyDefinition3);
    address.addProperties("zip",propertyDefinition4);

    definitions.put("Address", address);

    Example rep = ExampleBuilder.fromSchema(new Schema().$ref("User"), definitions);

    String xmlString = new XmlExampleSerializer().serialize(rep);
    assertEqualsIgnoreLineEnding(xmlString, "<?xml version='1.1' encoding='UTF-8'?><user><userName>fehguy</userName><addressess><address><street>12345 El Monte Blvd</street><city>Los Altos Hills</city><state>CA</state><zip>94022</zip></address></addressess><managers><additionalProp1>SVP Engineering</additionalProp1><additionalProp2>SVP Engineering</additionalProp2><additionalProp3>SVP Engineering</additionalProp3></managers><kidsAges>9</kidsAges></user>");
    assertEqualsIgnoreLineEnding(Yaml.pretty().writeValueAsString(rep),"username: fehguy\naddresses:\n- street: 12345 El Monte Blvd\n  city: Los Altos Hills\n  state: CA\n  zip: \"94022\"\nmanagers:\n  additionalProp1: SVP Engineering\n  additionalProp2: SVP Engineering\n  additionalProp3: SVP Engineering\nkidsAges:\n- 9\n");
}
 
Example 6
Source File: ExampleBuilderTest.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
@Test
public void testComplexArrayWithExample() throws Exception {
    Map<String, Schema> definitions = new HashMap<>();

    Schema address = new Schema()
      .example("{\"foo\":\"bar\"}")
      .xml(new XML()
        .name("address"));

    Schema propertyDefinition1 = new StringSchema();
    propertyDefinition1.setName("street");
    propertyDefinition1.setExample("12345 El Monte Blvd");

    Schema propertyDefinition2 = new StringSchema();
    propertyDefinition2.setName("city");
    propertyDefinition2.setExample("Los Altos Hills");

    Schema propertyDefinition3 = new StringSchema();
    propertyDefinition3.setName("state");
    propertyDefinition3.setExample("CA");
    propertyDefinition3.setMinLength(2);
    propertyDefinition3.setMaxLength(2);

    Schema propertyDefinition4 = new StringSchema();
    propertyDefinition4.setName("zip");
    propertyDefinition4.setExample("94022");

    address.addProperties("street",propertyDefinition1);
    address.addProperties("city",propertyDefinition2);
    address.addProperties("state",propertyDefinition3);
    address.addProperties("zip",propertyDefinition4);


    definitions.put("Address", address);

    // register the JSON serializer
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(new JsonNodeExampleSerializer());
    simpleModule.addDeserializer(Example.class, new JsonExampleDeserializer());
    Json.mapper().registerModule(simpleModule);

    Example rep = (Example) ExampleBuilder.fromSchema(new StringSchema().addEnumItem("hello").example("fun"), definitions);
    assertEqualsIgnoreLineEnding(Json.pretty(rep), "\"fun\"");
}