Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bean Validation @Min @Max Not working #247

Open
devsmilekang opened this issue May 11, 2023 · 1 comment
Open

Bean Validation @Min @Max Not working #247

devsmilekang opened this issue May 11, 2023 · 1 comment

Comments

@devsmilekang
Copy link

devsmilekang commented May 11, 2023

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
Copy link
Author

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?

@devsmilekang devsmilekang changed the title Bean Validation @Min @Max Not working with Bean Validation @Min @Max Not working May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant