BerkleyTechnologyServices / restdocs-spec

A maven plugin for generating Open API and Postman Collection specifications using Spring Restdocs.
Apache License 2.0
56 stars 18 forks source link

Bean Validation @Min @Max Not working #247

Open devsmilekang opened 1 year ago

devsmilekang commented 1 year ago

I'm trying to use ConstrainedFields for Bean Validation @Min, @Max but unable to get it to work.

<restdocs-spec.version>0.21</restdocs-spec.version>
<restdocs-api-spec.version>0.16.4</restdocs-api-spec.version>

As you can see below, SRequestDTO has property 'cc' and it should over than 0.

public record SRequestDTO(@NotEmpty String bb, @Min(value=1) int cc){}
void aa() throws Exception {
    SRequestDTO sRequest = new SRequestDTO("3", 3);
    ObjectMapper objectMapper = new ObjectMapper();

    ResultActions perform = mockMvc.perform(post("/aa")
            .contentType(MediaType.APPLICATION_JSON_VALUE)
            .content(objectMapper.writeValueAsString(sRequest)));

    ConstrainedFields fields = new ConstrainedFields(SRequestDTO.class);

    FieldDescriptor bb = fields.withPath("bb");

    perform.andExpect(MockMvcResultMatchers.status().isOk()).andDo(document("doc"

            , requestFields(
                    fields.withPath("bb").description("test bean validation"),
                    fields.withPath("cc").description("not working max")

            )
            , responseFields(
                    fieldWithPath("ab").description("ab")
            )
    ));
}

Generated openapi-3.0.json file. As you can see below, cc should be minimum 1.

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "Open Api Spec 3",
    "description" : "order-order-api 문서",
    "version" : "0.0.1-SNAPSHOT"
  },
  "servers" : [ {
    "url" : "http://localhost"
  } ],
  "tags" : [ ],
  "paths" : {
    "/aa" : {
      "post" : {
        "tags" : [ "aa" ],
        "operationId" : "doc",
        "requestBody" : {
          "content" : {
            "application/json;charset=UTF-8" : {
              "schema" : {
                "$ref" : "#/components/schemas/aa265663360"
              },
              "examples" : {
                "doc" : {
                  "value" : "{\"bb\":\"3\",\"cc\":3}"
                }
              }
            }
          }
        },
        "responses" : {
          "200" : {
            "description" : "200",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/aa-1389581174"
                },
                "examples" : {
                  "doc" : {
                    "value" : "{\"ab\":1}"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components" : {
    "schemas" : {
      "aa265663360" : {
        "required" : [ "bb" ],
        "type" : "object",
        "properties" : {
          "cc" : {
            "type" : "number",
            "description" : "not working max"
          },
          "bb" : {
            "minLength" : 1,
            "type" : "string",
            "description" : "test bean validation"
          }
        }
      },
      "aa-1389581174" : {
        "type" : "object",
        "properties" : {
          "ab" : {
            "type" : "number",
            "description" : "ab"
          }
        }
      }
    }
  }
}
devsmilekang commented 1 year ago

I found restdocs-api-spec support for @Min @Max constraints from 0.15.3 but restdocs-spec v0.21 support for restdocs-api-spec v0.11.4. Could you upgrade for it?