This repository has been archived by the owner on May 3, 2023. It is now read-only.
generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(swagger): set up annotation-based documentation (#63)
- Loading branch information
1 parent
03bddc8
commit dd4ad6a
Showing
12 changed files
with
197 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/ca/bc/gov/backendstartapi/config/SwaggerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ca.bc.gov.backendstartapi.config; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.ExternalDocumentation; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.info.Info; | ||
import io.swagger.v3.oas.models.info.License; | ||
import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
import io.swagger.v3.oas.models.security.SecurityScheme; | ||
import io.swagger.v3.oas.models.security.SecurityScheme.Type; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* General information to be displayed in the documentation of our API, following the <a | ||
* href="https://spec.openapis.org/oas/latest.html">OpenAPI specification</a>. | ||
* | ||
* <p>The generated documentation is to be rendered by <a href="https://swagger.io/">Swagger</a>. | ||
*/ | ||
@Configuration | ||
public class SwaggerConfig { | ||
|
||
private static final String BEARER_SECURITY_SCHEME_NAME = "bearerAuth"; | ||
|
||
/** General information about our API. */ | ||
@Bean | ||
public OpenAPI theRestApi() { | ||
return new OpenAPI() | ||
.info( | ||
new Info() | ||
.title("THE database REST API") | ||
.description("A REST API to fetch information from the THE database.") | ||
.version("v0.0.1") | ||
.termsOfService( | ||
"https://www2.gov.bc.ca/gov/content/data/open-data/api-terms-of-use-for-ogl-information") | ||
.license( | ||
new License() | ||
.name("OGL-BC") | ||
.url( | ||
"https://www2.gov.bc.ca/gov/content/data/open-data/open-government-licence-bc"))) | ||
.externalDocs( | ||
new ExternalDocumentation() | ||
.description("Our Jira project") | ||
.url("https://apps.nrs.gov.bc.ca/int/jira/projects/FSADT2")) | ||
.components( | ||
new Components() | ||
.addSecuritySchemes( | ||
BEARER_SECURITY_SCHEME_NAME, | ||
new SecurityScheme() | ||
.name(BEARER_SECURITY_SCHEME_NAME) | ||
.type(Type.HTTP) | ||
.scheme("bearer") | ||
.bearerFormat("JWT"))) | ||
.addSecurityItem(new SecurityRequirement().addList(BEARER_SECURITY_SCHEME_NAME)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/ca/bc/gov/backendstartapi/endpoint/parameters/PaginatedViaQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ca.bc.gov.backendstartapi.endpoint.parameters; | ||
|
||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Adds the following pagination parameters to the Swagger documentation of a method: | ||
* | ||
* <ul> | ||
* <li>page: Zero-based page index indicating the page to be returned; defaults to 0 | ||
* <li>perPage: The maximum number of results in a page; defaults to 20 | ||
* </ul> | ||
* | ||
* <p>The code for such parameters, along with their behaviour, must be implemented and documented | ||
* by the developer. See {@link PaginationParameters} and its uses, for instance. | ||
* | ||
* <p>Concept taken from {@link org.springdoc.core.converters.models.PageableAsQueryParam}. | ||
*/ | ||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Parameter( | ||
in = ParameterIn.QUERY, | ||
description = "Zero-based page index indicating the page to be returned.", | ||
name = "page", | ||
schema = @Schema(type = "integer", defaultValue = "0", minimum = "0")) | ||
@Parameter( | ||
in = ParameterIn.QUERY, | ||
description = "The maximum number of results in a page.", | ||
name = "perPage", | ||
schema = @Schema(type = "integer", defaultValue = "20", minimum = "1")) | ||
public @interface PaginatedViaQuery {} |
20 changes: 9 additions & 11 deletions
20
src/main/java/ca/bc/gov/backendstartapi/endpoint/parameters/PaginationParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
package ca.bc.gov.backendstartapi.endpoint.parameters; | ||
|
||
import io.swagger.v3.oas.annotations.Hidden; | ||
import jakarta.validation.constraints.Positive; | ||
import jakarta.validation.constraints.PositiveOrZero; | ||
|
||
/** | ||
* Pagination parameters to be used in the processing of HTTP GET requests. | ||
* | ||
* <p>Each page contains up to {@code size} results, excluding the first {@code page * size} | ||
* results. For instance, <code>{page = 2, size = 10}</code> will exclude the first 20 results, | ||
* returning results 21 up to 30. | ||
* | ||
* @param page The page to be returned. Zero-based, and must be non-negative; defaults to 0 | ||
* @param size The maximum number of results to be returned. Defaults to 20 | ||
* @param perPage The maximum number of results in each page. Defaults to 20 | ||
*/ | ||
public record PaginationParameters(@PositiveOrZero Integer page, @Positive Integer size) { | ||
@Hidden | ||
public record PaginationParameters(@PositiveOrZero Integer page, @Positive Integer perPage) { | ||
|
||
/** | ||
* Build an instance of {@link PaginationParameters}, using the default values for {@code page} | ||
* and {@code size} if they're null. | ||
* and {@code perPage} if they're null. | ||
*/ | ||
public PaginationParameters { | ||
if (page == null) { | ||
page = 0; | ||
} | ||
if (size == null) { | ||
size = 20; | ||
if (perPage == null) { | ||
perPage = 20; | ||
} | ||
} | ||
|
||
/** Get the number of results to be skipped by the search. */ | ||
/** Get the number of results to be skipped by the search. Useful for SQL queries. */ | ||
public int skip() { | ||
return page * size; | ||
return page * perPage; | ||
} | ||
} |
Oops, something went wrong.