Search in sources :

Example 86 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method shouldParseApiWithParametersUsingContentvsSchema.

@Test
public void shouldParseApiWithParametersUsingContentvsSchema() {
    // Tests that the content method of specifying the format of a parameter
    // gets resolved.
    // Test checks if an API's single parameter of array type gets fully resolved to
    // referenced definitions.
    String location = "src/test/resources/issue-1078/api.yaml";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    // This test uses an Array in the parameters, test if it get's fully resolved.
    options.setResolveFully(true);
    OpenAPIV3Parser tested = new OpenAPIV3Parser();
    // Parse yaml
    SwaggerParseResult result = tested.readLocation(location, emptyList(), options);
    OpenAPI api = result.getOpenAPI();
    Paths paths = api.getPaths();
    // First ensure all schemas were resolved, this is important when this library
    // is used to generate code
    Components components = api.getComponents();
    assertNotNull(components);
    assertThat(components.getSchemas().size(), equalTo(4));
    assertNotNull(components.getSchemas().get("LocationType"));
    assertNotNull(components.getSchemas().get("Lat"));
    assertNotNull(components.getSchemas().get("Long"));
    assertNotNull(components.getSchemas().get("SearchResult"));
    PathItem apiEndpoint = paths.get("/api-endpoint-1");
    List<Parameter> parameters = apiEndpoint.getGet().getParameters();
    // Ensure there's only one parameter in this test
    assertThat(parameters.size(), equalTo(1));
    // We are testing content for a parameter so make sure its there.
    Content content = parameters.get(0).getContent();
    assertNotNull(content);
    // spec says only one content is permitted in 3.x
    assertThat(content.size(), equalTo(1));
    // Ensure there's a media type
    MediaType mediaType = content.entrySet().iterator().next().getValue();
    assertNotNull(mediaType);
    // This test has a single parameter of type array
    Schema parameterSchema = mediaType.getSchema();
    Assert.assertTrue(parameterSchema instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) parameterSchema;
    // Test if the item schema was resolved properly
    Schema itemSchema = arraySchema.getItems();
    assertNotNull(itemSchema);
    Assert.assertTrue(itemSchema instanceof ObjectSchema);
    // Ensure the referenced item's schema has been resolved.
    ObjectSchema objSchema = (ObjectSchema) itemSchema;
    Map<String, Schema> objectItemSchemas = objSchema.getProperties();
    assertThat(objectItemSchemas.size(), equalTo(2));
    Assert.assertTrue(objectItemSchemas.get("lat") instanceof IntegerSchema);
    Assert.assertTrue(objectItemSchemas.get("long") instanceof IntegerSchema);
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) MediaType(io.swagger.v3.oas.models.media.MediaType) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 87 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue146.

@Test
public void testIssue146() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/issue_146.yaml");
    assertNotNull(openAPI);
    QueryParameter p = ((QueryParameter) openAPI.getPaths().get("/checker").getGet().getParameters().get(0));
    StringSchema pp = (StringSchema) ((ArraySchema) p.getSchema()).getItems();
    assertTrue("registration".equalsIgnoreCase(pp.getEnum().get(0)));
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 88 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue594.

@Test
public void testIssue594() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + "  /test:\n" + "    post:\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              minItems: 1\n" + "              maxItems: 1\n" + "              items:\n" + "                $ref: \"#/components/schemas/Pet\"\n" + "        description: Hello world\n" + "info:\n" + "  version: ''\n" + "  title: ''";
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, null);
    assertNotNull(result.getOpenAPI());
    ArraySchema schema = (ArraySchema) (result.getOpenAPI().getPaths().get("/test").getPost().getRequestBody().getContent().get("application/json").getSchema());
    assertEquals(schema.getItems().get$ref(), "#/components/schemas/Pet");
    assertNotNull(schema.getMaxItems());
    assertNotNull(schema.getMinItems());
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 89 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method selfReferenceTest.

@Test
public void selfReferenceTest(@Injectable final List<AuthorizationValue> auths) {
    String yaml = "" + "openapi: 3.0.1\n" + "paths:\n" + "  /selfRefA:\n" + "    get:\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/ModelA'\n" + "      responses:\n" + "        default:\n" + "          description: Default response\n" + "  /selfRefB:\n" + "    get:\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/ModelB'\n" + "      responses:\n" + "        default:\n" + "          description: Default response\n" + "  /selfRefC:\n" + "    get:\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/ModelC'\n" + "      responses:\n" + "        default:\n" + "          description: Default response\n" + "  /selfRefD:\n" + "    get:\n" + "      responses:\n" + "        default:\n" + "          description: Default response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                $ref: '#/components/schemas/ModelA'\n" + "  /selfRefE:\n" + "    get:\n" + "      responses:\n" + "        default:\n" + "          description: Default response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: array\n" + "                items:\n" + "                  $ref: '#/components/schemas/ModelA'\n" + "info:\n" + "  version: ''\n" + "  title: ''\n" + "components:\n" + "  schemas:\n" + "    ModelA:\n" + "      properties:\n" + "        modelB:\n" + "          $ref: '#/components/schemas/ModelB'\n" + "    ModelB:\n" + "      properties:\n" + "        modelB:\n" + "          $ref: '#/components/schemas/ModelB'\n" + "    ModelC:\n" + "      properties:\n" + "        modelA:\n" + "          $ref: '#/components/schemas/ModelA'";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml, auths, options).getOpenAPI();
    Schema schemaB = openAPI.getPaths().get("/selfRefB").getGet().getRequestBody().getContent().get("application/json").getSchema();
    assertTrue(schemaB instanceof Schema);
    assertEquals(openAPI.getComponents().getSchemas().get("ModelB"), schemaB);
    Schema schema = openAPI.getPaths().get("/selfRefE").getGet().getResponses().get("default").getContent().get("*/*").getSchema();
    assertTrue(schema instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) schema;
    assertEquals(openAPI.getComponents().getSchemas().get("ModelA"), arraySchema.getItems());
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 90 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method resolveComposedReferenceAllOfSchema.

@Test
public void resolveComposedReferenceAllOfSchema(@Injectable final List<AuthorizationValue> auths) {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/composed.yaml", auths, options).getOpenAPI();
    assertTrue(openAPI.getPaths().get("/withInvalidComposedModelArray").getPost().getRequestBody().getContent().get("application/json").getSchema() instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) openAPI.getPaths().get("/withInvalidComposedModelArray").getPost().getRequestBody().getContent().get("application/json").getSchema();
    assertTrue(arraySchema.getItems() instanceof ObjectSchema);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)107 Schema (io.swagger.v3.oas.models.media.Schema)85 Test (org.testng.annotations.Test)76 StringSchema (io.swagger.v3.oas.models.media.StringSchema)63 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)53 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)53 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)51 OpenAPI (io.swagger.v3.oas.models.OpenAPI)49 MapSchema (io.swagger.v3.oas.models.media.MapSchema)28 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)22 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)21 PathItem (io.swagger.v3.oas.models.PathItem)16 Parameter (io.swagger.v3.oas.models.parameters.Parameter)16 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)16 MediaType (io.swagger.v3.oas.models.media.MediaType)15 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)15 Operation (io.swagger.v3.oas.models.Operation)14 Content (io.swagger.v3.oas.models.media.Content)12 DateSchema (io.swagger.v3.oas.models.media.DateSchema)11 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)11