Skip to content

Commit 2b00744

Browse files
authored
Escape negative numbers in integer-backed enums (#274)
1 parent ca26dca commit 2b00744

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateStringEnum.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extension FileTranslator {
6161
guard let rawValue = anyValue as? Int else {
6262
throw GenericError(message: "Disallowed value for an integer enum '\(typeName)': \(anyValue)")
6363
}
64-
let caseName = "_\(rawValue)"
64+
let caseName = rawValue < 0 ? "_n\(abs(rawValue))" : "_\(rawValue)"
6565
return (caseName, .int(rawValue))
6666
}
6767
}

Tests/OpenAPIGeneratorCoreTests/Translator/CommonTranslations/Test_translateStringEnum.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ final class Test_translateStringEnum: Test_Core {
3838
XCTAssertEqual(names, ["a", "_empty"])
3939
}
4040

41+
func testCaseValuesForIntegerSchema() throws {
42+
let names = try _caseValues(
43+
.integer(
44+
allowedValues: -1,
45+
1
46+
)
47+
)
48+
XCTAssertEqual(names, ["_n1", "_1"])
49+
}
50+
4151
func _caseValues(_ schema: JSONSchema) throws -> [String] {
4252
self.continueAfterFailure = false
4353
let translator = makeTypesTranslator()

0 commit comments

Comments
 (0)