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

API does not have a return value. but API spec has #1490

Open
anilj1 opened this issue Nov 19, 2018 · 3 comments
Open

API does not have a return value. but API spec has #1490

anilj1 opened this issue Nov 19, 2018 · 3 comments

Comments

@anilj1
Copy link

anilj1 commented Nov 19, 2018

Generated API does not return the HTTP response code.

As shown below, the API specification has responses 200 and 400; however, when the addTeam() API is generated, it is generated with the return type 'void'.

I want to handle the response code 200 and/or 400. Does it mean I have explicitly, define the payload type in the response specification? Can someone please provide more details on how my 'responses' specification should be?

openapi generator 3.2.0
Swagger declaration file content or url
 49   /switches:
 50     post:                                                                                           
 51       summary: Add team                                                                           
 52       operationId: addTeam                                                                        
 53       requestBody:                                                                                  
 54         description: Team detail being added                                                      
 55         content:                                                                                    
 56           application/json:                                                                         
 57             schema:                                                                                 
 58               type: array                                                                           
 59               items:                                                                                
 60                 $ref: "#/components/schemas/addTeamPayload"                                                                           
 61       responses:                                                                                    
 62         200:                                                                                        
 63           description: Ok                                                                           
 64         400:                                                                                        
 65           description: Bad request                                                                  
 66       tags:                                                                                         
 67         - Team
Command line used for generation

java -jar openapi-generator-cli-3.2.0.jar generate -i teamApiSpec.yaml -g java

Generated Java code:
/**
 * Add team
 * 
 * @param body Team detail being added (optional)
 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
    
 */
public void addTeam(List<AddTeamPayload> body) throws ApiException {
    addTeamWithHttpInfo(body);
}

/**
 * Add Team
 * 
 * @param body Team detail being added (optional)
 * @return ApiResponse&lt;Void&gt;
 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
    
 */
public ApiResponse<Void> addTeamWithHttpInfo(List<AddTeamPayload> body) throws ApiException {
    com.squareup.okhttp.Call call = addTeamValidateBeforeCall(body, null, null);
    return apiClient.execute(call);
}
@anilj1
Copy link
Author

anilj1 commented Nov 20, 2018

Can someone please comment on this? This is important. Let me know if I have missed something in my API spec.

@anilj1
Copy link
Author

anilj1 commented Nov 20, 2018

As an additional verification, I tested the same API spec and generated the python code, and this API returns the values. However, Java code does not.

 36     def add_team(self, **kwargs):  # noqa: E501                                                   
 37         """Add team  # noqa: E501                                                                 
 38                                                                                                     
 39         This method makes a synchronous HTTP request by default. To make an                         
 40         asynchronous HTTP request, please pass async_req=True                                       
 41         >>> thread = api.add_team(async_req=True)                                                 
 42         >>> result = thread.get()                                                                   
 43                                                                                                     
 44         :param async_req bool                                                                       
 45         :param list[AddTeamPayload] add_team_payload: Team detail being added                 
 46         :return: None                                                                               
 47                  If the method is called asynchronously,                                            
 48                  returns the request thread.                                                        
 49         """                                                                                         
 50         kwargs['_return_http_data_only'] = True                                                     
 51         if kwargs.get('async_req'):                                                                 
 52             return self.add_team_with_http_info(**kwargs)  # noqa: E501                           
 53         else:                                                                                       
 54             (data) = self.add_team_with_http_info(**kwargs)  # noqa: E501                         
 55             return data 

@dmitry-weirdo
Copy link

dmitry-weirdo commented Jun 23, 2020

We ran into the same problem: when some HTTP method in OAS has no content (which is correct according to https://swagger.io/docs/specification/describing-responses/), the jaxrs-spec generator creates void method instead of methods returning Response.

The template for this method in jaxrs-spec generator is org\openapitools\openapi-generator\4.3.1\openapi-generator-4.3.1.jar!\JavaJaxRS\spec\apiInterface.mustache

Notably, the jaxrs-jersey generator generates Response returns for the same OAS.

Therefore, it seems to be a bug in jaxrs-spec generator.

UPDATE: there is a config option returnResponse for jaxrs-spec generator (see https://openapi-generator.tech/docs/generators/jaxrs-spec), but it makes all the methods return Response, i.e. even those methods which return an object from schema.

One option we may consider is to use custom status annotation with filter on void methods, see https://stackoverflow.com/questions/49446798/setting-a-jersey-response-status-code-from-interface-without-returning-response/49459897#49459897.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants