Skip to content

Commit 3eacb9c

Browse files
Add deprecated annotations on shorthand functions for deprecated operations (#384)
### Motivation If an operation is marked as deprecated in the OpenAPI document then the generated protocol requirement is annotated as deprecated. However, the shorthand overload for this function, in the protocol extension, was missing this annotation. ### Modifications Add deprecated annotation to the generated function in the protocol extension for deprecated API operations. ### Result If an operation is marked as deprecated in the OpenAPI document then both variants of the generated function (the protocol requirement _and_ the protocol extension) are annotated as deprecated. ### Test Plan Updated the snippet test to include a path with one deprecated operation and one non-deprecated operation. ### Related Issues - Fixes #367.
1 parent 577bafd commit 3eacb9c

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateAPIProtocol.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ extension TypesFileTranslator {
7878
)
7979
]
8080
)
81-
return .commentable(operation.comment, .function(function))
81+
if operation.operation.deprecated {
82+
return .commentable(operation.comment, .deprecated(.init(), .function(function)))
83+
} else {
84+
return .commentable(operation.comment, .function(function))
85+
}
8286
}
8387
}
8488

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,46 +1868,50 @@ final class SnippetBasedReferenceTests: XCTestCase {
18681868
)
18691869
}
18701870

1871-
func testPathsSimplestCase() throws {
1872-
try self.assertPathsTranslation(
1873-
"""
1874-
/health:
1871+
func testPaths() throws {
1872+
let paths = """
1873+
/healthOld:
18751874
get:
1876-
operationId: getHealth
1875+
operationId: getHealthOld
1876+
deprecated: true
18771877
responses:
18781878
'200':
18791879
description: A success response with a greeting.
18801880
content:
18811881
text/plain:
18821882
schema:
18831883
type: string
1884-
""",
1885-
"""
1886-
public protocol APIProtocol: Sendable {
1887-
func getHealth(_ input: Operations.getHealth.Input) async throws -> Operations.getHealth.Output
1888-
}
1889-
"""
1890-
)
1891-
}
1892-
1893-
func testPathsSimplestCaseExtension() throws {
1894-
try self.assertPathsTranslationExtension(
1895-
"""
1896-
/health:
1884+
/healthNew:
18971885
get:
1898-
operationId: getHealth
1886+
operationId: getHealthNew
18991887
responses:
19001888
'200':
19011889
description: A success response with a greeting.
19021890
content:
19031891
text/plain:
19041892
schema:
19051893
type: string
1906-
""",
1894+
"""
1895+
try self.assertPathsTranslation(
1896+
paths,
1897+
"""
1898+
public protocol APIProtocol: Sendable {
1899+
@available(*, deprecated)
1900+
func getHealthOld(_ input: Operations.getHealthOld.Input) async throws -> Operations.getHealthOld.Output
1901+
func getHealthNew(_ input: Operations.getHealthNew.Input) async throws -> Operations.getHealthNew.Output
1902+
}
1903+
"""
1904+
)
1905+
try self.assertPathsTranslationExtension(
1906+
paths,
19071907
"""
19081908
extension APIProtocol {
1909-
public func getHealth(headers: Operations.getHealth.Input.Headers = .init()) async throws -> Operations.getHealth.Output {
1910-
try await getHealth(Operations.getHealth.Input(headers: headers))
1909+
@available(*, deprecated)
1910+
public func getHealthOld(headers: Operations.getHealthOld.Input.Headers = .init()) async throws -> Operations.getHealthOld.Output {
1911+
try await getHealthOld(Operations.getHealthOld.Input(headers: headers))
1912+
}
1913+
public func getHealthNew(headers: Operations.getHealthNew.Input.Headers = .init()) async throws -> Operations.getHealthNew.Output {
1914+
try await getHealthNew(Operations.getHealthNew.Input(headers: headers))
19111915
}
19121916
}
19131917
"""

0 commit comments

Comments
 (0)