Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

springdoc-openapi different examples

Tags:

java

springdoc

I use springdoc-openapi to document my REST API. An error is returned by an error object, that has an errorCode and message. I use @Schema annotation to document an example. However I need different examples per different errors. Is there any way, how to do that?

Example from my code:

    @PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    @Operation(summary = "Get new license or retrieve previously issued one for this userId.", tags = "License Endpoint", description = "Licensing operations.",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "New license or previously issued license for this user, if request was called multiple times.",
                            content = {@Content(schema = @Schema(implementation = LicenseResponse.class))}
                    ),
                    @ApiResponse(responseCode = "400",
                            description = "License can not be retrieved because of either expired bundle or requested bundleId does not exist.",
                            //I need different example for this error
                            content = {@Content(schema = @Schema(implementation = LicenseErrorResponse.class))
                            }
                    ),
                    @ApiResponse(responseCode = "500",
                            description = "Internal Error",
                            //And different example for this error
                            content = {@Content(schema = @Schema(implementation = LicenseErrorResponse.class))
                            }
                    )
            }
    )
    @LoggedIO(input = INFO, result = INFO)
    public ResponseEntity<Object> newLicense(@Valid @RequestBody LicenseRequest licenseRequest) {
//content not interesting
}

import javax.validation.constraints.NotBlank;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class LicenseErrorResponse {

    // I need different examples for different error in controller.
    @Schema(example = "UNKNOWN_BUNDLE_ID", required = true)
    private final LicenseErrorCode licenseErrorCode;

    @Schema(example = "Bundle doesn't exist, bundleId=com.unknown.id")
    private final String message;

    @JsonCreator
    public LicenseErrorResponse(
                @NotBlank @JsonProperty(value = "errorCode") final LicenseErrorCode licenseErrorCode,
                @NotBlank @JsonProperty(value = "message") final String message) {
        this.licenseErrorCode = licenseErrorCode;
        this.message = message;
    }

    public enum LicenseErrorCode {
        EXPIRED_BUNDLE, UNKNOWN_BUNDLE_ID, OTHER
    }

}
like image 472
Michal Krasny Avatar asked Jun 03 '26 09:06

Michal Krasny


1 Answers

One way to do that is you can define a string as an example

public static final String exampleInternalError = "{\r\n"
            + "  \"licenseErrorCode\": 500,\r\n"
            + "  \"message\": \"Internal Error\"\r\n" + "}";

same is used to show the example as

@ApiResponse(responseCode = "500",
                            description = "Internal Error",
                            //And different example for this error
                            content = @Content(schema = @Schema(implementation = LicenseErrorResponse.class), 
                                   examples = @ExampleObject(description = "Internal Error", value = exampleInternalError)))
like image 164
SSK Avatar answered Jun 06 '26 00:06

SSK