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

Allow missing OAI Parameter name when target also has JAX-RS param #293

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,10 @@ ParameterContext getParameterContext(ParameterContextKey key, AnnotationTarget t
ParameterContext context = params.get(key);

if (context == null) {
context = params.values().stream().filter(c -> haveSameAnnotatedTarget(c, target, key.name))
context = params
.values()
.stream()
.filter(c -> haveSameAnnotatedTarget(c, target, key.name))
.findFirst()
.orElse(null);
}
Expand All @@ -1212,13 +1215,11 @@ boolean haveSameAnnotatedTarget(ParameterContext context, AnnotationTarget targe

if (target.equals(context.target)) {
/*
* If the annotations have the same (non-method) target or the same name with a common
* method target, it's the same parameter.
*
* This covers the situation where a @Parameter annotation was missing either
* 'name' or 'location' attributes (or both).
* This logic formerly also required that the parameter names matched
* (nameMatches == true) or that the kind of the target was not a
* method.
*/
return nameMatches || target.kind() != Kind.METHOD;
return true;
}

if (nameMatches && target.kind() == Kind.METHOD && context.target.kind() == Kind.METHOD_PARAMETER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public void testParamNameOverride() throws IOException, JSONException {
ParamNameOverrideTestResource.class);
}

@Test
public void testCommonTargetMethodParameter() throws IOException, JSONException {
test("params.common-annotation-target-method.json", CommonTargetMethodParameterResource.class);
}

/***************** Test models and resources below. ***********************/

public static class Widget {
Expand Down Expand Up @@ -808,4 +813,19 @@ public String echo(
return p1;
}
}

@Path("/common-target-method")
static class CommonTargetMethodParameterResource {
@DefaultValue("10")
@QueryParam("limit")
@Parameter(description = "Description of the limit query parameter")
public void setLimit(int limit) {
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public String[] getRecords() {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"openapi": "3.0.1",
"paths": {
"/common-target-method": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
},
"parameters": [
{
"name": "limit",
"in": "query",
"description": "Description of the limit query parameter",
"schema": {
"format": "int32",
"default": 10,
"type": "integer"
}
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,10 @@ ParameterContext getParameterContext(ParameterContextKey key, AnnotationTarget t
ParameterContext context = params.get(key);

if (context == null) {
context = params.values().stream().filter(c -> haveSameAnnotatedTarget(c, target, key.name))
context = params
.values()
.stream()
.filter(c -> haveSameAnnotatedTarget(c, target, key.name))
.findFirst()
.orElse(null);
}
Expand All @@ -1227,14 +1230,7 @@ boolean haveSameAnnotatedTarget(ParameterContext context, AnnotationTarget targe
boolean nameMatches = Objects.equals(context.name, name);

if (target.equals(context.target)) {
/*
* If the annotations have the same (non-method) target or the same name with a common
* method target, it's the same parameter.
*
* This covers the situation where a @Parameter annotation was missing either
* 'name' or 'location' attributes (or both).
*/
return nameMatches || target.kind() != Kind.METHOD;
return true;
}

if (nameMatches && target.kind() == Kind.METHOD && context.target.kind() == Kind.METHOD_PARAMETER) {
Expand Down