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 on 'double' Nested Fields doesn't work #212

Open
LeeJaeDoo opened this issue Nov 8, 2022 · 0 comments
Open

Bean Validation on 'double' Nested Fields doesn't work #212

LeeJaeDoo opened this issue Nov 8, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@LeeJaeDoo
Copy link

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

testImplementation("com.epages:restdocs-api-spec-mockmvc:0.16.2")

As you can see below, WorkflowDefinitionRequest has property SwitchBackBucketGroupParams[].SwitchBackBucketParams[].activity and it should not be null.

@Getter
@NoArgsConstructor
public class WorkflowDefinitionRequest {

    @NotBlank
    private String name;
    private String defDescription;

    @Valid
    @NotEmpty
    private List<SwitchBackBucketGroupParams> switchBackBucketGroupList;
}

@Getter
@NoArgsConstructor
public class SwitchBackBucketGroupParams {

    @NotNull
    private BucketType bucketType;

    private String description;

    @Valid
    @NotEmpty
    private List<SwitchBackBucketParams> switchBackBucketList;

}

@Getter
@NoArgsConstructor
public class SwitchBackBucketParams {

    @Valid
    @NotNull
    private ExperimentalActivityParams activity;

}

@Getter
@NoArgsConstructor
public class ExperimentalActivityParams {

    @NotBlank
    private String name;

    private String description;

    @NotNull
    private ActivityType type;
}
    @Test
    void createWorkflowDefinition() throws Exception {
        given(workflowService.registerWorkflow(any())).willReturn(1L);
        ConstrainedFields constrainedFields = new ConstrainedFields(WorkflowDefinitionRequest.class);

        this.mockMvc
            .perform(RestDocumentationRequestBuilders.post(URL_WORKFLOW)
                                                     .contentType(APPLICATION_JSON)
                                                     .content(readJson("workflowDefinitionRequest.json")))
            .andExpect(status().isCreated())
            .andDo(document("create-workflow-definition",
                            resource(ResourceSnippetParameters.builder()
                                         .description("create test")
                                         .requestSchema(Schema.schema("workflow create request"))
                                         .responseSchema(Schema.schema("201 response"))
                                         .tag(TAG_WORKFLOW_BUILD)
                                         .requestFields(constrainedFields.withPath("name").type(JsonFieldType.STRING).description("name"),
            constrainedFields.withPath("description").type(JsonFieldType.STRING).optional().description("description"),
            constrainedFields.withPath("switchBackBucketGroupList").type(JsonFieldType.ARRAY).description("bucket group"),
            constrainedFields.withMappedPath("switchBackBucketGroupList[].bucketType", 
"switchBackBucketGroupList").type(JsonFieldType.STRING).description("type of bucket group"),
constrainedFields.withMappedPath("switchBackBucketGroupList[].description", "switchBackBucketGroupList").type(JsonFieldType.STRING).optional().description("description of bucket group"),
            constrainedFields.withMappedPath("switchBackBucketGroupList[].switchBackBucketList", "switchBackBucketGroupList").type(JsonFieldType.ARRAY).description("bucket list of bucket group"),
            constrainedFields.withMappedPath("switchBackBucketGroupList[].switchBackBucketList[].activity", "switchBackBucketGroupList[].switchBackBucketList[]").type(JsonFieldType.OBJECT).description("activity of bucket list"),
            constrainedFields.withMappedPath("switchBackBucketGroupList[].switchBackBucketList[].activity.name", "switchBackBucketGroupList[].switchBackBucketList[].activity").type(JsonFieldType.STRING).optional().description("name of activity"),
            constrainedFields.withMappedPath("switchBackBucketGroupList[].switchBackBucketList[].activity.description", "switchBackBucketGroupList[].switchBackBucketList[].activity").type(JsonFieldType.STRING).optional().description("description of activity"),
            constrainedFields.withMappedPath("switchBackBucketGroupList[].switchBackBucketList[].activity.type", "switchBackBucketGroupList[].switchBackBucketList[].activity").type(JsonFieldType.STRING).description("type of activity")
                                                              .build())));
}

Generated openapi-3.0.yml file. As you can see below, author should be required but it doesn't show up in the required fields.

components:
  schemas:
    workflow create request:
      title: workflow create request
      required:
      - name
      - switchBackBucketGroupList
      type: object
      properties:
        name:
          minLength: 1
          type: string
          description: name
        defDescription:
          type: string
          description: definition description
        switchBackBucketGroupList:
          type: array
          description: bucket group
          items:
            required:
            - bucketType
            - description     // 'switchBackBucketGroupList' is a required object, 'switchBackBucketGroupList.description' is not a required field, but 'switchBackBucketGroupList.description' is exposed as a required field
            - switchBackBucketList
            type: object
            properties:
              switchBackBucketList:
                type: array
                description: bucket list of bucket group
                items:
                // 'required' not exposed
                  type: object
                  properties:
                    activity:
                      type: object
                      properties:
                        description:
                          type: string
                          description: description of activity
                        type:
                          type: string
                          description: type of activity
                        name:
                          type: string
                          description: name of activity
                      description: activity of bucket list

(+ Required is marked on property other than required within the marked object)

Is it a bug or am I missing something?

@LeeJaeDoo LeeJaeDoo changed the title Bean Validation on double Nested Fields doesn't work Bean Validation on 'double' Nested Fields doesn't work Nov 8, 2022
@ozscheyge ozscheyge added the bug Something isn't working label Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants