Skip to content

Commit f92eb0d

Browse files
committed
fix to ensure the initialization of response headers when there is at least one required response header.
1 parent 0fc97c5 commit f92eb0d

File tree

1 file changed

+93
-12
lines changed

1 file changed

+93
-12
lines changed

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponse.swift

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,23 @@ extension TypesFileTranslator {
102102
asSwiftSafeName: swiftSafeName
103103
)
104104

105-
let responseStructDecl = translateStructBlueprint(
106-
.init(
107-
comment: nil,
108-
access: config.access,
109-
typeName: typeName,
110-
conformances: Constants.Operation.Output.Payload.conformances,
111-
properties: [
112-
headersProperty,
113-
contentProperty,
114-
]
115-
)
105+
let structBlueprint: StructBlueprint = .init(
106+
comment: nil,
107+
access: config.access,
108+
typeName: typeName,
109+
conformances: Constants.Operation.Output.Payload.conformances,
110+
properties: [
111+
headersProperty,
112+
contentProperty,
113+
]
116114
)
117115

118-
return responseStructDecl
116+
let responseStructDecl = _calculateRequiredHeadersForInitialize(
117+
with: headers,
118+
from: translateStructBlueprint(structBlueprint)
119+
)
120+
121+
return responseStructDecl.deprecate(if: structBlueprint.isDeprecated)
119122
}
120123

121124
/// Returns a list of declarations for the specified reusable response
@@ -138,4 +141,82 @@ extension TypesFileTranslator {
138141
response: response
139142
)
140143
}
144+
145+
/// Calculate the necessary parameters for initializing the response headers
146+
/// and return the list of specified reusable response declarations.
147+
/// - Parameters:
148+
/// - headers: the typed response headers
149+
/// - responseStructDecl: A structure declaration before calculate.
150+
/// - Returns: A structure declaration.
151+
private func _calculateRequiredHeadersForInitialize(
152+
with headers: [TypedResponseHeader],
153+
from responseStructDecl: Declaration
154+
) -> Declaration {
155+
guard case .commentable(let comment, let structDec) = responseStructDecl,
156+
case .struct(let structDescription) = structDec
157+
else {
158+
return responseStructDecl
159+
}
160+
161+
let newMembers: [Declaration] = structDescription
162+
.members
163+
.reduce(into: [Declaration]()) { partialResult, member in
164+
let labelHeaders = "headers"
165+
166+
if case .commentable(let memberComment, let memberDecl) = member,
167+
case .function(let memberFuncDescription) = memberDecl,
168+
memberFuncDescription.signature.kind == .initializer,
169+
memberFuncDescription.signature.parameters.first(where: { $0.label == labelHeaders }) != nil
170+
{
171+
172+
let initParameters: [ParameterDescription] = memberFuncDescription
173+
.signature
174+
.parameters
175+
.map { parameterDesc in
176+
guard parameterDesc.label == labelHeaders else {
177+
return parameterDesc
178+
}
179+
let defaultValue: Expression? = {
180+
if headers.isEmpty {
181+
return PropertyBlueprint.DefaultValue.emptyInit.asExpression
182+
}
183+
if headers.first(where: { !$0.isOptional }) != nil {
184+
return nil
185+
}
186+
return PropertyBlueprint.DefaultValue.emptyInit.asExpression
187+
}()
188+
return .init(
189+
label: parameterDesc.label,
190+
name: parameterDesc.name,
191+
type: parameterDesc.type,
192+
defaultValue: defaultValue
193+
)
194+
}
195+
196+
let initDescription: FunctionDescription = .init(
197+
accessModifier: memberFuncDescription.signature.accessModifier,
198+
kind: memberFuncDescription.signature.kind,
199+
parameters: initParameters,
200+
keywords: memberFuncDescription.signature.keywords,
201+
returnType: memberFuncDescription.signature.returnType,
202+
body: memberFuncDescription.body
203+
)
204+
205+
partialResult.append(
206+
.commentable(memberComment, .function(initDescription))
207+
)
208+
} else {
209+
partialResult.append(member)
210+
}
211+
}
212+
213+
let structDescriptionWithCalc = StructDescription(
214+
accessModifier: structDescription.accessModifier,
215+
name: structDescription.name,
216+
conformances: structDescription.conformances,
217+
members: newMembers
218+
)
219+
220+
return .commentable(comment, .struct(structDescriptionWithCalc))
221+
}
141222
}

0 commit comments

Comments
 (0)