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

The following examples show how to use io.swagger.v3.oas.models.media.Schema#setAdditionalProperties() . 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: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testArbitraryObjectResponseMapInline() {
    OpenAPI openAPI = new OpenAPI();

    Schema schema = new Schema();
    schema.setAdditionalProperties(new ObjectSchema());

    openAPI.path("/foo/baz", new PathItem()
            .get(new Operation()
                    .responses(new ApiResponses().addApiResponse("200", new ApiResponse()
                            .description("it works!")
                            .content(new Content().addMediaType("*/*", new MediaType().schema(schema)))))));

    new InlineModelResolver().flatten(openAPI);

    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");

    Schema property = response.getContent().get("*/*").getSchema();
    assertTrue(property.getAdditionalProperties() != null);
    assertTrue(property.getAdditionalProperties() instanceof  Schema);
    assertTrue(openAPI.getComponents().getSchemas() == null);
    Schema inlineProp = (Schema)property.getAdditionalProperties();
    assertTrue(inlineProp instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) inlineProp;
    assertNull(op.getProperties());
}
 
Example 2
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testInlineMapResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();

    Schema schema = new Schema();
    schema.setAdditionalProperties(new StringSchema());
    schema.addExtension("x-ext", "ext-prop");

    ApiResponse apiResponse = new ApiResponse();
    apiResponse.description("it works!");
    MediaType mediaType = new MediaType();
    mediaType.setSchema(schema);

    Content content = new Content();
    content.addMediaType("*/*",mediaType);

    apiResponse.setContent(content);
    apiResponse.addExtension("x-foo", "bar");

    ApiResponses apiResponses = new ApiResponses();
    apiResponses.addApiResponse("200",apiResponse);


    openAPI.path("/foo/baz", new PathItem()
            .get(new Operation()
                    .responses(apiResponses)));


    new InlineModelResolver().flatten(openAPI);

    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");

    Schema property = response.getContent().get("*/*").getSchema();
    assertTrue(property.getAdditionalProperties() != null);
    assertTrue(openAPI.getComponents().getSchemas() == null);
    assertEquals(1, property.getExtensions().size());
    assertEquals("ext-prop", property.getExtensions().get("x-ext"));
}
 
Example 3
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testInlineMapResponseWithObjectSchema() throws Exception {
    OpenAPI openAPI = new OpenAPI();

    Schema schema = new Schema();
    schema.setAdditionalProperties(new ObjectSchema()
            .addProperties("name", new StringSchema()));
    schema.addExtension("x-ext", "ext-prop");

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

    ApiResponses apiResponses = new ApiResponses().addApiResponse("200",apiResponse);

    openAPI.path("/foo/baz", new PathItem()
            .get(new Operation()
                    .responses(apiResponses)));

    new InlineModelResolver().flatten(openAPI);

    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    Schema property = response.getContent().get("*/*").getSchema();
    assertTrue(property.getAdditionalProperties() != null);
    assertEquals(1, property.getExtensions().size());
    assertEquals("ext-prop", property.getExtensions().get("x-ext"));
    assertTrue(openAPI.getComponents().getSchemas().size() == 1);

    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_map200");
    assertTrue(inline instanceof Schema);
    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
 
Example 4
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 5
Source File: SchemaProcessorTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testProcessMapProperty_AdditionalPropertiesIsRefProperty() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final Schema refProperty = new Schema().$ref(ref);


    refProperty.setAdditionalProperties(refProperty);

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

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

    new FullVerifications() {{
    }};

    assertEquals((((Schema)refProperty.getAdditionalProperties()).get$ref()), "#/components/schemas/bar");
}