-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
[Swift] Convert default value of enum with not string type to string #4481
Conversation
@@ -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"); |
There was a problem hiding this comment.
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.
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
}
}
@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. |
…rator into swift4-enum-default-value
Sync'd master and updates samples. Tests passed via https://app.bitrise.io/build/8d5397847f3ed3b8 (bitrise.io CI) |
@wing328 |
@tasuwo thanks for the PR, which has been included in the v4.2.2 release: https://twitter.com/oas_generator/status/1201432648544972800 |
Fixes #4480
PR checklist
./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).master
,4.3.x
,5.0.x
. Default:master
.@jgavris @ehyche @Edubits @jaz-ah @d-date