Skip to content

Commit

Permalink
Handle the case where @Default annotation is specified withou… (#1864)
Browse files Browse the repository at this point in the history
Motivation:

A `NoSuchElementException` is raised during server startup time if a
user specified a `@Default` annotation without a value:

    public class MyAnnotatedService {
        @get("/foo")
        public String foo(@param @default String value) {
            return String.valueOf(value);
        }
    }

Modifications:

- Allow having no value in `@Default` annotation

Result:

- Fixes #1858
  • Loading branch information
trustin committed Jun 28, 2019
1 parent e6cd273 commit db94a79
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Expand Up @@ -1023,7 +1023,7 @@ private AnnotatedValueResolver build() {
}

shouldExist = false;
defaultValue = getSpecifiedValue(aDefault.value()).get();
defaultValue = getSpecifiedValue(aDefault.value()).orElse(null);
} else {
// Warn if @Default exists in an unsupported place.
final StringBuilder msg = new StringBuilder();
Expand Down
Expand Up @@ -481,6 +481,13 @@ public String paramDefault2(RequestContext ctx,
return username + '/' + password;
}

@Get
@Path("/param/default_null")
public String paramDefaultNull(RequestContext ctx, @Param @Default String value) {
validateContext(ctx);
return value;
}

@Get
@Path("/param/precedence/{username}")
public String paramPrecedence(RequestContext ctx,
Expand Down Expand Up @@ -779,6 +786,8 @@ public void testParam() throws Exception {
testBody(hc, get("/7/param/precedence/line5?username=dot&password=armeria5"), "line5/armeria5");

testStatusCode(hc, get("/7/param/default2"), 400);

testBody(hc, get("/7/param/default_null"), "(null)");
}
}

Expand Down

0 comments on commit db94a79

Please sign in to comment.