99import io .dinject .controller .Produces ;
1010import io .dinject .controller .Put ;
1111import io .dinject .javalin .generator .javadoc .Javadoc ;
12- import io .swagger .v3 .oas .annotations .Hidden ;
13- import io .swagger .v3 .oas .models .Operation ;
14- import io .swagger .v3 .oas .models .PathItem ;
15- import io .swagger .v3 .oas .models .responses .ApiResponse ;
16- import io .swagger .v3 .oas .models .responses .ApiResponses ;
12+ import io .dinject .javalin .generator .openapi .MethodDocBuilder ;
1713
1814import javax .lang .model .element .ExecutableElement ;
1915import javax .lang .model .element .VariableElement ;
2622
2723import static io .dinject .javalin .generator .Constants .JAVALIN_ROLES ;
2824
29- class MethodReader {
25+ public class MethodReader {
3026
3127 private final ProcessingContext ctx ;
3228 private final ControllerReader bean ;
@@ -76,12 +72,16 @@ boolean isWebMethod() {
7672 return webMethod != null ;
7773 }
7874
75+ public Javadoc getJavadoc () {
76+ return javadoc ;
77+ }
78+
7979 private String produces (ControllerReader bean ) {
8080 final Produces produces = findAnnotation (Produces .class );
8181 return (produces != null ) ? produces .value () : bean .getProduces ();
8282 }
8383
84- <A extends Annotation > A findAnnotation (Class <A > type ) {
84+ public <A extends Annotation > A findAnnotation (Class <A > type ) {
8585 A annotation = element .getAnnotation (type );
8686 if (annotation != null ) {
8787 return annotation ;
@@ -120,80 +120,16 @@ void read() {
120120 }
121121 }
122122
123- void addMeta (ProcessingContext ctx ) {
124-
125- if (webMethod != null && notHidden ()) {
126-
127- Operation operation = new Operation ();
128- //operation.setOperationId();
129- operation .setSummary (javadoc .getSummary ());
130- operation .setDescription (javadoc .getDescription ());
131-
132- if (javadoc .isDeprecated ()) {
133- operation .setDeprecated (true );
134- } else if (findAnnotation (Deprecated .class ) != null ) {
135- operation .setDeprecated (true );
136- }
137-
138- PathItem pathItem = ctx .pathItem (fullPath );
139- switch (webMethod ) {
140- case GET :
141- pathItem .setGet (operation );
142- break ;
143- case PUT :
144- pathItem .setPut (operation );
145- break ;
146- case POST :
147- pathItem .setPost (operation );
148- break ;
149- case DELETE :
150- pathItem .setDelete (operation );
151- break ;
152- case PATCH :
153- pathItem .setPatch (operation );
154- break ;
155- }
156-
157- for (MethodParam param : params ) {
158- param .addMeta (ctx , javadoc , operation );
159- }
160-
161- ApiResponses responses = new ApiResponses ();
162- operation .setResponses (responses );
163-
164- ApiResponse response = new ApiResponse ();
165- response .setDescription (javadoc .getReturnDescription ());
166-
167- if (isVoid ) {
168- if (isEmpty (response .getDescription ())) {
169- response .setDescription ("No content" );
170- }
171- } else {
172- String contentMediaType = (produces == null ) ? MediaType .APPLICATION_JSON : produces ;
173- response .setContent (ctx .createContent (returnType (), contentMediaType ));
174- }
175- responses .addApiResponse (httpStatusCode (), response );
176- }
177- }
178-
179- private TypeMirror returnType () {
180- if (actualExecutable != null ) {
181- return actualExecutable .getReturnType ();
182- }
183- return element .getReturnType ();
184- }
185-
186- private boolean isEmpty (String value ) {
187- return value == null || value .isEmpty ();
188- }
189-
190123 /**
191- * Return true if the method is included in documentation .
124+ * Build the OpenAPI documentation for the method / operation .
192125 */
193- private boolean notHidden () {
194- return !ctx .isOpenApiAvailable () || findAnnotation (Hidden .class ) == null ;
126+ void buildApiDocumentation (ProcessingContext ctx ) {
127+ if (webMethod != null ) {
128+ new MethodDocBuilder (this , ctx .doc ()).build ();
129+ }
195130 }
196131
132+
197133 void addRoute (Append writer ) {
198134
199135 if (webMethod != null ) {
@@ -202,7 +138,7 @@ void addRoute(Append writer) {
202138 segments = PathSegments .parse (fullPath );
203139
204140 writer .append (" ApiBuilder.%s(\" %s\" , ctx -> {" , webMethod .name ().toLowerCase (), segments .fullPath ()).eol ();
205- writer .append (" ctx.status(%s);" , httpStatusCode ()).eol ();
141+ writer .append (" ctx.status(%s);" , getStatusCode ()).eol ();
206142
207143 List <PathSegments .Segment > metricSegments = segments .metricSegments ();
208144 for (PathSegments .Segment metricSegment : metricSegments ) {
@@ -214,7 +150,7 @@ void addRoute(Append writer) {
214150 }
215151 writer .append (" " );
216152
217- if (isReturnContent ()) {
153+ if (! isVoid ()) {
218154 writeContextReturn (writer );
219155 }
220156 writer .append ("controller." );
@@ -226,7 +162,7 @@ void addRoute(Append writer) {
226162 params .get (i ).buildParamName (writer );
227163 }
228164 writer .append (")" );
229- if (isReturnContent ()) {
165+ if (! isVoid ()) {
230166 writer .append (")" );
231167 }
232168 writer .append (";" ).eol ();
@@ -264,14 +200,6 @@ private List<String> roles() {
264200 return methodRoles .isEmpty () ? bean .getRoles () : methodRoles ;
265201 }
266202
267- private String httpStatusCode () {
268- return Integer .toString (webMethod .statusCode (isVoid ));
269- }
270-
271- private boolean isReturnContent () {
272- return !isVoid ;
273- }
274-
275203 private boolean readMethodAnnotation () {
276204
277205 Form form = findAnnotation (Form .class );
@@ -307,4 +235,36 @@ private boolean setWebMethod(WebMethod webMethod, String value) {
307235 this .webMethodPath = value ;
308236 return true ;
309237 }
238+
239+ public WebMethod getWebMethod () {
240+ return webMethod ;
241+ }
242+
243+ public String getFullPath () {
244+ return fullPath ;
245+ }
246+
247+ public List <MethodParam > getParams () {
248+ return params ;
249+ }
250+
251+ public boolean isVoid () {
252+ return isVoid ;
253+ }
254+
255+ public String getProduces () {
256+ return produces ;
257+ }
258+
259+ public TypeMirror getReturnType () {
260+ if (actualExecutable != null ) {
261+ return actualExecutable .getReturnType ();
262+ }
263+ return element .getReturnType ();
264+ }
265+
266+ public String getStatusCode () {
267+ return Integer .toString (webMethod .statusCode (isVoid ));
268+ }
269+
310270}
0 commit comments