Skip to content

Commit 360f1ec

Browse files
committed
Add swagger docs
1 parent c006288 commit 360f1ec

4 files changed

Lines changed: 75 additions & 5 deletions

File tree

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>org.springframework.boot</groupId>
1313
<artifactId>spring-boot-starter-parent</artifactId>
14-
<version>3.3.3</version>
14+
<version>3.5.0</version>
1515
<relativePath/>
1616
</parent>
1717

@@ -31,6 +31,11 @@
3131
<version>5.11.3</version>
3232
<scope>test</scope>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.springdoc</groupId>
36+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
37+
<version>2.8.8</version>
38+
</dependency>
3439
</dependencies>
3540

3641
<build>
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
package com.programmersdiary;
22

3-
public record CenterTakingRequest(Tile tileToTake, int tilesToPutOnFloor, int patternLineIndex) {
4-
}
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
@Schema(description = "Request to take tiles from the center area")
6+
public record CenterTakingRequest(
7+
@Schema(description = "Tile color to take from center", example = "BLUE",
8+
allowableValues = {"RED", "BLUE", "YELLOW", "BLACK", "WHITE"})
9+
Tile tileToTake,
10+
11+
@Schema(description = "Number of tiles to put on floor", example = "1", minimum = "0")
12+
int tilesToPutOnFloor,
13+
14+
@Schema(description = "Pattern line index (0-4) where remaining tiles go", example = "2", minimum = "0", maximum = "4")
15+
int patternLineIndex
16+
) {}
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
package com.programmersdiary;
22

3-
public record FactoryTakingRequest(int factoryIndex, Tile tileToTake, int tilesToPutOnFloor, int patternLineIndex) {
4-
}
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
@Schema(description = "Request to take tiles from a factory display")
6+
public record FactoryTakingRequest(
7+
@Schema(description = "Factory display index (0-4)", example = "0", minimum = "0", maximum = "4")
8+
int factoryIndex,
9+
10+
@Schema(description = "Tile color to take", example = "RED",
11+
allowableValues = {"RED", "BLUE", "YELLOW", "BLACK", "WHITE"})
12+
Tile tileToTake,
13+
14+
@Schema(description = "Number of tiles to put on floor", example = "0", minimum = "0")
15+
int tilesToPutOnFloor,
16+
17+
@Schema(description = "Pattern line index (0-4) where remaining tiles go", example = "1", minimum = "0", maximum = "4")
18+
int patternLineIndex
19+
) {}

src/main/java/com/programmersdiary/GameController.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.programmersdiary;
22

3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.media.Content;
5+
import io.swagger.v3.oas.annotations.media.ExampleObject;
6+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
7+
import io.swagger.v3.oas.annotations.tags.Tag;
38
import org.springframework.web.bind.annotation.GetMapping;
49
import org.springframework.web.bind.annotation.PostMapping;
510
import org.springframework.web.bind.annotation.RequestBody;
@@ -8,6 +13,7 @@
813
import java.util.Map;
914

1015
@RestController
16+
@Tag(name = "Azul Game", description = "An API for Azul board game")
1117
public class GameController {
1218
private final Game game;
1319

@@ -16,16 +22,34 @@ public GameController(Game game) {
1622
}
1723

1824
@GetMapping("/show")
25+
@Operation(summary = "Get game state as a plain-text", description = "Returns the current game state as a plain-text")
26+
@ApiResponse(responseCode = "200", description = "Current game state")
1927
public String show() {
2028
return game.toString();
2129
}
2230

2331
@GetMapping("/showJson")
32+
@Operation(summary = "Get game state as JSON", description = "Returns the current game state as a JSON object")
33+
@ApiResponse(responseCode = "200", description = "Current game state in JSON format")
2434
public Map<String, Object> showJson() {
2535
return game.jsonObject();
2636
}
2737

2838
@PostMapping("/takeFromFactory")
39+
@Operation(summary = "Take tiles from factory",
40+
description = "Take tiles of a specific color from a factory display, drop some on the floor (if you want) and put them on a pattern line")
41+
@ApiResponse(responseCode = "200", description = "Updated game state after taking tiles")
42+
@io.swagger.v3.oas.annotations.parameters.RequestBody(
43+
content = @Content(
44+
examples = {
45+
@ExampleObject(
46+
name = "Take RED tiles",
47+
description = "The example is not guaranteed to work due to randomness of game. Please check the board state first!",
48+
value = "{\"factoryIndex\": 0, \"tileToTake\": \"RED\", \"tilesToPutOnFloor\": 0, \"patternLineIndex\": 1}"
49+
)
50+
}
51+
)
52+
)
2953
public Map<String, Object> takeTilesFromFactory(@RequestBody FactoryTakingRequest factoryTakingRequest) {
3054
game.executeFactoryOfferPhaseWithFactory(
3155
factoryTakingRequest.factoryIndex(), factoryTakingRequest.tileToTake(),
@@ -35,6 +59,20 @@ public Map<String, Object> takeTilesFromFactory(@RequestBody FactoryTakingReques
3559
}
3660

3761
@PostMapping("/takeFromCenter")
62+
@Operation(summary = "Take tiles from center",
63+
description = "Take tiles of a specific color from the center area, drop some on the floor (if you want) and put them on a pattern line")
64+
@ApiResponse(responseCode = "200", description = "Updated game state after taking tiles")
65+
@io.swagger.v3.oas.annotations.parameters.RequestBody(
66+
content = @Content(
67+
examples = {
68+
@ExampleObject(
69+
name = "Take BLUE tiles",
70+
description = "The example is not guaranteed to work due to randomness of game. Please check the board state first!",
71+
value = "{\"tileToTake\": \"BLUE\", \"tilesToPutOnFloor\": 1, \"patternLineIndex\": 2}"
72+
)
73+
}
74+
)
75+
)
3876
public Map<String, Object> takeTilesFromCenter(@RequestBody CenterTakingRequest centerTakingRequest) {
3977
game.executeFactoryOfferPhaseWithCenter(
4078
centerTakingRequest.tileToTake(), centerTakingRequest.tilesToPutOnFloor(),

0 commit comments

Comments
 (0)