Skip to content

Commit 01c12f5

Browse files
authored
[Bug] Handle the '\r\n' newline correctly (#169)
[Bug] Handle the '\r\n' newline correctly ### Motivation Fixes #143, which tracks including the '\r\n' newline in a description in the OpenAPI document. ### Modifications Replaces the '\r\n' character with '\n' before rendering comments, as '\n' is handled properly. ### Result OpenAPI documents that include '\r\n' in a description produce compiling code now. ### Test Plan Added a unit test for this case. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #169
1 parent b3c2b09 commit 01c12f5

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ struct TextBasedRenderer: RendererProtocol {
5454
prefix = "// MARK:"
5555
commentString = string
5656
}
57-
return commentString.transformingLines { line in
58-
if line.isEmpty {
59-
return prefix
57+
return
58+
commentString
59+
.transformingLines { line in
60+
if line.isEmpty {
61+
return prefix
62+
}
63+
return "\(prefix) \(line)"
6064
}
61-
return "\(prefix) \(line)"
62-
}
6365
}
6466

6567
/// Renders the specified import statements.
@@ -721,7 +723,7 @@ fileprivate extension String {
721723
/// Returns an array of strings, where each string represents one line
722724
/// in the current string.
723725
func asLines() -> [String] {
724-
split(separator: "\n", omittingEmptySubsequences: false)
726+
split(omittingEmptySubsequences: false, whereSeparator: \.isNewline)
725727
.map(String.init)
726728
}
727729

Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ final class Test_TextBasedRenderer: XCTestCase {
6767
// MARK: - Lorem ipsum
6868
"""#
6969
)
70+
try _test(
71+
.inline(
72+
"""
73+
Generated by foo\r\nAlso, bar
74+
"""
75+
),
76+
renderedBy: renderer.renderedComment,
77+
rendersAs:
78+
#"""
79+
// Generated by foo
80+
// Also, bar
81+
"""#
82+
)
7083
}
7184

7285
func testImports() throws {

Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ paths:
1616
description: "Even more information about working with pets"
1717
get:
1818
summary: List all pets
19-
description: "You can fetch all the pets here"
19+
description: "You can fetch\r\nall the pets here"
2020
operationId: listPets
2121
tags:
2222
- pets

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public struct Client: APIProtocol {
3535
private var converter: Converter { client.converter }
3636
/// List all pets
3737
///
38-
/// You can fetch all the pets here
38+
/// You can fetch
39+
/// all the pets here
3940
///
4041
/// - Remark: HTTP `GET /pets`.
4142
/// - Remark: Generated from `#/paths//pets/get(listPets)`.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ extension APIProtocol {
6060
fileprivate extension UniversalServer where APIHandler: APIProtocol {
6161
/// List all pets
6262
///
63-
/// You can fetch all the pets here
63+
/// You can fetch
64+
/// all the pets here
6465
///
6566
/// - Remark: HTTP `GET /pets`.
6667
/// - Remark: Generated from `#/paths//pets/get(listPets)`.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Foundation
99
public protocol APIProtocol: Sendable {
1010
/// List all pets
1111
///
12-
/// You can fetch all the pets here
12+
/// You can fetch
13+
/// all the pets here
1314
///
1415
/// - Remark: HTTP `GET /pets`.
1516
/// - Remark: Generated from `#/paths//pets/get(listPets)`.
@@ -704,7 +705,8 @@ public enum Components {
704705
public enum Operations {
705706
/// List all pets
706707
///
707-
/// You can fetch all the pets here
708+
/// You can fetch
709+
/// all the pets here
708710
///
709711
/// - Remark: HTTP `GET /pets`.
710712
/// - Remark: Generated from `#/paths//pets/get(listPets)`.

0 commit comments

Comments
 (0)