Skip to content

[REQ] [Javascript] Support multiple media types in responses #8119

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

Open
phillipuniverse opened this issue Dec 8, 2020 · 0 comments
Open

Comments

@phillipuniverse
Copy link
Contributor

phillipuniverse commented Dec 8, 2020

Is your feature request related to a problem? Please describe.

This is a very similar issue as #440 but for Javascript. Here is an example endpoint with multiple representations that could be returned (csv or json):

  /endpoint-returning-csv/:
    get:
      responses:
        '200':
          content:
            text/csv:
              schema:
                format: binary
                type: string
          description: Retrieve CSV data
        default:
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
            description: Default error response
      summary: Retrieve CSV export
      tags:
      - default

This generates the following API:

endpointReturningCsvGet(opts, callback) {
      opts = opts || {};
      let postBody = null;

      let pathParams = {
      };
      let queryParams = {
      };
      let headerParams = {
      };
      let formParams = {
      };

      let authNames = ['AuthToken'];
      let contentTypes = [];
      let accepts = ['text/csv', 'application/json'];
      let returnType = File;
      return this.apiClient.callApi(
        '/endpoint-returning-csv/', 'GET',
        pathParams, queryParams, headerParams, formParams, postBody,
        authNames, contentTypes, accepts, returnType, null, callback
      );
    }

This all looks correct. But, the generated apiClient.callApi only ever returns a single mime type, and always application/json if it is included:

{{#emitJSDoc}}/**
* Chooses a content type from the given array, with JSON preferred; i.e. return JSON if included, otherwise return the first.
* @param {Array.<String>} contentTypes
* @returns {String} The chosen content type, preferring JSON.
*/{{/emitJSDoc}}
jsonPreferredMime(contentTypes) {
for (var i = 0; i < contentTypes.length; i++) {
if (this.isJsonMime(contentTypes[i])) {
return contentTypes[i];
}
}
return contentTypes[0];
}

Describe the solution you'd like

I would like to be able to describe multiple produces parameters with different content types as documented in OpenAPI 3 and all of those be passed to the server. It would be nice to have full deserialization support for all of the different content types like in the Python PR, but at a minimum all content types should be sent in the Accept header.

Describe alternatives you've considered

A workaround for me is to remove the secondary JSON response altogether, leaving only the CSV response. Since I was coming from Swagger 2 codegen anyway that only allowed a single media type, this solution worked for me as a stop-gap.

Additional context

None!

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

No branches or pull requests

2 participants