Java Code Examples for io.swagger.v3.oas.models.responses.ApiResponse#description()

The following examples show how to use io.swagger.v3.oas.models.responses.ApiResponse#description() . 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 6 votes vote down vote up
private void processProduces() {

    if (getProduces() == null) {
      return;
    }

    List<String> produceList = Arrays.stream(produces).filter(s -> !StringUtils.isEmpty(s))
        .collect(Collectors.toList());

    if (!produceList.isEmpty()) {
      ApiResponse apiResponse = new ApiResponse();
      Content content = new Content();
      MediaType mediaType = new MediaType();
      Schema schema = ModelConverter
          .getSchema(getMethod().getReturnType(), getComponents(), RequestResponse.RESPONSE);
      mediaType.schema(schema);
      for (String produce : produceList) {
        content.addMediaType(produce, mediaType);
      }
      apiResponse.description("OK");
      apiResponse.setContent(content);
      addResponse(HttpStatuses.OK, apiResponse);
    }
  }
 
Example 2
Source File: OASErrors.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
public static Map<String, ApiResponse> generateStandardApiErrorResponses() {
  Map<String, ApiResponse> responses = new LinkedHashMap<>();

  List<Integer> responseCodes = getStandardHttpStatusCodes();
  for (Integer responseCode : responseCodes) {
    if (responseCode >= 400 && responseCode <= 599) {
      ApiResponse apiResponse = new ApiResponse();
      apiResponse.description(HttpStatus.toMessage(responseCode));
      apiResponse.content(new Content()
          .addMediaType("application/vnd.api+json",
              new MediaType().schema(new Failure().$ref()))
      );
      responses.put(responseCode.toString(), apiResponse);
    }
  }

  return responses;
}
 
Example 3
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 4
Source File: SwaggerConverter.java    From raptor with Apache License 2.0 5 votes vote down vote up
/**
 * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#responseObject
 *
 * @return
 */


protected ApiResponse getSuccessApiResponse(Rpc rpc) {
    ApiResponse apiResponse = new ApiResponse();

    apiResponse.content(getContent(rpc.responseType()));
    Type type = this.schmea.getType(rpc.responseType());
    apiResponse.description(type.documentation());
    return apiResponse;
}
 
Example 5
Source File: SchemaGenerator.java    From Poseidon with Apache License 2.0 5 votes vote down vote up
private static void resolveAPIResponse(String status, Type resolvedType, ApiResponses responses, Path modelsDir) {
    final Schema<?> responseSchema = processType(resolvedType, modelsDir);
    final ApiResponse response = new ApiResponse();
    final Content content = new Content();
    final MediaType mediaType = new MediaType();
    mediaType.schema(responseSchema);
    content.addMediaType("application/json", mediaType);
    response.content(content);
    response.description("Some random thing");
    responses.addApiResponse(status, response);
}
 
Example 6
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 7
Source File: OAS3Parser.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new operation object using the URI template object
 *
 * @param resource API resource data
 * @return a new operation object using the URI template object
 */
private Operation createOperation(SwaggerData.Resource resource) {
    Operation operation = new Operation();
    populatePathParameters(operation, resource.getPath());
    updateOperationManagedInfo(resource, operation);

    ApiResponses apiResponses = new ApiResponses();
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.description("OK");
    apiResponses.addApiResponse(APIConstants.SWAGGER_RESPONSE_200, apiResponse);
    operation.setResponses(apiResponses);
    return operation;
}
 
Example 8
Source File: OpenAPIDeserializer.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public ApiResponse getResponse(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }

    ApiResponse apiResponse = new ApiResponse();
    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                apiResponse.set$ref(mungedRef);
            }else{
                apiResponse.set$ref(ref.textValue());
            }
             return apiResponse;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }

    String value = getString("description", node, true, location, result);
    if (StringUtils.isNotBlank(value)) {
        apiResponse.description(value);
    }


    ObjectNode headerObject = getObject("headers", node, false, location, result);
    if (headerObject != null) {
        Map<String, Header> headers = getHeaders(headerObject, location, result, false);
        if (headers != null &&  headers.size() > 0) {
            apiResponse.setHeaders(headers);
        }
    }

    ObjectNode linksObj = getObject("links", node, false, location, result);
    if (linksObj != null) {
         Map<String,Link> links = getLinks(linksObj, location, result, false);
         if(links != null && links.size() > 0) {
             apiResponse.setLinks(links);
         }
    }

    ObjectNode contentObject = getObject("content", node, false, location, result);
    if (contentObject != null) {
        apiResponse.setContent(getContent(contentObject, String.format("%s.%s", location, "content"), result));
    }

    Map <String,Object> extensions = getExtensions(node);
    if(extensions != null && extensions.size() > 0) {
        apiResponse.setExtensions(extensions);
    }

    Set<String> keys = getKeys(node);
    for(String key : keys) {
        if(!RESPONSE_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }


    return apiResponse;
}
 
Example 9
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineArrayResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();

    ObjectSchema items = new ObjectSchema();
    items.addExtension("x-ext", "ext-items");
    items.addProperties("name", new StringSchema());


    ArraySchema schema = new ArraySchema()
            .items(items);
    schema.addExtension("x-ext", "ext-prop");

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

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

    new InlineModelResolver().flatten(openAPI);

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

    assertNotNull(apiResponse.getContent().get("*/*").getSchema());
    Schema responseProperty = apiResponse.getContent().get("*/*").getSchema();

    // no need to flatten more
    assertTrue(responseProperty instanceof ArraySchema);

    ArraySchema ap = (ArraySchema) responseProperty;
    assertEquals(1, ap.getExtensions().size());
    assertEquals("ext-prop", ap.getExtensions().get("x-ext"));

    Schema p = ap.getItems();

    assertNotNull(p);

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

    assertEquals(1, p.getExtensions().size());
    assertEquals("ext-items", p.getExtensions().get("x-ext"));

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

    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
 
Example 10
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineArrayResponseWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();

    ApiResponse apiResponse  = new ApiResponse();
    apiResponse.addExtension("x-foo", "bar");
    apiResponse.description("it works!");

    Map<String,Schema> properties = new HashMap<>();
    properties.put("name", new StringSchema());

    apiResponse.content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema()
                    .items(new ObjectSchema()
                            .title("FooBar")
                            .properties(properties)))));

    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");
    assertNotNull(response);

    assertNotNull(response.getContent().get("*/*").getSchema());
    Schema responseProperty = response.getContent().get("*/*").getSchema();

    // no need to flatten more
    assertTrue(responseProperty instanceof ArraySchema);

    ArraySchema ap = (ArraySchema) responseProperty;
    Schema p = ap.getItems();

    assertNotNull(p);

    assertEquals(p.get$ref(), "#/components/schemas/"+ "FooBar");


    Schema inline = openAPI.getComponents().getSchemas().get("FooBar");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}