Java Code Examples for io.swagger.v3.oas.models.media.ArraySchema#setItems()

The following examples show how to use io.swagger.v3.oas.models.media.ArraySchema#setItems() . 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: InlineModelResolver.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("static-method")
public Schema modelFromProperty(ArraySchema object, @SuppressWarnings("unused") String path) {
    String description = object.getDescription();
    String example = null;

    Object obj = object.getExample();
    if (obj != null) {
        example = obj.toString();
    }

    Schema inner = object.getItems();
    if (inner instanceof ObjectSchema) {
        ArraySchema model = new ArraySchema();
        model.setDescription(description);
        model.setExample(example);
        model.setItems(object.getItems());
        return model;
    }

    return null;
}
 
Example 2
Source File: InlineModelResolver.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("static-method")
public Schema modelFromProperty(Schema object, @SuppressWarnings("unused") String path) {
    String description = object.getDescription();
    String example = null;

    Object obj = object.getExample();
    if (obj != null) {
        example = obj.toString();
    }
    ArraySchema model = new ArraySchema();
    model.setDescription(description);
    model.setExample(example);
    if (object.getAdditionalProperties() != null && !(object.getAdditionalProperties() instanceof Boolean)) {
        model.setItems((Schema)  object.getAdditionalProperties());
    }
    return model;
}
 
Example 3
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineArrayModelWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("InnerUserTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    ArraySchema arraySchema =  new ArraySchema();
    List<String> required = new LinkedList<>();
    required.add("name");
    arraySchema.setRequired(required);
    arraySchema.setItems(objectSchema);


    openAPI.getComponents().addSchemas("User", arraySchema);


    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);

    Schema user = openAPI.getComponents().getSchemas().get("InnerUserTitle");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
}
 
Example 4
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineArrayModelWithoutTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema objectSchema = new ObjectSchema();
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    ArraySchema arraySchema =  new ArraySchema();
    List<String> required = new LinkedList<>();
    required.add("name");
    arraySchema.setRequired(required);
    arraySchema.setItems(objectSchema);

    openAPI.getComponents().addSchemas("User", arraySchema);


    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);

    Schema user = openAPI.getComponents().getSchemas().get("User_inner");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
}
 
Example 5
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayResponse() {
    OpenAPI openAPI = new OpenAPI();


    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("name", new StringSchema());
    ArraySchema schema = new ArraySchema();
    schema.setItems(objectSchema);

    ApiResponse apiResponse = new ApiResponse();
    apiResponse.addExtension("x-foo", "bar");
    apiResponse.setDescription("it works!");
    apiResponse.setContent(new Content().addMediaType("*/*", new MediaType().schema(schema)));

    openAPI.path("/foo/baz", new PathItem()
            .get(new Operation()
                    .responses(new ApiResponses().addApiResponse("200", apiResponse))));

    new InlineModelResolver().flatten(openAPI);

    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema);

    ArraySchema am = (ArraySchema) response.getContent().get("*/*").getSchema();
    Schema items = am.getItems();
    assertTrue(items.get$ref() != null);

    assertEquals(items.get$ref(), "#/components/schemas/inline_response_200");


    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertTrue(inline instanceof Schema);

    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
 
Example 6
Source File: SchemaAnalyzer.java    From tcases with MIT License 4 votes vote down vote up
/**
 * Resolves the items schema for an array schema.
 */
private void resolveItems( OpenAPI api, ArraySchema array)
  {
  array.setItems( resultFor( "items", () -> resolve( api, array.getItems())));
  }
 
Example 7
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.
 */
private static Schema<?> combineArraySchemas( OpenApiContext context, Schema<?> base, Schema<?> additional)
  {
  ArraySchema combined = combineGenericSchemas( context, new ArraySchema(), base, additional);

  // Combine maxItems
  combined.setMaxItems(
    base.getMaxItems() == null?
    additional.getMaxItems() :

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

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

    additional.getMaxItems());

  // Combine minItems
  combined.setMinItems(
    base.getMinItems() == null?
    additional.getMinItems() :

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

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

    additional.getMinItems());

  // Combine uniqueItems
  combined.setUniqueItems(
    base.getUniqueItems() == null?
    additional.getUniqueItems() :

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

    combineAssertions( "uniqueItems: %s", base.getUniqueItems(), additional.getUniqueItems()));

  // Combine items
  Schema<?> baseItems = Optional.ofNullable( asArraySchema( base)).map( ArraySchema::getItems).orElse( null);
  Schema<?> additionalItems = Optional.ofNullable( asArraySchema( additional)).map( ArraySchema::getItems).orElse( null);    
  combined.setItems( context.resultFor( "items", () -> combineSchemas( context, baseItems, additionalItems)));     

  return combined;
  }
 
Example 8
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 9
Source File: SchemaProcessorTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testProcessArraySchema() throws Exception {

    Schema property = new Schema();
    SchemaProcessor propertyProcessor = new SchemaProcessor(cache, openAPI);
    propertyProcessor.processSchema(property);


    ArraySchema model = new ArraySchema();
    model.setItems(property);

    SchemaProcessor schemaProcessor = new SchemaProcessor(cache, openAPI);
    schemaProcessor.processSchema(model);

    assertEquals(property,model.getItems());

}
 
Example 10
Source File: SchemaProcessorTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testProcessArrayProperty_ItemsIsRefProperty() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final Schema refProperty = new Schema().$ref(ref);

    ArraySchema arrayProperty = new ArraySchema();
    arrayProperty.setItems(refProperty);

    expectCallToExternalRefProcessor(ref, RefFormat.URL, "bar");

    new SchemaProcessor(cache, openAPI).processSchema(arrayProperty);

    new FullVerifications() {{
    }};

    assertEquals((arrayProperty.getItems()).get$ref(), "#/components/schemas/bar");

}