ePages-de / ePages-de/restdocs-api-spec

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

Open
#212 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Kotlin
Stars
427
Forks
116
Avg merge
23h 54m
Merged PRs (30d)
1

Description

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

```groovy
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.

```java
@Getter
@NoArgsConstructor
public class WorkflowDefinitionRequest {

@NotBlank
private String name;
private String defDescription;

@Valid
@NotEmpty
private List switchBackBucketGroupList;
}

@Getter
@NoArgsConstructor
public class SwitchBackBucketGroupParams {

@NotNull
private BucketType bucketType;

private String description;

@Valid
@NotEmpty
private List 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;
}
```
```java
@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.

```yml
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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.