Skip to content

Commit b00422f

Browse files
committed
Generate a convenience static property to create a responses without any parameters
1 parent ecdaa49 commit b00422f

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseOutcome.swift

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ extension TypesFileTranslator {
2323
/// - operation: The OpenAPI operation.
2424
/// - operationJSONPath: The JSON path to the operation in the OpenAPI
2525
/// document.
26-
/// - Returns: A declaration of the enum case and a declaration of the
26+
/// - Returns: A tuple containing a declaration of the enum case, a declaration of the
2727
/// structure unique to the response that contains the response headers
28-
/// and a body payload.
28+
/// and a body payload, a declaration of a throwing getter and, an optional convenience static property.
2929
/// - Throws: An error if there's an issue generating the declarations, such
3030
/// as unsupported response types or invalid definitions.
3131
func translateResponseOutcomeInTypes(
3232
_ outcome: OpenAPI.Operation.ResponseOutcome,
3333
operation: OperationDescription,
3434
operationJSONPath: String
35-
) throws -> (payloadStruct: Declaration?, enumCase: Declaration, throwingGetter: Declaration) {
36-
35+
) throws -> (
36+
payloadStruct: Declaration?, enumCase: Declaration, staticMember: Declaration?, throwingGetter: Declaration
37+
) {
3738
let typedResponse = try typedResponse(from: outcome, operation: operation)
3839
let responseStructTypeName = typedResponse.typeUsage.typeName
3940
let responseKind = outcome.status.value.asKind
@@ -55,14 +56,29 @@ extension TypesFileTranslator {
5556
}
5657
associatedValues.append(.init(type: .init(responseStructTypeName)))
5758

58-
let enumCaseDesc = EnumCaseDescription(name: enumCaseName, kind: .nameWithAssociatedValues(associatedValues))
59-
let enumCaseDecl: Declaration = .commentable(
60-
responseKind.docComment(
61-
userDescription: typedResponse.response.description,
62-
jsonPath: operationJSONPath + "/responses/" + responseKind.jsonPathComponent
63-
),
64-
.enumCase(enumCaseDesc)
59+
let enumCaseDocComment = responseKind.docComment(
60+
userDescription: typedResponse.response.description,
61+
jsonPath: operationJSONPath + "/responses/" + responseKind.jsonPathComponent
6562
)
63+
let enumCaseDesc = EnumCaseDescription(name: enumCaseName, kind: .nameWithAssociatedValues(associatedValues))
64+
let enumCaseDecl: Declaration = .commentable(enumCaseDocComment, .enumCase(enumCaseDesc))
65+
66+
var staticMemberDecl: Declaration?
67+
let responseHasNoHeaders = typedResponse.response.headers?.isEmpty ?? true
68+
let responseHasNoContent = typedResponse.response.content.isEmpty
69+
if responseHasNoContent && responseHasNoHeaders && !responseKind.wantsStatusCode {
70+
let staticMemberDesc = VariableDescription(
71+
accessModifier: .internal,
72+
isStatic: true,
73+
kind: .var,
74+
left: .identifier(.pattern(enumCaseName)),
75+
type: .member(["Self"]),
76+
right: .closureInvocation(body: [
77+
.expression(.functionCall(calledExpression: .dot(enumCaseName), arguments: [.dot("init")]))
78+
])
79+
)
80+
staticMemberDecl = .commentable(enumCaseDocComment, .variable(staticMemberDesc))
81+
}
6682

6783
let throwingGetterDesc = VariableDescription(
6884
accessModifier: config.access,
@@ -113,7 +129,7 @@ extension TypesFileTranslator {
113129
)
114130
let throwingGetterDecl = Declaration.commentable(throwingGetterComment, .variable(throwingGetterDesc))
115131

116-
return (responseStructDecl, enumCaseDecl, throwingGetterDecl)
132+
return (responseStructDecl, enumCaseDecl, staticMemberDecl, throwingGetterDecl)
117133
}
118134
}
119135

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateOperations.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ extension TypesFileTranslator {
143143
let documentedMembers: [Declaration] = documentedOutcomes.flatMap {
144144
inlineResponseDecl,
145145
caseDecl,
146-
throwingGetter in [inlineResponseDecl, caseDecl, throwingGetter].compactMap { $0 }
146+
staticDecl,
147+
throwingGetter in [inlineResponseDecl, caseDecl, staticDecl, throwingGetter].compactMap { $0 }
147148
}
148149

149150
let allMembers: [Declaration]

0 commit comments

Comments
 (0)