@@ -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,36 @@ 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+ 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+ }
6689
6790 let throwingGetterDesc = VariableDescription (
6891 accessModifier: config. access,
@@ -113,7 +136,7 @@ extension TypesFileTranslator {
113136 )
114137 let throwingGetterDecl = Declaration . commentable ( throwingGetterComment, . variable( throwingGetterDesc) )
115138
116- return ( responseStructDecl, enumCaseDecl, throwingGetterDecl)
139+ return ( responseStructDecl, enumCaseDecl, staticMemberDecl , throwingGetterDecl)
117140 }
118141}
119142
0 commit comments