devexperts / swagger-codegen-ts

Typesafe Swagger API generator for TypeScript
Mozilla Public License 2.0
80 stars 16 forks source link

Feature/arbitrary file downloads for oas 3 #144

Closed Geksanit closed 2 years ago

Geksanit commented 3 years ago

part of https://github.com/devexperts/swagger-codegen-ts/issues/73

dxCLA commented 3 years ago

CLA assistant check
All committers have signed the CLA.

kokovtsev commented 3 years ago

@Geksanit looks like this change does not 100% cover our need; for example, I tried generating the code using the spec below

  /file:
    get:
      tags:
        - files
      operationId: getFile
      responses:
        200:
          description: succesfull operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SomeJsonObject'
            text/csv:
              schema:
                type: string

The generated code adds the Accept: application/json header to the request and uses the SomeJsonObject to decode the response. That is, it is still impossible to download a CSV file.

Let's discuss what interface should be generated for the spec above. As I see it, the consumer should be able to choose the desired response type. Looks like there are two ways:

  1. Generate two separate methods:
    readonly getFileAsApplicationJson: () => Kind2<F, Error, SomeJsonObject>;
    readonly getFileAsTextCsv: () => Kind2<F, Error, string>;
  2. Accept an additional parameter:
    readonly getFile: (params: {accept: 'application/json'}) => Kind2<F, Error, SomeJsonObject>;
    readonly getFile: (params: {accept: 'text/csv'}) => Kind2<F, Error, string>;

    What do you think?

Geksanit commented 2 years ago

@kokovtsev see how the code is generated now:

type MapToResponsegetFile = { 'application/json': SomeJsonObject; 'text/csv': string };
...
readonly getFile: <A extends keyof MapToResponsegetFile = 'application/json'>(parameters?: {
accept?: A;
}) => HKT<F, MapToResponsegetFile[A]>;
kokovtsev commented 2 years ago

Thanks @Geksanit, looks great except for some minor things! I'm adding some comments in the code