Skip to content

Commit f92bb66

Browse files
committed
Fix NullPointerException in MigrateApiParamSchemaValue for argument-less @ApiParam
A marker @ApiParam / @parameter annotation written without parentheses has null arguments (J.Annotation.getArguments() returns null, not an empty list). visitAnnotation iterated the arguments without a null check, throwing a NullPointerException and aborting the whole recipe run. Guard against null arguments and return the annotation unchanged, since there is nothing to migrate when no arguments are present.
1 parent e55d6ff commit f92bb66

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/main/java/org/openrewrite/openapi/swagger/MigrateApiParamSchemaValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class MigrateApiParamSchemaValue extends JavaIsoVisitor<ExecutionContext> {
4444
@Override
4545
public Annotation visitAnnotation(Annotation annotation, ExecutionContext ctx) {
4646
J.Annotation a = super.visitAnnotation(annotation, ctx);
47-
if (!PARAMETER_ANNOTATION_MATCHER.matches(a)) {
47+
if (!PARAMETER_ANNOTATION_MATCHER.matches(a) || a.getArguments() == null) {
4848
return a;
4949
}
5050

src/test/java/org/openrewrite/openapi/swagger/SwaggerToOpenAPITest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,56 @@ class Example {
198198
);
199199
}
200200

201+
@Test
202+
void migrateApiParamWithoutArguments() {
203+
rewriteRun(
204+
//language=java
205+
java(
206+
"""
207+
import io.swagger.annotations.ApiParam;
208+
209+
class Example {
210+
@ApiParam
211+
private Integer foo;
212+
}
213+
""",
214+
"""
215+
import io.swagger.v3.oas.annotations.Parameter;
216+
217+
class Example {
218+
@Parameter
219+
private Integer foo;
220+
}
221+
"""
222+
)
223+
);
224+
}
225+
226+
@Test
227+
void migrateApiParamWithoutArgumentsOnMethodParameter() {
228+
rewriteRun(
229+
//language=java
230+
java(
231+
"""
232+
import io.swagger.annotations.ApiParam;
233+
234+
class Example {
235+
void create(@ApiParam Integer foo) {
236+
}
237+
}
238+
""",
239+
"""
240+
import io.swagger.v3.oas.annotations.Parameter;
241+
242+
class Example {
243+
void create(@Parameter Integer foo) {
244+
}
245+
}
246+
"""
247+
)
248+
);
249+
}
250+
201251
/**
202252
*
203253
* Same test as {@link #migrateApiParam()} making sure the order of the annotation properties doesn't break the logic

0 commit comments

Comments
 (0)