From 2a981cf1f14960f4a0acbb761e493af84ee27965 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Thu, 3 Aug 2023 22:32:56 +0200 Subject: [PATCH 1/4] [Bug] Handle the '\r' character correctly --- .../Renderer/TextBasedRenderer.swift | 13 ++++++++----- .../Resources/Docs/petstore.yaml | 2 +- .../ReferenceSources/Petstore/Client.swift | 4 +++- .../ReferenceSources/Petstore/Server.swift | 4 +++- .../Resources/ReferenceSources/Petstore/Types.swift | 8 ++++++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift index e0da301c..58a63349 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift @@ -54,12 +54,15 @@ struct TextBasedRenderer: RendererProtocol { prefix = "// MARK:" commentString = string } - return commentString.transformingLines { line in - if line.isEmpty { - return prefix + return + commentString + .replacingOccurrences(of: "\r", with: "\n") + .transformingLines { line in + if line.isEmpty { + return prefix + } + return "\(prefix) \(line)" } - return "\(prefix) \(line)" - } } /// Renders the specified import statements. diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml b/Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml index b8dce4fe..a367d7f5 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml @@ -16,7 +16,7 @@ paths: description: "Even more information about working with pets" get: summary: List all pets - description: "You can fetch all the pets here" + description: "You can fetch\r\nall the pets here" operationId: listPets tags: - pets diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift index 0c9e2b2c..505f8490 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift @@ -35,7 +35,9 @@ public struct Client: APIProtocol { private var converter: Converter { client.converter } /// List all pets /// - /// You can fetch all the pets here + /// You can fetch + /// + /// all the pets here /// /// - Remark: HTTP `GET /pets`. /// - Remark: Generated from `#/paths//pets/get(listPets)`. diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift index 628e9805..c00ab15a 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift @@ -60,7 +60,9 @@ extension APIProtocol { fileprivate extension UniversalServer where APIHandler: APIProtocol { /// List all pets /// - /// You can fetch all the pets here + /// You can fetch + /// + /// all the pets here /// /// - Remark: HTTP `GET /pets`. /// - Remark: Generated from `#/paths//pets/get(listPets)`. diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift index 2db92dc5..ebaa56c6 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift @@ -9,7 +9,9 @@ import Foundation public protocol APIProtocol: Sendable { /// List all pets /// - /// You can fetch all the pets here + /// You can fetch + /// + /// all the pets here /// /// - Remark: HTTP `GET /pets`. /// - Remark: Generated from `#/paths//pets/get(listPets)`. @@ -704,7 +706,9 @@ public enum Components { public enum Operations { /// List all pets /// - /// You can fetch all the pets here + /// You can fetch + /// + /// all the pets here /// /// - Remark: HTTP `GET /pets`. /// - Remark: Generated from `#/paths//pets/get(listPets)`. From 41d8659ead0bbcc91e8dcd7f098ecd3bcea9141c Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Thu, 3 Aug 2023 22:36:53 +0200 Subject: [PATCH 2/4] Added a unit test --- .../Renderer/Test_TextBasedRenderer.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift b/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift index f1b30e63..9d7ea5a7 100644 --- a/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift +++ b/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift @@ -67,6 +67,20 @@ final class Test_TextBasedRenderer: XCTestCase { // MARK: - Lorem ipsum """# ) + try _test( + .inline( + """ + Generated by foo\r\nAlso, bar + """ + ), + renderedBy: renderer.renderedComment, + rendersAs: + #""" + // Generated by foo + // + // Also, bar + """# + ) } func testImports() throws { From e2e7f3b2eaf42004e3e749bbcd046fa7d07b0cdc Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Fri, 4 Aug 2023 08:50:26 +0200 Subject: [PATCH 3/4] PR feedback: Treat \r\n as a single return, not two --- .../_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift | 4 ++-- .../Renderer/Test_TextBasedRenderer.swift | 1 - .../Resources/ReferenceSources/Petstore/Client.swift | 1 - .../Resources/ReferenceSources/Petstore/Server.swift | 1 - .../Resources/ReferenceSources/Petstore/Types.swift | 2 -- 5 files changed, 2 insertions(+), 7 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift index 58a63349..0b1ac92b 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift @@ -56,7 +56,6 @@ struct TextBasedRenderer: RendererProtocol { } return commentString - .replacingOccurrences(of: "\r", with: "\n") .transformingLines { line in if line.isEmpty { return prefix @@ -724,7 +723,8 @@ fileprivate extension String { /// Returns an array of strings, where each string represents one line /// in the current string. func asLines() -> [String] { - split(separator: "\n", omittingEmptySubsequences: false) + replacingOccurrences(of: "\r\n", with: "\n") + .split(separator: "\n", omittingEmptySubsequences: false) .map(String.init) } diff --git a/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift b/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift index 9d7ea5a7..35cada04 100644 --- a/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift +++ b/Tests/OpenAPIGeneratorCoreTests/Renderer/Test_TextBasedRenderer.swift @@ -77,7 +77,6 @@ final class Test_TextBasedRenderer: XCTestCase { rendersAs: #""" // Generated by foo - // // Also, bar """# ) diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift index 505f8490..60d3b3c4 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift @@ -36,7 +36,6 @@ public struct Client: APIProtocol { /// List all pets /// /// You can fetch - /// /// all the pets here /// /// - Remark: HTTP `GET /pets`. diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift index c00ab15a..fc3dc9a7 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift @@ -61,7 +61,6 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol { /// List all pets /// /// You can fetch - /// /// all the pets here /// /// - Remark: HTTP `GET /pets`. diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift index ebaa56c6..e8cadeb1 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift @@ -10,7 +10,6 @@ public protocol APIProtocol: Sendable { /// List all pets /// /// You can fetch - /// /// all the pets here /// /// - Remark: HTTP `GET /pets`. @@ -707,7 +706,6 @@ public enum Operations { /// List all pets /// /// You can fetch - /// /// all the pets here /// /// - Remark: HTTP `GET /pets`. From e74848b7906706a3d414eb3f84bdae577a8f4685 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Fri, 4 Aug 2023 09:03:03 +0200 Subject: [PATCH 4/4] PR feedback: use split + \.isNewline --- Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift index 0b1ac92b..3fe4699e 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift @@ -723,8 +723,7 @@ fileprivate extension String { /// Returns an array of strings, where each string represents one line /// in the current string. func asLines() -> [String] { - replacingOccurrences(of: "\r\n", with: "\n") - .split(separator: "\n", omittingEmptySubsequences: false) + split(omittingEmptySubsequences: false, whereSeparator: \.isNewline) .map(String.init) }