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
17.04k stars 6.03k forks source link

[JAVA] Would be nice if StreamingOutput/Media type APPLICATION_OCTET_STREAM gives an Inputstream at the generated JAVA code. #4831

Open amrk7 opened 7 years ago

amrk7 commented 7 years ago
Description

If the media type is APPLICATION_OCTET_STREAM and the response is a javax.ws.rs.core.StreamingOutput, it would be nice to get an InputStream of bytes at the client side.

Currently the behaviour is, swagger generates its own StreamingOutput model which is apparently useless to do anything.

Swagger-codegen version
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.1</version>
Swagger declaration file content or url

Relevant portion of swagger json spec

  "get": {
    "vendorExtensions": {},
    "operationId": "downloadResults",
    "produces": [
      "application/octet-stream"
    ],
    "parameters": [],
    "responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "type": "ref",
          "required": false,
          "vendorExtensions": {},
          "$ref": "#/definitions/StreamingOutput",
          "refFormat": "INTERNAL",
          "simpleRef": "StreamingOutput"
        },
        "headers": {},
        "vendorExtensions": {}
      }
    }
  }
Command line used for generation

I am generating the spec myself as a part of maven goal through this

Swagger swagger = new Swagger();

    final BeanConfig beanConfig = new BeanConfig();
    beanConfig.setResourcePackage(ar[0]);
    Info info = new Info();
    beanConfig.setInfo(info);
    beanConfig.setSchemes(new String[]{"http"});
    beanConfig.setBasePath("/");

    final Scanner scanner = beanConfig;
    swagger.info(info);
    Set<Class<?>> classes = scanner.classes();

    Reader reader = new Reader(swagger, null);
    swagger = reader.read(classes);
    swagger = ((SwaggerConfig) scanner).configure(swagger);

    SwaggerConfigLocator.getInstance().putSwagger("swagger.config.id.default", swagger);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    FileUtils.writeStringToFile(new File(ar[1]), mapper.writeValueAsString(swagger));

Used the following maven plugin artifact to generate code from spec

<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.1</version>
Steps to reproduce

Return a javax.ws.rs.core.StreamingOutput with media type as APPLICATION_OCTET_STREAM. Jersey streams the data to the client. We need to have client side models that can provide an input stream to consume this.

Suggest a Fix

We can probably return the InputStream from OkHttp if the PRODUCES is APPLICATION_OCTET_STREAM

InputStream is = response.body().byteStream();

wing328 commented 7 years ago

@amrk7 thanks for the suggestion. Do you mind submitting a PR so that we can review the change more easily?

If you need help on which file to start, please let us know.

amrk7 commented 7 years ago

Yes that will be helpful. Which module should I start?

On Feb 26, 2017 3:04 PM, "wing328" notifications@github.com wrote:

@amrk7 https://github.com/amrk7 thanks for the suggestion. Do you mind submitting a PR so that we can review the change more easily?

If you need help on which file to start, please let us know.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/swagger-api/swagger-codegen/issues/4831#issuecomment-282544063, or mute the thread https://github.com/notifications/unsubscribe-auth/AWdlU3i6IAIMSXwP9o4RwpulMeuL-W-dks5rgUcrgaJpZM4MINih .