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

The following examples show how to use io.swagger.v3.oas.models.responses.ApiResponse#setDescription() . 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: OperationBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Sets description.
 *
 * @param response the response
 * @param apiResponseObject the api response object
 */
private void setDescription(io.swagger.v3.oas.annotations.responses.ApiResponse response,
		ApiResponse apiResponseObject) {
	if (StringUtils.isNotBlank(response.description())) {
		apiResponseObject.setDescription(response.description());
	}
	else {
		GenericResponseBuilder.setDescription(response.responseCode(), apiResponseObject);
	}
}
 
Example 2
Source File: GenericResponseBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Compute response from doc map.
 *
 * @param components the components
 * @param methodParameter the method parameter
 * @param apiResponsesOp the api responses op
 * @param methodAttributes the method attributes
 * @return the map
 */
private Map<String, ApiResponse> computeResponseFromDoc(Components components, MethodParameter methodParameter, ApiResponses apiResponsesOp,
		MethodAttributes methodAttributes) {
	// Parsing documentation, if present
	Set<io.swagger.v3.oas.annotations.responses.ApiResponse> responsesArray = getApiResponses(methodParameter.getMethod());
	if (!responsesArray.isEmpty()) {
		methodAttributes.setWithApiResponseDoc(true);
		for (io.swagger.v3.oas.annotations.responses.ApiResponse apiResponseAnnotations : responsesArray) {
			String httpCode = apiResponseAnnotations.responseCode();
			ApiResponse apiResponse = new ApiResponse();
			if (StringUtils.isNotBlank(apiResponseAnnotations.ref())) {
				apiResponse.$ref(apiResponseAnnotations.ref());
				apiResponsesOp.addApiResponse(apiResponseAnnotations.responseCode(), apiResponse);
				continue;
			}
			apiResponse.setDescription(propertyResolverUtils.resolve(apiResponseAnnotations.description()));
			buildContentFromDoc(components, apiResponsesOp, methodAttributes, apiResponseAnnotations, apiResponse);
			Map<String, Object> extensions = AnnotationsUtils.getExtensions(apiResponseAnnotations.extensions());
			if (!CollectionUtils.isEmpty(extensions))
				apiResponse.extensions(extensions);
			AnnotationsUtils.getHeaders(apiResponseAnnotations.headers(), methodAttributes.getJsonViewAnnotation())
					.ifPresent(apiResponse::headers);
			apiResponsesOp.addApiResponse(httpCode, apiResponse);
		}
	}
	return apiResponsesOp;
}
 
Example 3
Source File: GenericResponseBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Sets description.
 *
 * @param httpCode the http code
 * @param apiResponse the api response
 */
public static void setDescription(String httpCode, ApiResponse apiResponse) {
	try {
		HttpStatus httpStatus = HttpStatus.valueOf(Integer.parseInt(httpCode));
		apiResponse.setDescription(httpStatus.getReasonPhrase());
	}
	catch (IllegalArgumentException e) {
		apiResponse.setDescription(DEFAULT_DESCRIPTION);
	}
}
 
Example 4
Source File: ProtoOpenAPI.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
/**
 * Add openAPI path item to the the openAPI object.
 *
 * @param path            name of the pathItem
 * @param scopes          array of operation scopes
 * @param throttlingTier throttling tier
 */
void addOpenAPIPath(String path, String[] scopes, String throttlingTier) {
    PathItem pathItem = new PathItem();
    Operation operation = new Operation();
    operation.setOperationId(UUID.randomUUID().toString());
    addOauth2SecurityRequirement(operation, scopes);
    if (StringUtils.isNotEmpty(throttlingTier)) {
        operation.addExtension(OpenAPIConstants.THROTTLING_TIER, throttlingTier);
    }
    //needs to add the basic Auth Requirement to the operation level because if scopes are mentioned,
    // there would be oauth2 security requirement in method level.
    if (isBasicAuthEnabled) {
        addBasicAuthSecurityRequirement(operation);
    }
    if (isAPIKeyEnabled) {
        addAPIKeySecurityRequirement(operation);
    }
    //For each path, the only available http method is "post" according to the grpc mapping.
    pathItem.setPost(operation);
    if (openAPI.getPaths() == null) {
        openAPI.setPaths(new Paths());
    }
    //as Responses object is mandatory
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.setDescription(ProtoToOpenAPIConstants.RESPONSE_DESCRIPTION);
    ApiResponses apiResponses = new ApiResponses();
    apiResponses.addApiResponse(ProtoToOpenAPIConstants.SUCCESS_RESPONSE_CODE, apiResponse);
    operation.setResponses(apiResponses);
    //append forward slash to preserve openAPI syntax
    openAPI.getPaths().addPathItem(ProtoToOpenAPIConstants.PATH_SEPARATOR + path, pathItem);
}
 
