Java Code Examples for io.swagger.v3.oas.models.responses.ApiResponses#get()

The following examples show how to use io.swagger.v3.oas.models.responses.ApiResponses#get() . 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: OperationContext.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
public void correctResponse(ApiResponses apiResponses) {

    if (apiResponses == null) {
      return;
    }

    // no annotations are processed
    // generate a default response based on the method return value
    if (apiResponses.get(HttpStatuses.OK) == null) {
      ApiResponse apiResponse = new ApiResponse();

      Class<?> returnType = method.getReturnType();
      if (returnType == Void.TYPE || returnType == Void.class) {
        return;
      }

      MediaType mediaType = new MediaType();

      Schema refSchema = ModelConverter.getSchema(returnType, getComponents(), RequestResponse.RESPONSE);
      mediaType.schema(refSchema);

      Content content = new Content();
      content.addMediaType(MediaTypes.APPLICATION_JSON, mediaType);
      apiResponse.description("OK");
      apiResponse.setContent(content);
      apiResponses.addApiResponse(HttpStatuses.OK, apiResponse);
    }
  }
 
Example 2
Source File: AbstractEndpointGenerationTest.java    From flow with Apache License 2.0 4 votes vote down vote up
private void assertPath(Class<?> testEndpointClass,
        Method expectedEndpointMethod, PathItem actualPath) {
    Operation actualOperation = actualPath.getPost();
    assertEquals("Unexpected tag in the OpenAPI spec",
            actualOperation.getTags(),
            Collections.singletonList(testEndpointClass.getSimpleName()));
    assertTrue(String.format(
            "Unexpected OpenAPI operation id: does not contain the endpoint name of the class '%s'",
            testEndpointClass.getSimpleName()),
            actualOperation.getOperationId()
                    .contains(getEndpointName(testEndpointClass)));
    assertTrue(String.format(
            "Unexpected OpenAPI operation id: does not contain the name of the endpoint method '%s'",
            expectedEndpointMethod.getName()),
            actualOperation.getOperationId()
                    .contains(expectedEndpointMethod.getName()));

    if (expectedEndpointMethod.getParameterCount() > 0) {
        Schema requestSchema = extractSchema(
                actualOperation.getRequestBody().getContent());
        assertRequestSchema(requestSchema,
                expectedEndpointMethod.getParameterTypes(),
                expectedEndpointMethod.getParameters());
    } else {
        assertNull(String.format(
                "No request body should be present in path schema for endpoint method with no parameters, method: '%s'",
                expectedEndpointMethod), actualOperation.getRequestBody());
    }

    ApiResponses responses = actualOperation.getResponses();
    assertEquals(
            "Every operation is expected to have a single '200' response",
            1, responses.size());
    ApiResponse apiResponse = responses.get("200");
    assertNotNull(
            "Every operation is expected to have a single '200' response",
            apiResponse);

    if (expectedEndpointMethod.getReturnType() != void.class) {
        assertSchema(extractSchema(apiResponse.getContent()),
                expectedEndpointMethod.getReturnType());
    } else {
        assertNull(String.format(
                "No response is expected to be present for void method '%s'",
                expectedEndpointMethod), apiResponse.getContent());
    }

    assertNotNull(
            "Non-anonymous endpoint method should have a security data defined for it in the schema",
            actualOperation.getSecurity());
}
 
Example 3
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "data")
public void readProducesTestEndpoint(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);

    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 18);

    //parameters operation get
    PathItem producesTestEndpoint = paths.get("/producesTest");
    Assert.assertNotNull(producesTestEndpoint.getGet());
    Assert.assertNotNull(producesTestEndpoint.getGet().getParameters());
    Assert.assertTrue(producesTestEndpoint.getGet().getParameters().isEmpty());

    Operation operation = producesTestEndpoint.getGet();
    ApiResponses responses = operation.getResponses();
    Assert.assertNotNull(responses);
    Assert.assertFalse(responses.isEmpty());

    ApiResponse response = responses.get("200");
    Assert.assertNotNull(response);
    Assert.assertEquals("it works", response.getDescription());

    Content content = response.getContent();
    Assert.assertNotNull(content);
    MediaType mediaType = content.get("application/json");
    Assert.assertNotNull(mediaType);

    Schema schema = mediaType.getSchema();
    Assert.assertNotNull(schema);
    Assert.assertTrue(schema instanceof ObjectSchema);

    ObjectSchema objectSchema = (ObjectSchema) schema;
    schema = objectSchema.getProperties().get("name");
    Assert.assertNotNull(schema);

    Assert.assertTrue(schema instanceof StringSchema);
}
 
