[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#4481wing328 merged 5 commits intoOpenAPITools:masterfrom
Conversation
| 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.
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. |
|
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.shif 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