OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.33k stars 6.45k forks source link

[Question] Generate Swift client code without AnyCodable #16699

Open nathanchia opened 11 months ago

nathanchia commented 11 months ago

Hello, I'm new to using openapi-generator. Like the title says, I ran the command below to generate a Swift Client:

openapi-generator generate -i openapi.yaml -g swift5 -o Generated/ 

All the models generated only contain type AnyCodable? even though I explicitly defined them in my yaml file. Is there a way to configure this so that only specified types are used? I used version 3.0.3 before this and it worked fine. However, now I need to implement an interceptor for refresh token logic using taskCompletionShouldRetry.

Generated Model:

public struct ErrorResponseBody: Codable, JSONEncodable, Hashable {

    public var errorCode: AnyCodable?  // <-- TODO: Want errorCode and detail to both be type String. 
    public var detail: AnyCodable?

    public init(errorCode: AnyCodable?, detail: AnyCodable? = nil) {
        self.errorCode = errorCode
        self.detail = detail
    }

    public enum CodingKeys: String, CodingKey, CaseIterable {
        case errorCode = "error_code"
        case detail
    }

    // Encodable protocol methods

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(errorCode, forKey: .errorCode)
        try container.encodeIfPresent(detail, forKey: .detail)
    }
}

Yaml file:

openapi: '3.1.0'
info:
  title: TestAPI
  version: 0.1.0
paths:
  "/session":
    post:
      tags:
      - chat
      summary: Start Session
      description: Start a new chat session. A new session id is returned.
      operationId: start_session
      parameters:
      - name: request-id
        in: header
        required: true
        schema:
          type: string
          title: Request-Id
      - name: access_token
        in: header
        required: true
        schema:
          type: string
          title: Access Token
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ChatResponseBody-Output"
        '400':
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponseBody"
          description: Bad Request
        '401':
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponseBody"
          description: Unauthorized
        '404':
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponseBody"
          description: Not Found
        '422':
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponseBody"
          description: Unprocessable Entity
        '500':
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponseBody"
          description: Internal Server Error
        '503':
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponseBody"
          description: Service Unavailable
        '504':
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponseBody"
          description: Gateway Timeout
components:
  schemas:
    ChatResponseBody-Output:
      properties:
        role:
          "$ref": "#/components/schemas/ChatRole"
        user_id:
          type: string
          title: User Id
        message_id:
          type: string
          title: Message Id
        session_id:
          type: string
          title: Session Id
        message:
          type: string
          title: Message
        timestamp:
          type: string
          title: Timestamp
        payload:
          type: object
          title: Payload
      type: object
      required:
      - role
      - user_id
      - message_id
      - session_id
      - message
      - timestamp
      title: ChatResponseBody
    ErrorResponseBody:
      properties:
        error_code:
          type: string
          title: Error Code
        detail:
          type: string
          title: Detail
      type: object
      required:
      - error_code
      title: ErrorResponseBody
    ChatRole:
      type: string
      enum:
      - assistant
      - user
      title: ChatRole

Thank you.

cc: @4brunu

wing328 commented 11 months ago
public var errorCode: AnyCodable?  // <-- TODO: Want errorCode and detail to both be type String. 

what about using customized templates (e.g. via -t option in CLI) ?

4brunu commented 11 months ago

Hi, sorry for the late response. I tested it locally and I can reproduce the issue. I have no idea why, but I think it may be an issue with your spec file? Sorry but I don't currently have much time to look into this.

barcister commented 10 months ago

@nathanchia I've had to make some changes to your API definition and it seems to be working just fine: link

here's the gist

let me know if you need any more help