Example 4
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "data")
public void readPathsObject(JsonNode rootNode) throws Exception {

    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    //System.out.println(openAPI);

    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 18);


    PathItem petRef = paths.get("/pathItemRef");

    PathItem petEndpoint = paths.get("/pet");
    Assert.assertNotNull(petEndpoint);
    Assert.assertEquals(petEndpoint.getSummary(),"summary");
    Assert.assertEquals(petEndpoint.getDescription(),"description");
    Assert.assertNotNull(petEndpoint.getPost().getExternalDocs());
    Assert.assertEquals(petEndpoint.getPost().getExternalDocs().getUrl(),"http://swagger.io");
    Assert.assertEquals(petEndpoint.getPost().getExternalDocs().getDescription(),"Find out more");

    //Operation trace
    Assert.assertNotNull(petEndpoint.getTrace());
    Assert.assertNotNull(petEndpoint.getDescription());

    //Operation post
    Assert.assertNotNull(petEndpoint.getPost());
    Assert.assertNotNull(petEndpoint.getPost().getTags());
    Assert.assertEquals(petEndpoint.getPost().getTags().size(), 1);
    Assert.assertEquals(petEndpoint.getPost().getSummary(), "Add a new pet to the store");
    Assert.assertNull(petEndpoint.getPost().getDescription());
    Assert.assertEquals(petEndpoint.getPost().getOperationId(), "addPet");
    Assert.assertNotNull(petEndpoint.getServers());
    Assert.assertEquals(petEndpoint.getServers().size(), 1);
    Assert.assertNotNull(petEndpoint.getParameters());
    Assert.assertEquals(petEndpoint.getParameters().size(), 2);
    Assert.assertNotNull(petEndpoint.getPost().getParameters());
    Assert.assertEquals(petEndpoint.getPost().getSecurity().get(0).get("petstore_auth").get(0), "write:pets");
    Assert.assertEquals(petEndpoint.getPost().getSecurity().get(0).get("petstore_auth").get(1), "read:pets");

    ApiResponses responses = petEndpoint.getPost().getResponses();
    Assert.assertNotNull(responses);
    assertTrue(responses.containsKey("405"));
    ApiResponse response = responses.get("405");
    Assert.assertEquals(response.getDescription(), "Invalid input");
    Assert.assertEquals(response.getHeaders().get("X-Rate-Limit").getDescription(), "calls per hour allowed by the user");


    //parameters operation get

    PathItem petByStatusEndpoint = paths.get("/pet/findByStatus");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getTags());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getIn(),"query");
    Assert.assertEquals(petByStatusEndpoint.getGet().getCallbacks().get("mainHook").get("$request.body#/url").getPost().getResponses().get("200").getDescription(),"webhook successfully processed operation");

}
 
Example 5
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void readOAS(/*JsonNode rootNode*/) throws Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    final JsonNode rootNode = mapper.readTree(Files.readAllBytes(java.nio.file.Paths.get(getClass().getResource("/oas4.yaml").toURI())));
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);

    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);

    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 114);



    PathItem stripe = paths.get("/v1/3d_secure");

    Assert.assertNotNull(stripe);


    Assert.assertNotNull(stripe.getPost());
    Assert.assertNull(stripe.getPost().getDescription());
    Assert.assertEquals(stripe.getPost().getOperationId(), "Create3DSecure");
    Assert.assertNotNull(stripe.getPost().getParameters());

    ApiResponses responses = stripe.getPost().getResponses();
    Assert.assertNotNull(responses);
    assertTrue(responses.containsKey("200"));
    ApiResponse response = responses.get("200");
    Assert.assertEquals(response.getDescription(), "Successful response.");
    Assert.assertEquals(response.getContent().get("application/json").getSchema().get$ref(),"#/components/schemas/three_d_secure");



    PathItem stripeGet = paths.get("/v1/account/external_accounts");

    Assert.assertNotNull(stripeGet);


    Assert.assertNotNull(stripeGet.getGet());
    Assert.assertNull(stripeGet.getGet().getDescription());
    Assert.assertEquals(stripeGet.getGet().getOperationId(), "AllAccountExternalAccounts");
    Assert.assertNotNull(stripeGet.getGet().getParameters());

    ApiResponses responsesGet = stripeGet.getGet().getResponses();
    Assert.assertNotNull(responsesGet);
    assertTrue(responsesGet.containsKey("200"));
    ApiResponse responseGet = responsesGet.get("200");
    Assert.assertEquals(responseGet.getDescription(), "Successful response.");
    Map<String, Schema> properties = (Map<String, Schema>) responseGet.getContent().get("application/json").getSchema().getProperties();

    Assert.assertNotNull(properties);
    Assert.assertNull(properties.get("data").getType());
    Assert.assertEquals(properties.get("has_more").getDescription(), "True if this list has another page of items after this one that can be fetched.");
    assertTrue(properties.get("data") instanceof ComposedSchema );


    ComposedSchema data =  (ComposedSchema) properties.get("data");
    assertTrue(data.getOneOf().get(0) instanceof ArraySchema );
    ArraySchema items = (ArraySchema)data.getOneOf().get(0);
    Assert.assertEquals(items.getItems().get$ref(),"#/components/schemas/bank_account");

}