Example 5
Source File: ApiResponseTransformerTest.java    From swagger-brake with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransformShouldNotFailWhenFromContentIsNull() {
    // given
    String code = "401";
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.setDescription("Unauthorized");
    // when
    Response result = underTest.transform(new ImmutablePair<>(code, apiResponse));
    // then
    assertThat(result).isNotNull();
    assertThat(result.getCode()).isEqualTo(code);
}
 
Example 6
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayResponse() {
    OpenAPI openAPI = new OpenAPI();


    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("name", new StringSchema());
    ArraySchema schema = new ArraySchema();
    schema.setItems(objectSchema);

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

    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");
    assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema);

    ArraySchema am = (ArraySchema) response.getContent().get("*/*").getSchema();
    Schema items = am.getItems();
    assertTrue(items.get$ref() != null);

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


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

    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
 
Example 7
Source File: OpenApiCustomizer.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Allows to customize the responses of the given {@link Operation} instance; the method is invoked
 * for all instances available.
 *
 * @param operation operation instance
 * @param ori CXF data about the given operation instance
 */
protected void customizeResponses(final Operation operation, final OperationResourceInfo ori) {
    if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
        ApiResponse response = operation.getResponses().entrySet().iterator().next().getValue();
        if (StringUtils.isBlank(response.getDescription())
                || (StringUtils.isNotBlank(javadocProvider.getMethodResponseDoc(ori))
                && Reader.DEFAULT_DESCRIPTION.equals(response.getDescription()))) {

            response.setDescription(javadocProvider.getMethodResponseDoc(ori));
        }
    }
}
 
Example 8
Source File: SwaggerConverter.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public ApiResponse convert(io.swagger.models.Response v2Response, List<String> produces) {
    ApiResponse response = new ApiResponse();
    Content content = new Content();

    if (v2Response instanceof RefResponse) {

        RefResponse ref = (RefResponse) v2Response;
        if (ref.get$ref().indexOf("#/responses") == 0) {
            String updatedRef = "#/components/responses" + ref.get$ref().substring("#/responses".length());
            ref.set$ref(updatedRef);
        }

        response.set$ref(ref.get$ref());
    } else {

        List<String> mediaTypes = new ArrayList<>(globalProduces);
        if (produces != null) {
            // use this for media type
            mediaTypes.clear();
            mediaTypes.addAll(produces);
        }

        if (mediaTypes.size() == 0) {
            mediaTypes.add("*/*");
        }

        response.setDescription(v2Response.getDescription());

        if (v2Response.getSchema() != null) {
            Schema schema = convertFileSchema(convert(v2Response.getSchema()));
            for (String type : mediaTypes) {
                // TODO: examples
                MediaType mediaType = new MediaType();
                content.addMediaType(type, mediaType.schema(schema));
            }
            response.content(content);
        }

        response.content(convertExamples(v2Response.getExamples(), content));
        response.setExtensions(convert(v2Response.getVendorExtensions()));

        if (v2Response.getHeaders() != null && v2Response.getHeaders().size() > 0) {
            response.setHeaders(convertHeaders(v2Response.getHeaders()));
        }
    }

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


    StringSchema stringSchema1 = new StringSchema();

    ObjectSchema objectSchema1 = new ObjectSchema();
    objectSchema1.addProperties("name", stringSchema1);
    objectSchema1.addExtension("x-ext", "ext-prop");

    MediaType mediaType1 = new MediaType();
    mediaType1.setSchema(objectSchema1);

    Content content1 = new Content();
    content1.addMediaType("*/*", mediaType1 );

    ApiResponse response1= new ApiResponse();
    response1.setDescription("it works!");
    response1.setContent(content1);

    ApiResponses responses1 = new ApiResponses();
    responses1.addApiResponse("200",response1);

    Operation operation1 = new Operation();
    operation1.setResponses(responses1);

    PathItem pathItem1 = new PathItem();
    pathItem1.setGet(operation1);
    openAPI.path("/foo/bar",pathItem1);



    StringSchema stringSchema2 = new StringSchema();

    ObjectSchema objectSchema2 = new ObjectSchema();
    objectSchema2.addProperties("name", stringSchema2);
    objectSchema2.addExtension("x-ext", "ext-prop");
    MediaType mediaType2 = new MediaType();
    mediaType2.setSchema(objectSchema2);

    Content content2 = new Content();
    content2.addMediaType("*/*", mediaType2 );

    ApiResponse response2 = new ApiResponse();
    response2.setDescription("it works!");
    response2.addExtension("x-foo","bar");
    response2.setContent(content2);

    ApiResponses responses2 = new ApiResponses();
    responses2.addApiResponse("200",response2);

    Operation operation2 = new Operation();
    operation2.setResponses(responses2);

    PathItem pathItem2 = new PathItem();
    pathItem2.setGet(operation2);
    openAPI.path("/foo/baz",pathItem2);


    new InlineModelResolver().flatten(openAPI);

    Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses();

    ApiResponse response = responses.get("200");
    assertNotNull(response);

    Schema schema = response.getContent().get("*/*").getSchema();
    assertTrue(schema.get$ref() != null);
    assertEquals(1, schema.getExtensions().size());
    assertEquals("ext-prop", schema.getExtensions().get("x-ext"));

    Schema model = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertTrue(model.getProperties().size() == 1);
    assertNotNull(model.getProperties().get("name"));
    assertTrue(model.getProperties().get("name") instanceof StringSchema);
}
 
