swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.94k stars 6.03k forks source link

[Jaxrs-spec] Improvements for Jaxrs-spec #4509

Open jfiala opened 7 years ago

jfiala commented 7 years ago
Description
Swagger-codegen version

2.2.2-SNAPSHOT

Related issues

4327

Suggest a Fix
wing328 commented 7 years ago

generate api as interface (which then can be easily implemented)

I believe this will be a breaking change so better target 2.3.0 for this enhancement.

jfiala commented 7 years ago

If you don't want a breaking change, we could also add the interface with a suffix (e.g. "I" - "MyServiceI") and the class implementing the interface would only generate if it doesn't exist, thus making it non-breaking or add the interface approach as a library or separate language?

jfiala commented 7 years ago

Implemented solution at PR #4717, please review and advise if you prefer a non-breaking copy of the jaxrs-spec language or a breaking change for 2.3.0.

ePaul commented 7 years ago

as there is currently no standardized way for supporting multipart/form-data (see https://java.net/jira/browse/JAX_RS_SPEC-413), I changed formParams.mustache to

 @FormParam(value = "{{paramName}}") InputStream {{paramName}}InputStream

Please use {{baseName}} instead of {{paramName}} in all annotations which refer to the actual on-the-wire parameter name. (See #4898.)

mamghari commented 7 years ago

Hi @jfiala,

About the point:

I think it is generally a bad practice for RESTful web services because whitout the Response object how do you manage the HTTP code returned ? How do you add HTTP Headers in your response?

It could be another option for the plugin but it is a bad practice... this is my point of view.

Tillerino commented 7 years ago

Hi @mamghari

IMO, a much cleaner way of handling response codes in JAX-RS is using WebApplicationException and its child classes. For example to return a 404, throw a NotFoundException from your endpoint. There are a bunch of exceptions shipped with the JAX-RS specification, you can extend WebApplicationException with your own exceptions and error codes, or even throw arbitrary ad-hoc exceptions using the various constructors of WebApplicationException - including specifying a Response object.

The clear benefit of this is that the interface shows your business logic and remains easily testable.

keithchong commented 7 years ago

I'm using the current online swagger editor to generate the jaxrs-spec server code from the example petstore definition. I'm seeing the unresolved (no import added) Attachment issue but also, I'm seeing an issue with the InputStream reference - it does not have an import as well:

public Response uploadFile(@PathParam("petId") @ApiParam("ID of pet to update") Long petId,@FormParam(value = "additionalMetadata")  String additionalMetadata, @FormParam(value = "file") InputStream fileInputStream, @FormParam(value = "file") Attachment fileDetail) {