swaggerblueprint

Blueprint project
git clone git://git.wimdupont.com/swaggerblueprint.git
Log | Files | Refs | README | LICENSE

commit f785774200199ff34fad1179edd3eea8cfd77489
parent 2783bfbe6da317a3123e45327a2700d381a3ab4a
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sun, 19 Mar 2023 00:06:39 +0100

added actual validation

Diffstat:
Msrc/main/java/com/wimdupont/swagger/blueprint/controller/BlueprintController.java | 5+++--
Msrc/main/java/com/wimdupont/swagger/blueprint/controller/dto/BlueprintDto.java | 19++++++++++++-------
Asrc/main/java/com/wimdupont/swagger/blueprint/controller/dto/validation/ReadGroup.java | 4++++
Msrc/main/resources/application.properties | 3+++
Msrc/test/resources/play.sh | 67++++++++++++++++++++++++++++++++++++++++++-------------------------
5 files changed, 64 insertions(+), 34 deletions(-)

diff --git a/src/main/java/com/wimdupont/swagger/blueprint/controller/BlueprintController.java b/src/main/java/com/wimdupont/swagger/blueprint/controller/BlueprintController.java @@ -3,6 +3,7 @@ package com.wimdupont.swagger.blueprint.controller; import com.wimdupont.swagger.blueprint.controller.dto.BlueprintDto; import com.wimdupont.swagger.blueprint.service.BlueprintService; import io.swagger.v3.oas.annotations.Operation; +import jakarta.validation.Valid; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -39,13 +40,13 @@ public class BlueprintController { @PostMapping @Operation(summary = "Create blueprint", description = "This will create a new blueprint") - public BlueprintDto createBlueprint(@RequestBody BlueprintDto blueprintDto) { + public BlueprintDto createBlueprint(@RequestBody @Valid BlueprintDto blueprintDto) { return blueprintService.createBlueprint(blueprintDto); } @PutMapping("/{id}") @Operation(summary = "Update blueprint", description = "This will update the existing blueprint") - public BlueprintDto updateBlueprint(@PathVariable UUID id, @RequestBody BlueprintDto blueprintDto) { + public BlueprintDto updateBlueprint(@PathVariable UUID id, @RequestBody @Valid BlueprintDto blueprintDto) { return blueprintService.updateBlueprint(id, blueprintDto); } diff --git a/src/main/java/com/wimdupont/swagger/blueprint/controller/dto/BlueprintDto.java b/src/main/java/com/wimdupont/swagger/blueprint/controller/dto/BlueprintDto.java @@ -2,6 +2,7 @@ package com.wimdupont.swagger.blueprint.controller.dto; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; +import com.wimdupont.swagger.blueprint.controller.dto.validation.ReadGroup; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema.AccessMode; import jakarta.validation.constraints.Min; @@ -12,17 +13,21 @@ import java.util.UUID; @JsonDeserialize(builder = BlueprintDto.Builder.class) public class BlueprintDto { - @NotNull - @Schema(accessMode = AccessMode.READ_ONLY, description = "Unique identifier of the blueprint", example = "030e5b9d-b4ed-4900-af24-cb2eac570056") - UUID id; + @NotNull(groups = ReadGroup.class) + @Schema(accessMode = AccessMode.READ_ONLY, + description = "Unique identifier of the blueprint", + example = "030e5b9d-b4ed-4900-af24-cb2eac570056") + private UUID id; @NotNull - @Schema(description = "Name of the blueprint", example = "blueprint-name") - String name; + @Schema(description = "Name of the blueprint", + example = "blueprint-name") + private String name; @Min(10) - @Schema(description = "Number of the blueprint", example = "42") - Integer number; + @Schema(description = "Number of the blueprint", + example = "42") + private Integer number; private BlueprintDto(Builder builder) { setId(builder.id); diff --git a/src/main/java/com/wimdupont/swagger/blueprint/controller/dto/validation/ReadGroup.java b/src/main/java/com/wimdupont/swagger/blueprint/controller/dto/validation/ReadGroup.java @@ -0,0 +1,4 @@ +package com.wimdupont.swagger.blueprint.controller.dto.validation; + +public interface ReadGroup { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties @@ -1,3 +1,6 @@ springdoc.packagesToScan=com.wimdupont.swagger.blueprint springdoc.pathsToMatch=/v1, /blueprints/** + server.error.include-message=ALWAYS +server.error.include-binding-errors=ON_PARAM +server.error.include-exception=false diff --git a/src/test/resources/play.sh b/src/test/resources/play.sh @@ -1,8 +1,20 @@ #!/bin/sh +base_url="http://localhost:8080/blueprints" + +with_error=0 + +while getopts "e" arg; do + case $arg in + e) with_error=1;; + *);; + esac +done + +shift $((OPTIND-1)) + method=$1 id=$2 -base_url="http://localhost:8080/blueprints" if [ -z "${method}" ]; then echo "Request method? (GET/POST/PUT/DELETE)" @@ -16,35 +28,40 @@ get_id () { fi } +get_url () { + base_url+="$1" + if [ "${with_error}" -eq 1 ]; then + base_url="${base_url}?errors=true" + fi + echo "${base_url}" +} + case ${method^^} in - GET) - echo "Leave id empty to retrieve all." - get_id - if [ -z "${id}" ]; then - curl -X GET -sw '%{http_code}' "${base_url}" | jq - else - curl -X GET -sw '%{http_code}' "${base_url}/${id}" | jq - fi - ;; - - POST) - curl -X POST -sw '%{http_code}' "${base_url}" -H 'Content-Type: application/json' -d '{"name":"Created John","number":120}' | jq - ;; - - PUT) + GET) get_id - curl -X PUT -sw '%{http_code}' "${base_url}/${id}" -H 'Content-Type: application/json' -d '{"name":"Updated John","number":166}' | jq - ;; + if [ -z "${id}" ]; then + curl -X GET -sw '%{http_code}' "$(get_url)" | jq + else + curl -X GET -sw '%{http_code}' "$(get_url "/${id}")" | jq + fi + ;; - DELETE) - get_id - curl -k -X 'DELETE' -sw '%{http_code}' "${base_url}/${id}" | jq + POST) + curl -X POST -sw '%{http_code}' "$(get_url)" -H 'Content-Type: application/json' -d '{"name":"Created John","number":120}' | jq ;; - *) - echo "Unknown http method." - ;; -esac + PUT) + get_id + curl -X PUT -sw '%{http_code}' "$(get_url "/${id}")" -H 'Content-Type: application/json' -d '{"name":null,"number":166}' | jq + ;; + DELETE) + get_id + curl -k -X 'DELETE' -sw '%{http_code}' "$(get_url "/${id}")" | jq + ;; + *) + echo "Unknown http method." + ;; +esac