OpenAPITools / OpenAPITools/openapi-generator

[BUG] [SWIFT] OneOf Code Gen

Open
#3,624 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Hi All

I have a project where the YML has alot of oneOf's in it (see example below)
The swift generated code creates a callback type of 'oneofarrayarrayarray' but doesnt define the type anywhere

Is this a bug or a limitation of the swift code gen

$ openapi-generator version
4.1.0

One example of the YML

'/areas/{id}':
    get:
      tags:
        - areas
      summary: "Retrieve an Area by ID"
      operationId: getAreasById
      parameters:
        - $ref: '#/components/parameters/id'
        - $ref: '#/components/parameters/areaName'
        - $ref: '#/components/parameters/modelType'
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                oneOf:
                - type: array
                  items:
                    $ref: '#/components/schemas/areaLite'
                - type: array
                  items:
                    $ref: '#/components/schemas/areaBasic'
                - type: array
                  items:
                    $ref: '#/components/schemas/areaFull'
              example:
                 - areaLite:
                      $ref: '#/components/schemas/areaLite'
                 - areaBasic:
                      $ref: '#/components/schemas/areaBasic'
                 - areaFull: 
                      $ref: '#/components/schemas/areaFull'

Code Gen Output

    open class func getAreasWithRequestBuilder(propertyId: Int, limit: Int? = nil, modelType: ModelType_getAreas? = nil, offset: Int? = nil, modifiedSince: Date? = nil, sort: String? = nil) -> RequestBuilder<OneOfarrayarrayarray> {
        let path = "/areas"
        let URLString = OpenAPIClientAPI.basePath + path
        let parameters: [String:Any]? = nil
        
        var url = URLComponents(string: URLString)
        url?.queryItems = APIHelper.mapValuesToQueryItems([
            "limit": limit?.encodeToJSON(), 
            "modelType": modelType?.rawValue, 
            "offset": offset?.encodeToJSON(), 
            "propertyId": propertyId.encodeToJSON(), 
            "modifiedSince": modifiedSince?.encodeToJSON(), 
            "sort": sort
        ])

        let requestBuilder: RequestBuilder<OneOfarrayarrayarray>.Type = OpenAPIClientAPI.requestBuilderFactory.getBuilder()

        return requestBuilder.init(method: "GET", URLString: (url?.string ?? URLString), parameters: parameters, isBody: false)
    }

My workaround (will get annoying every time they update their API)

    public enum responseTest : Codable {
        case basic(PropertiesBasic)
        case lite(PropertiesLite)
        case full(PropertiesFull)
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied oneOf schema using openapi-generator version 4.1.0, then inspect the Swift generator output around RequestBuilder. Done means the generated Swift output defines a usable response type instead of referencing an undefined type, or clearly establishes that the schema is unsupported.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.