Skip to content

Commit df52530

Browse files
Support empty string OpenAPI identifiers (#59)
### Motivation The OpenAPI specification allows the use of the empty string in some places, for example in the following enum, which we've seen in some real-world OpenAPI documents: ```yaml type: string enum: - "" - foo - bar ``` We have code that converts an OpenAPI identifier to a suitable string that can be used as a Swift identifier. It currently returns an empty string when given an empty string as input, which is not a valid Swift identifier. We already have some logic there to avoid clashing with some Swift keywords, so this can be extended to handle the empty string. ### Modifications - Map the empty string OpenAPI identifier to "_empty" ### Result We can generate compiling code when presented with empty string OpenAPI identifiers. ### Test Plan - Added unit test. - Extended reference test. ### Resolves - Resolves #33. - Fixes #61. --------- Signed-off-by: Si Beaumont <[email protected]>
1 parent 6aae3f5 commit df52530

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

Sources/_OpenAPIGeneratorCore/Extensions/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fileprivate extension String {
6363
/// ensures that the identifier starts with a letter and not a number.
6464
var sanitizedForSwiftCode: String {
6565
guard !isEmpty else {
66-
return self
66+
return "_empty"
6767
}
6868

6969
// Only allow [a-zA-Z][a-zA-Z0-9_]*

Tests/OpenAPIGeneratorCoreTests/Extensions/Test_String.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ final class Test_String: Test_Core {
2929

3030
// Reserved name
3131
("Type", "_Type"),
32+
33+
// Empty string
34+
("", "_empty"),
3235
]
3336
for (input, sanitized) in cases {
3437
XCTAssertEqual(input.asSwiftSafeName, sanitized)

Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ paths:
3535
- water
3636
- land
3737
- air
38+
- ""
3839
type: string
3940
- name: feeds
4041
in: query

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,13 +691,15 @@ public enum Operations {
691691
case water
692692
case land
693693
case air
694+
case _empty
694695
/// Parsed a raw value that was not defined in the OpenAPI document.
695696
case undocumented(String)
696697
public init?(rawValue: String) {
697698
switch rawValue {
698699
case "water": self = .water
699700
case "land": self = .land
700701
case "air": self = .air
702+
case "": self = ._empty
701703
default: self = .undocumented(rawValue)
702704
}
703705
}
@@ -707,9 +709,10 @@ public enum Operations {
707709
case .water: return "water"
708710
case .land: return "land"
709711
case .air: return "air"
712+
case ._empty: return ""
710713
}
711714
}
712-
public static var allCases: [habitatPayload] { [.water, .land, .air] }
715+
public static var allCases: [habitatPayload] { [.water, .land, .air, ._empty] }
713716
}
714717
public var habitat: Operations.listPets.Input.Query.habitatPayload?
715718
/// - Remark: Generated from `#/paths/pets/GET/query/feedsPayload`.

0 commit comments

Comments
 (0)