yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
625 stars 146 forks source link

Make UploadFile type codable. #289

Open 0x0c opened 2 years ago

0x0c commented 2 years ago

Motivation

I fixed UploadFile type to confirm Codable to upload binary data either a reference of model or binary data as string a.k.a a primitive type. This changes allows to upload following model that could not be uploaded because of UploadFile can't be encode or decode when UploadFile is contained in user defined models.

I uploaded a minimal, reproducible example here.

paths:
  /withModel:
    post:
      summary: ''
      operationId: post-withModel
      responses:
        '200':
          description: OK
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                uploadData:
                  $ref: '#/components/schemas/UploadModel'
    parameters: []
...
components:
  schemas:
    UploadModel:
      title: UploadModel
      type: object
      properties:
        binary:
          type: string
          format: binary

This model is converted to this code and swift complier could not build as following error.

Screen Shot 2021-11-27 at 16 53 05

Trade-off

When generating specs with --option typeAliases.File:UploadFile, TestSpec could not be build because of the error below.

SwagGen/Specs/TestSpec/generated/Swift/Sources/Requests/GetFile.swift:52:49: error: cannot convert value of type 'Data' to expected argument type 'File' (aka 'UploadFile')
                case 200: self = try .status200(data)
                                                ^

To overcome this issue, we have to define public typealias File that responds to request body or response.

If there any concerns, please let me know freely :)

0x0c commented 2 years ago

@yonaskolb Hi, is it possible to review this PR?