@@ -23,17 +23,18 @@ extension TypesFileTranslator {
23
23
/// - operation: The OpenAPI operation.
24
24
/// - operationJSONPath: The JSON path to the operation in the OpenAPI
25
25
/// 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
27
27
/// 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 .
29
29
/// - Throws: An error if there's an issue generating the declarations, such
30
30
/// as unsupported response types or invalid definitions.
31
31
func translateResponseOutcomeInTypes(
32
32
_ outcome: OpenAPI . Operation . ResponseOutcome ,
33
33
operation: OperationDescription ,
34
34
operationJSONPath: String
35
- ) throws -> ( payloadStruct: Declaration ? , enumCase: Declaration , throwingGetter: Declaration ) {
36
-
35
+ ) throws -> (
36
+ payloadStruct: Declaration ? , enumCase: Declaration , staticMember: Declaration ? , throwingGetter: Declaration
37
+ ) {
37
38
let typedResponse = try typedResponse ( from: outcome, operation: operation)
38
39
let responseStructTypeName = typedResponse. typeUsage. typeName
39
40
let responseKind = outcome. status. value. asKind
@@ -55,14 +56,36 @@ extension TypesFileTranslator {
55
56
}
56
57
associatedValues. append ( . init( type: . init( responseStructTypeName) ) )
57
58
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
65
62
)
63
+ let enumCaseDesc = EnumCaseDescription ( name: enumCaseName, kind: . nameWithAssociatedValues( associatedValues) )
64
+ let enumCaseDecl : Declaration = . commentable( enumCaseDocComment, . enumCase( enumCaseDesc) )
65
+
66
+ let 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: config. access,
72
+ isStatic: true ,
73
+ kind: . var,
74
+ left: . identifier( . pattern( enumCaseName) ) ,
75
+ type: . member( [ " Self " ] ) ,
76
+ getter: [
77
+ . expression(
78
+ . functionCall(
79
+ calledExpression: . dot( enumCaseName) ,
80
+ arguments: [ . functionCall( calledExpression: . dot( " init " ) ) ]
81
+ )
82
+ )
83
+ ]
84
+ )
85
+ staticMemberDecl = . commentable( enumCaseDocComment, . variable( staticMemberDesc) )
86
+ } else {
87
+ staticMemberDecl = nil
88
+ }
66
89
67
90
let throwingGetterDesc = VariableDescription (
68
91
accessModifier: config. access,
@@ -113,7 +136,7 @@ extension TypesFileTranslator {
113
136
)
114
137
let throwingGetterDecl = Declaration . commentable ( throwingGetterComment, . variable( throwingGetterDesc) )
115
138
116
- return ( responseStructDecl, enumCaseDecl, throwingGetterDecl)
139
+ return ( responseStructDecl, enumCaseDecl, staticMemberDecl , throwingGetterDecl)
117
140
}
118
141
}
119
142
0 commit comments