Search in sources :

Example 26 with Content

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

the class OpenAPIV3ParserTest method testIncompatibleRefs.

@Test
public void testIncompatibleRefs() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + "  /test:\n" + "    post:\n" + "      responses:\n" + "        '200':\n" + "          $ref: '#/components/schemas/Schema'\n" + "        '400':\n" + "          definitions: this is right\n" + "          description: Bad Request\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                $ref: '#/components/schemas/Schema'\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/Schema'\n" + "        required: true\n" + "info:\n" + "  version: ''\n" + "  title: ''\n" + "components:\n" + "  schemas:\n" + "    Schema: {}";
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, null);
    assertNotNull(result.getOpenAPI());
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 27 with Content

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

the class OpenAPIV3ParserTest method testIssueSameRefsDifferentModel.

@Test
public void testIssueSameRefsDifferentModel() throws IOException {
    String pathFile = FileUtils.readFileToString(new File("src/test/resources/same-refs-different-model-domain.yaml"), "UTF-8");
    WireMock.stubFor(get(urlPathMatching("/issue-domain")).willReturn(aResponse().withStatus(HttpURLConnection.HTTP_OK).withHeader("Content-type", "application/json").withBody(pathFile.getBytes(StandardCharsets.UTF_8))));
    pathFile = FileUtils.readFileToString(new File("src/test/resources/same-refs-different-model.yaml"), "UTF-8");
    pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    final SwaggerParseResult openAPI = parser.readContents(pathFile, null, options);
    Yaml.prettyPrint(openAPI);
    assertEquals(openAPI.getMessages().size(), 0);
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) File(java.io.File) Test(org.testng.annotations.Test)

Example 28 with Content

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

the class OpenAPIV3ParserTest method testConverterIssue17.

@Test
public void testConverterIssue17() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "info:\n" + "  version: 0.0.0\n" + "  title: nada\n" + "paths:\n" + "  /persons:\n" + "    get:\n" + "      parameters:\n" + "        - name: testParam\n" + "          in: query\n" + "          style: form\n" + "          schema:\n" + "            type: array\n" + "            items:\n" + "              type: string\n" + "      responses:\n" + "        '200':\n" + "          description: Successful response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                $ref: '#/components/schemas/Content'\n" + "components:\n" + "  schemas:\n" + "    Content:\n" + "      type: object";
    ParseOptions options = new ParseOptions();
    options.setResolve(false);
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, options);
    assertNotNull(result.getOpenAPI());
    assertEquals((result.getOpenAPI().getPaths().get("/persons").getGet().getResponses().get("200").getContent().get("*/*").getSchema()).get$ref(), "#/components/schemas/Content");
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 29 with Content

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

the class InlineModelResolverTest method testInlineMapResponse.

@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"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 30 with Content

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

the class InlineModelResolverTest method resolveInlineArrayRequestBody.

@Test
public void resolveInlineArrayRequestBody() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema addressSchema = new ObjectSchema();
    addressSchema.addProperties("street", new StringSchema());
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("address", addressSchema);
    ArraySchema arraySchema = new ArraySchema();
    arraySchema.items(objectSchema);
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(arraySchema))))));
    new InlineModelResolver().flatten(openAPI);
    RequestBody body = openAPI.getPaths().get("/hello").getGet().getRequestBody();
    Schema schema = body.getContent().get("*/*").getSchema();
    assertTrue(schema instanceof ArraySchema);
    ArraySchema am = (ArraySchema) schema;
    Schema inner = am.getItems();
    assertTrue(inner.get$ref() != null);
    assertEquals("#/components/schemas/hello_body", inner.get$ref());
    Schema inline = openAPI.getComponents().getSchemas().get("hello_body");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    Schema address = (Schema) inline.getProperties().get("address");
    assertNotNull(address);
    assertEquals("#/components/schemas/hello_address", address.get$ref());
    Schema inlineProp = openAPI.getComponents().getSchemas().get("hello_address");
    assertNotNull(inlineProp);
    assertTrue(inlineProp instanceof Schema);
    assertNotNull(inlineProp.getProperties().get("street"));
    assertTrue(inlineProp.getProperties().get("street") instanceof StringSchema);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)121 Test (org.testng.annotations.Test)120 MediaType (io.swagger.v3.oas.models.media.MediaType)70 Schema (io.swagger.v3.oas.models.media.Schema)70 Operation (io.swagger.v3.oas.annotations.Operation)65 Content (io.swagger.v3.oas.models.media.Content)64 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)62 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)59 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)57 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)53 Operation (io.swagger.v3.oas.models.Operation)52 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)51 StringSchema (io.swagger.v3.oas.models.media.StringSchema)50 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)42 PathItem (io.swagger.v3.oas.models.PathItem)41 ArrayList (java.util.ArrayList)41 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)40 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)37 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)37 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)33