Skip to content

Commit ed25b5d

Browse files
hospelmartintaylor1635
andauthored
BENCH-159: change to be compliant with the latest openapi version (#54)
* change to be compliant with the latest openapi version * BENCH-161 Refactored codebase * BENCH-161 Fixed README.md * change to be compliant with the latest openapi version * BENCH-161 Updated CategoryServiceTest * rebase with main * add permission * BENCH-161 Changes reflecting Andrews observations * swagger config changes * change to be compliant with the latest openapi version * rebase with main * rebase conflict * add permission * swagger config changes Co-authored-by: martintaylor1635 <[email protected]>
1 parent 734cb3b commit ed25b5d

File tree

7 files changed

+94
-27
lines changed

7 files changed

+94
-27
lines changed

.github/workflows/build_integration_and_code_analysis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ jobs:
4444
Build-Test:
4545
name: Integration Test
4646
runs-on: ubuntu-latest
47+
permissions:
48+
checks: write
49+
4750
steps:
4851
- uses: actions/checkout@v3
4952

@@ -67,7 +70,8 @@ jobs:
6770
run: mvn clean verify
6871

6972
- name: Publish test coverage results
70-
uses: PavanMudigonda/[email protected]
73+
id: jacoco_reporter
74+
uses: PavanMudigonda/[email protected]
7175
with:
7276
coverage_results_path: 'target/site/jacoco/jacoco.xml'
7377
coverage_report_title: 'Test coverage results'

pom.xml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,9 @@
6262
</dependency>
6363

6464
<dependency>
65-
<groupId>io.springfox</groupId>
66-
<artifactId>springfox-boot-starter</artifactId>
67-
<version>3.0.0</version>
68-
</dependency>
69-
70-
<dependency>
71-
<groupId>io.springfox</groupId>
72-
<artifactId>springfox-swagger-ui</artifactId>
73-
<version>3.0.0</version>
65+
<groupId>org.springdoc</groupId>
66+
<artifactId>springdoc-openapi-ui</artifactId>
67+
<version>1.6.12</version>
7468
</dependency>
7569

7670
<dependency>
@@ -84,6 +78,11 @@
8478
<artifactId>mapstruct</artifactId>
8579
<version>1.5.3.Final</version>
8680
</dependency>
81+
<dependency>
82+
<groupId>org.springdoc</groupId>
83+
<artifactId>springdoc-openapi-security</artifactId>
84+
<version>1.6.12</version>
85+
</dependency>
8786
</dependencies>
8887

8988
<build>
@@ -185,6 +184,24 @@
185184
</execution>
186185
</executions>
187186
</plugin>
187+
<plugin>
188+
<groupId>org.springdoc</groupId>
189+
<artifactId>springdoc-openapi-maven-plugin</artifactId>
190+
<version>0.2</version>
191+
<executions>
192+
<execution>
193+
<phase>integration-test</phase>
194+
<goals>
195+
<goal>generate</goal>
196+
</goals>
197+
</execution>
198+
</executions>
199+
<configuration>
200+
<apiDocsUrl>http://localhost:8080/api-docs</apiDocsUrl>
201+
<outputFileName>openapi.yml</outputFileName>
202+
<outputDir>${project.build.directory}</outputDir>
203+
</configuration>
204+
</plugin>
188205
</plugins>
189206
</build>
190207

src/main/java/com/answerdigital/academy/answerking/config/SecurityConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.context.annotation.Bean;
44
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
55
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
67
import org.springframework.security.config.http.SessionCreationPolicy;
78
import org.springframework.security.core.userdetails.User;
89
import org.springframework.security.core.userdetails.UserDetails;
@@ -50,4 +51,15 @@ public InMemoryUserDetailsManager userDetailsManager() {
5051

5152
return new InMemoryUserDetailsManager(paul, john, ringo, george);
5253
}
54+
55+
@Bean
56+
public WebSecurityCustomizer webSecurityCustomizer() {
57+
return (web) -> web
58+
.ignoring()
59+
.antMatchers("/")
60+
.antMatchers("/api/swagger/**”,”/api/swagger-ui/**”,”/api/swagger-ui.html”," +
61+
"/api/swagger-ui-custom.html", "/webjars/**", "/api/swagger-resources/**",
62+
"/api/configuration/**”, ”/api/api-docs/**");
63+
}
64+
5365
}
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
package com.answerdigital.academy.answerking.config;
22

3+
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.oas.models.info.Info;
5+
import io.swagger.v3.oas.models.info.License;
6+
import org.springdoc.core.GroupedOpenApi;
37
import org.springframework.context.annotation.Bean;
48
import org.springframework.context.annotation.Configuration;
5-
import springfox.documentation.builders.PathSelectors;
6-
import springfox.documentation.builders.RequestHandlerSelectors;
7-
import springfox.documentation.spi.DocumentationType;
8-
import springfox.documentation.spring.web.plugins.Docket;
99

