openapi-processor-spring icon indicating copy to clipboard operation
openapi-processor-spring copied to clipboard

Type annotation on request body model erroneously results in parameter annotation as well

Open vvanpo opened this issue 3 months ago • 1 comments

I've pushed a failing test case to illustrate this behaviour: https://github.com/openapi-processor/openapi-processor-spring/compare/master...vvanpo:openapi-processor-spring:master

When running this, the test output shows the following diff:

/.../openapi-processor-spring/build/resources/testInt/tests/params-request-body-annotation/outputs/api/Api.java
--- /.../openapi-processor-spring/build/resources/testInt/tests/params-request-body-annotation/outputs/api/Api.java
+++ /.../generated/api/Api.java
@@ -1,8 +1,9 @@
 package generated.api;
 
 import generated.model.Book;
 import generated.support.Generated;
+import lombok.Builder;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
@@ -11,7 +12,7 @@
 public interface Api {
 
     @ResponseStatus(HttpStatus.CREATED)
     @PostMapping(path = "/book", consumes = {"application/json"}, produces = {"application/json"})
-    Book postBook(@RequestBody Book body);
+    Book postBook(@RequestBody @Builder Book body);
 
 }
/.../openapi-processor-spring/build/resources/testInt/tests/params-request-body-annotation/outputs/api/Api.java
--- /.../openapi-processor-spring/build/resources/testInt/tests/params-request-body-annotation/outputs/api/Api.java
+++ /.../generated/api/Api.java
@@ -1,8 +1,9 @@
 package generated.api;
 
 import generated.model.Book;
 import generated.support.Generated;
+import lombok.Builder;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
@@ -11,7 +12,7 @@
 public interface Api {
 
     @ResponseStatus(HttpStatus.CREATED)
     @PostMapping(path = "/book", consumes = {"application/json"}, produces = {"application/json"})
-    Book postBook(@RequestBody Book body);
+    Book postBook(@RequestBody @Builder Book body);
 
 }

Basically, annotating a model that also happens to be a request body results in an unwanted parameter annotation, that in this case breaks because lombok.Builder is not a valid parameter annotation.

vvanpo avatar Oct 10 '25 13:10 vvanpo

yeah, I see. The annotation @Target is a little bit tricky. The processor just sees strings.

a workaround may be object. This will add the annotation only at the class level. An issue could be that it adds the annotation to all generated dtos.

openapi-processor-mapping: v14

options:
  package-name: io.openapiprocessor.openapi

map:
  types:
    - type: object @ lombok.Builder

I'm thinking about something like this to help with annotation targets:

openapi-processor-mapping: v14

options:
  package-name: generated

map:
  types:
    - type: Book @ lombok.Builder

annotationTargets:
 - lombok.Builder: ['object' /*, 'parameter', 'property'*/]

The processor could have a hardcoded list of common annotations that would be extended by annotationTargets.

It may also help with openapi-processor/openapi-processor-base#304

hauner avatar Oct 10 '25 14:10 hauner