Skip to content
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

[Question] Generate Swift client code without AnyCodable #16699

Open
nathanchia opened this issue Oct 1, 2023 · 3 comments
Open

[Question] Generate Swift client code without AnyCodable #16699

nathanchia opened this issue Oct 1, 2023 · 3 comments
Labels
Announcement Project/release related announcements

Comments

@nathanchia
Copy link

nathanchia commented Oct 1, 2023

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

@nathanchia nathanchia added the Announcement Project/release related announcements label Oct 1, 2023
@wing328
Copy link
Member

wing328 commented Oct 2, 2023

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
Copy link
Contributor

4brunu commented Oct 5, 2023

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
Copy link
Contributor

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Announcement Project/release related announcements
Projects
None yet
Development

No branches or pull requests

4 participants