1010
@Configuration
1111
public class SwaggerConfig {
12+
13+
@Bean
14+
public GroupedOpenApi publicApi() {
15+
return GroupedOpenApi.builder()
16+
.group("public-apis")
17+
.pathsToMatch("/**")
18+
.pathsToExclude("/admins/**")
19+
.build();
20+
}
21+
1222
@Bean
13-
public Docket swagger() {
14-
return new Docket(DocumentationType.SWAGGER_2)
15-
.select()
16-
.apis(RequestHandlerSelectors.any())
17-
.paths(PathSelectors.any())
23+
public GroupedOpenApi actuatorApi() {
24+
return GroupedOpenApi.builder()
25+
.group("admins")
26+
.pathsToMatch("/admins/**")
1827
.build();
1928
}
29+
30+
@Bean
31+
public OpenAPI springShopOpenAPI() {
32+
return new OpenAPI()
33+
.info(new Info().title("AnswerKing API")
34+
.description("Answer King application")
35+
.version("v0.0.1")
36+
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
37+
}
38+
2039
}

src/main/java/com/answerdigital/academy/answerking/controller/CategoryController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import com.answerdigital.academy.answerking.request.AddCategoryRequest;
55
import com.answerdigital.academy.answerking.request.UpdateCategoryRequest;
66
import com.answerdigital.academy.answerking.service.CategoryService;
7+
import io.swagger.v3.oas.annotations.Operation;
8+
import io.swagger.v3.oas.annotations.media.Content;
9+
import io.swagger.v3.oas.annotations.media.Schema;
10+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
11+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
712
import org.springframework.beans.factory.annotation.Autowired;
813
import org.springframework.http.HttpStatus;
914
import org.springframework.http.ResponseEntity;
@@ -41,6 +46,11 @@ public ResponseEntity<Category> addCategory(@Valid @RequestBody final AddCategor
4146
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
4247
}
4348

49+
@Operation(summary = "Get all categories.")
50+
@ApiResponses(value = {
51+
@ApiResponse(responseCode = "200", description = "Found the the list of categories",
52+
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Category.class)) })
53+
})
4454
@GetMapping
4555
public ResponseEntity<Collection<Category>> getAllCategories() {
4656
final Set<Category> categories = categoryService.findAll();

src/main/java/com/answerdigital/academy/answerking/exception/util/RestResponseEntityExceptionHandler.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class RestResponseEntityExceptionHandler {
2121
@ExceptionHandler(MethodArgumentNotValidException.class)
2222
public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(
2323
final MethodArgumentNotValidException exception,
24-
final HttpServletRequest request
25-
) {
24+
final HttpServletRequest request) {
25+
2626
String detail;
2727
try {
2828
detail = Objects.requireNonNull(exception.getFieldError()).getDefaultMessage();
@@ -37,8 +37,8 @@ public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(
3737
@ExceptionHandler(ConstraintViolationException.class)
3838
public ResponseEntity<ErrorResponse> handleConstraintViolationException(
3939
final ConstraintViolationException exception,
40-
final HttpServletRequest request
41-
) {
40+
final HttpServletRequest request ) {
41+
4242
final List<String> errorMessages = exception.getConstraintViolations().stream()
4343
.map(ConstraintViolation::getMessage)
4444
.toList();
@@ -50,8 +50,8 @@ public ResponseEntity<ErrorResponse> handleConstraintViolationException(
5050
@ExceptionHandler(AnswerKingException.class)
5151
public ResponseEntity<ErrorResponse> handleAnswerKingException(
5252
final AnswerKingException exception,
53-
final HttpServletRequest request
54-
) {
53+
final HttpServletRequest request) {
54+
5555
final ErrorResponse response = new ErrorResponse(exception, request);
5656
return new ResponseEntity<>(response, exception.getStatus());
5757
}
@@ -60,9 +60,10 @@ public ResponseEntity<ErrorResponse> handleAnswerKingException(
6060
@ExceptionHandler(value = {Exception.class, RuntimeException.class})
6161
public ResponseEntity<ErrorResponse> defaultExceptionHandler(
6262
final Exception exception,
63-
final HttpServletRequest request
64-
) {
63+
final HttpServletRequest request) {
64+
6565
final ErrorResponse response = new ErrorResponse(new InternalServerErrorException(exception.getMessage()), request);
6666
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
6767
}
68+
6869
}

src/main/resources/application.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
server:
2+
servlet:
3+
context-path: /api
4+
15
spring:
26
profiles:
37
active: dev

0 commit comments

Comments
 (0)