Skip to content

Commit

Permalink
Handle list and map parameters in Validator (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu authored Oct 22, 2020
1 parent 070b130 commit 3f0105b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
28 changes: 21 additions & 7 deletions client-runtime/src/main/java/com/microsoft/rest/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,29 @@ public static void validate(Object parameter) {
|| parameterToken.isSupertypeOf(Period.class)) {
return;
}
if (TypeToken.of(List.class).isSupertypeOf(parameterType)) {
List<?> items = (List<?>) parameter;
for (Object item : items) {
Validator.validate(item);
}
}
else if (TypeToken.of(Map.class).isSupertypeOf(parameterType)) {
Map<?, ?> entries = (Map<?, ?>) parameter;
for (Map.Entry<?, ?> entry : entries.entrySet()) {
Validator.validate(entry.getKey());
Validator.validate(entry.getValue());
}
}
else {
Annotation skipParentAnnotation = parameterType.getAnnotation(SkipParentValidation.class);

Annotation skipParentAnnotation = parameterType.getAnnotation(SkipParentValidation.class);

if (skipParentAnnotation == null) {
for (Class<?> c : parameterToken.getTypes().classes().rawTypes()) {
validateClass(c, parameter);
if (skipParentAnnotation == null) {
for (Class<?> c : parameterToken.getTypes().classes().rawTypes()) {
validateClass(c, parameter);
}
} else {
validateClass(parameterType, parameter);
}
} else {
validateClass(parameterType, parameter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ public void validateList() throws Exception {
}
}

@Test
public void validateListParameter() throws Exception {
ListWrapper body = new ListWrapper();
body.list = new ArrayList<StringWrapper>();
Validator.validate(body); // pass
StringWrapper wrapper = new StringWrapper();
wrapper.value = "valid";
body.list.add(wrapper);
Validator.validate(body.list); // pass
}

@Test
public void validateMap() throws Exception {
MapWrapper body = new MapWrapper();
Expand Down

0 comments on commit 3f0105b

Please sign in to comment.