Example 10
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 2 votes vote down vote up
@Test
public void testInlineResponseModelWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();

    String responseTitle = "GetBarResponse";

    StringSchema stringSchema1 = new StringSchema();

    ObjectSchema objectSchema1 = new ObjectSchema();
    objectSchema1.setTitle(responseTitle);
    objectSchema1.addProperties("name", stringSchema1);


    MediaType mediaType1 = new MediaType();
    mediaType1.setSchema(objectSchema1);

    Content content1 = new Content();
    content1.addMediaType("*/*", mediaType1 );

    ApiResponse response1= new ApiResponse();
    response1.setDescription("it works!");
    response1.setContent(content1);

    ApiResponses responses1 = new ApiResponses();
    responses1.addApiResponse("200",response1);

    Operation operation1 = new Operation();
    operation1.setResponses(responses1);

    PathItem pathItem1 = new PathItem();
    pathItem1.setGet(operation1);
    openAPI.path("/foo/bar",pathItem1);



    StringSchema stringSchema2 = new StringSchema();

    ObjectSchema objectSchema2 = new ObjectSchema();
    objectSchema2.addProperties("name", stringSchema2);
    objectSchema2.addExtension("x-foo", "bar");

    MediaType mediaType2 = new MediaType();
    mediaType2.setSchema(objectSchema2);

    Content content2 = new Content();
    content2.addMediaType("*/*", mediaType2 );

    ApiResponse response2 = new ApiResponse();
    response2.setDescription("it works!");

    response2.setContent(content2);

    ApiResponses responses2 = new ApiResponses();
    responses2.addApiResponse("200",response2);

    Operation operation2 = new Operation();
    operation2.setResponses(responses2);

    PathItem pathItem2 = new PathItem();
    pathItem2.setGet(operation2);
    openAPI.path("/foo/baz",pathItem2);



    new InlineModelResolver().flatten(openAPI);

    Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses();

    ApiResponse response = responses.get("200");
    assertNotNull(response);
    assertTrue(response.getContent().get("*/*").getSchema().get$ref() != null );

    Schema model = openAPI.getComponents().getSchemas().get(responseTitle);
    assertTrue(model.getProperties().size() == 1);
    assertNotNull(model.getProperties().get("name"));
    assertTrue(model.getProperties().get("name") instanceof StringSchema);
}