@@ -24,6 +24,7 @@ extension TypesFileTranslator {
24
24
response: TypedResponse
25
25
) throws -> Declaration {
26
26
let response = response. response
27
+
27
28
let headersTypeName = typeName. appending (
28
29
swiftComponent: Constants . Operation. Output. Payload. Headers. typeName,
29
30
jsonComponent: " headers "
@@ -32,35 +33,40 @@ extension TypesFileTranslator {
32
33
from: response,
33
34
inParent: headersTypeName
34
35
)
35
- let headerProperties : [ PropertyBlueprint ] = try headers. map { header in
36
- try parseResponseHeaderAsProperty (
37
- for: header,
38
- parent: headersTypeName
36
+ let headersProperty : PropertyBlueprint ?
37
+ if !headers. isEmpty {
38
+ let headerProperties : [ PropertyBlueprint ] = try headers. map { header in
39
+ try parseResponseHeaderAsProperty (
40
+ for: header,
41
+ parent: headersTypeName
42
+ )
43
+ }
44
+ let headerStructComment : Comment ? =
45
+ headersTypeName
46
+ . docCommentWithUserDescription ( nil )
47
+ let headersStructBlueprint : StructBlueprint = . init(
48
+ comment: headerStructComment,
49
+ access: config. access,
50
+ typeName: headersTypeName,
51
+ conformances: Constants . Operation. Output. Payload. Headers. conformances,
52
+ properties: headerProperties
53
+ )
54
+ let headersStructDecl = translateStructBlueprint (
55
+ headersStructBlueprint
39
56
)
57
+ headersProperty = PropertyBlueprint (
58
+ comment: . doc( " Received HTTP response headers " ) ,
59
+ originalName: Constants . Operation. Output. Payload. Headers. variableName,
60
+ typeUsage: headersTypeName. asUsage,
61
+ default: headersStructBlueprint. hasEmptyInit ? . emptyInit : nil ,
62
+ associatedDeclarations: [
63
+ headersStructDecl
64
+ ] ,
65
+ asSwiftSafeName: swiftSafeName
66
+ )
67
+ } else {
68
+ headersProperty = nil
40
69
}
41
- let headerStructComment : Comment ? =
42
- headersTypeName
43
- . docCommentWithUserDescription ( nil )
44
- let headersStructBlueprint : StructBlueprint = . init(
45
- comment: headerStructComment,
46
- access: config. access,
47
- typeName: headersTypeName,
48
- conformances: Constants . Operation. Output. Payload. Headers. conformances,
49
- properties: headerProperties
50
- )
51
- let headersStructDecl = translateStructBlueprint (
52
- headersStructBlueprint
53
- )
54
- let headersProperty = PropertyBlueprint (
55
- comment: . doc( " Received HTTP response headers " ) ,
56
- originalName: Constants . Operation. Output. Payload. Headers. variableName,
57
- typeUsage: headersTypeName. asUsage,
58
- default: headersStructBlueprint. hasEmptyInit ? . emptyInit : nil ,
59
- associatedDeclarations: [
60
- headersStructDecl
61
- ] ,
62
- asSwiftSafeName: swiftSafeName
63
- )
64
70
65
71
let bodyTypeName = typeName. appending (
66
72
swiftComponent: Constants . Operation. Body. typeName,
@@ -70,54 +76,60 @@ extension TypesFileTranslator {
70
76
response. content,
71
77
inParent: bodyTypeName
72
78
)
73
- var bodyCases : [ Declaration ] = [ ]
74
- for typedContent in typedContents {
75
- let contentType = typedContent. content. contentType
76
- let identifier = contentSwiftName ( contentType)
77
- let associatedType = typedContent. resolvedTypeUsage
78
- if TypeMatcher . isInlinable ( typedContent. content. schema) , let inlineType = typedContent. typeUsage {
79
- let inlineTypeDecls = try translateSchema (
80
- typeName: inlineType. typeName,
81
- schema: typedContent. content. schema,
82
- overrides: . none
79
+
80
+ let contentProperty : PropertyBlueprint ?
81
+ if !typedContents. isEmpty {
82
+ var bodyCases : [ Declaration ] = [ ]
83
+ for typedContent in typedContents {
84
+ let contentType = typedContent. content. contentType
85
+ let identifier = contentSwiftName ( contentType)
86
+ let associatedType = typedContent. resolvedTypeUsage
87
+ if TypeMatcher . isInlinable ( typedContent. content. schema) , let inlineType = typedContent. typeUsage {
88
+ let inlineTypeDecls = try translateSchema (
89
+ typeName: inlineType. typeName,
90
+ schema: typedContent. content. schema,
91
+ overrides: . none
92
+ )
93
+ bodyCases. append ( contentsOf: inlineTypeDecls)
94
+ }
95
+
96
+ let bodyCase : Declaration = . commentable(
97
+ contentType. docComment ( typeName: bodyTypeName) ,
98
+ . enumCase(
99
+ name: identifier,
100
+ kind: . nameWithAssociatedValues( [
101
+ . init( type: associatedType. fullyQualifiedSwiftName)
102
+ ] )
103
+ )
83
104
)
84
- bodyCases. append ( contentsOf : inlineTypeDecls )
105
+ bodyCases. append ( bodyCase )
85
106
}
86
-
87
- let bodyCase : Declaration = . commentable(
88
- contentType. docComment ( typeName: bodyTypeName) ,
89
- . enumCase(
90
- name: identifier,
91
- kind: . nameWithAssociatedValues( [
92
- . init( type: associatedType. fullyQualifiedSwiftName)
93
- ] )
107
+ let hasNoContent : Bool = bodyCases. isEmpty
108
+ let contentEnumDecl : Declaration = . commentable(
109
+ bodyTypeName. docCommentWithUserDescription ( nil ) ,
110
+ . enum(
111
+ isFrozen: true ,
112
+ accessModifier: config. access,
113
+ name: bodyTypeName. shortSwiftName,
114
+ conformances: Constants . Operation. Body. conformances,
115
+ members: bodyCases
94
116
)
95
117
)
96
- bodyCases. append ( bodyCase)
97
- }
98
- let hasNoContent : Bool = bodyCases. isEmpty
99
- let contentEnumDecl : Declaration = . commentable(
100
- bodyTypeName. docCommentWithUserDescription ( nil ) ,
101
- . enum(
102
- isFrozen: true ,
103
- accessModifier: config. access,
104
- name: bodyTypeName. shortSwiftName,
105
- conformances: Constants . Operation. Body. conformances,
106
- members: bodyCases
107
- )
108
- )
109
118
110
- let contentTypeUsage = bodyTypeName. asUsage. withOptional ( hasNoContent)
111
- let contentProperty = PropertyBlueprint (
112
- comment: . doc( " Received HTTP response body " ) ,
113
- originalName: Constants . Operation. Body. variableName,
114
- typeUsage: contentTypeUsage,
115
- default: hasNoContent ? . nil : nil ,
116
- associatedDeclarations: [
117
- contentEnumDecl
118
- ] ,
119
- asSwiftSafeName: swiftSafeName
120
- )
119
+ let contentTypeUsage = bodyTypeName. asUsage. withOptional ( hasNoContent)
120
+ contentProperty = PropertyBlueprint (
121
+ comment: . doc( " Received HTTP response body " ) ,
122
+ originalName: Constants . Operation. Body. variableName,
123
+ typeUsage: contentTypeUsage,
124
+ default: hasNoContent ? . nil : nil ,
125
+ associatedDeclarations: [
126
+ contentEnumDecl
127
+ ] ,
128
+ asSwiftSafeName: swiftSafeName
129
+ )
130
+ } else {
131
+ contentProperty = nil
132
+ }
121
133
122
134
let responseStructDecl = translateStructBlueprint (
123
135
. init(
@@ -129,6 +141,7 @@ extension TypesFileTranslator {
129
141
headersProperty,
130
142
contentProperty,
131
143
]
144
+ . compactMap { $0 }
132
145
)
133
146
)
134
147
0 commit comments