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

[Swift] Convert default value of enum with not string type to string #4481

Merged
merged 5 commits into from
Nov 25, 2019

Conversation

tasuwo
Copy link
Contributor

@tasuwo tasuwo commented Nov 13, 2019

Fixes #4480

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.
    @jgavris @ehyche @Edubits @jaz-ah @d-date

@@ -51,8 +54,80 @@ public void converterTest() {
Assert.assertEquals(enumVar.dataType, "String");
Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
Assert.assertEquals(enumVar.name, "name");
Assert.assertEquals(enumVar.defaultValue, ".VALUE2");
Assert.assertEquals(enumVar.defaultValue, ".value2");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With enum, name of default value should be converted as enum var name.

In current master version generator, if you use following declaration...

openapi: 3.0.0
info:
  description: ''
  version: 1.0.0
  title: ''
paths:
  /items:
    post:
      description: ''
      operationId: addItem
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NewItem'
      responses:
        '405':
          description: Invalid input
components:
  schemas:
    NewItem:
      properties:
        size:
          type: string
          enum:
          - VALUE1
          - VALUE2
          - VALUE3
          default: VALUE2

... following code will be generated.

import Foundation

public struct NewItem: Codable {

    public enum Size: String, Codable {
        case value1 = "VALUE1"
        case value2 = "VALUE2"
        case value3 = "VALUE3"
    }
    public var size: Size? = .VALUE2

    public init(size: Size?) {
        self.size = size
    }

}

This code fails to compile.

This is because, enum var name is camelized at following.

https://github.com/OpenAPITools/openapi-generator/pull/4481/files#diff-01cf6e8770037e6a2a690476f2310d57R776-R781

So name of default value for enum should be converted as enum var name by toEnumVarName, even if the type is string.

After apply this PR's fix, code will be generated as follows.

import Foundation

public struct NewItem: Codable {

    public enum Size: String, Codable {
        case value1 = "VALUE1"
        case value2 = "VALUE2"
        case value3 = "VALUE3"
    }
    public var size: Size? = .value2

    public init(size: Size?) {
        self.size = size
    }

}

@d-date
Copy link

d-date commented Nov 15, 2019

@tasuwo Thanks to raise PR. Please run script under ./bin/petstore-swift4-all.sh and run build on each Xcode project and compile should be passed.

@wing328 wing328 modified the milestones: 4.2.1, 4.2.2 Nov 15, 2019
@wing328
Copy link
Member

wing328 commented Nov 25, 2019

Sync'd master and updates samples. Tests passed via https://app.bitrise.io/build/8d5397847f3ed3b8 (bitrise.io CI)

@wing328 wing328 merged commit eff94da into OpenAPITools:master Nov 25, 2019
@tasuwo
Copy link
Contributor Author

tasuwo commented Nov 25, 2019

@wing328
Thank you for updating samples and merging PR 🙇

@wing328
Copy link
Member

wing328 commented Dec 2, 2019

@tasuwo thanks for the PR, which has been included in the v4.2.2 release: https://twitter.com/oas_generator/status/1201432648544972800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] [Swift] fails to generate with integer type enum and default value
3 participants