Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate a static property to create a responses without any parameters #656

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ extension TypesFileTranslator {
/// - operation: The OpenAPI operation.
/// - operationJSONPath: The JSON path to the operation in the OpenAPI
/// document.
/// - Returns: A declaration of the enum case and a declaration of the
/// - Returns: A tuple containing a declaration of the enum case, a declaration of the
/// structure unique to the response that contains the response headers
/// and a body payload.
/// and a body payload, a declaration of a throwing getter and, an optional convenience static property.
/// - Throws: An error if there's an issue generating the declarations, such
/// as unsupported response types or invalid definitions.
func translateResponseOutcomeInTypes(
_ outcome: OpenAPI.Operation.ResponseOutcome,
operation: OperationDescription,
operationJSONPath: String
) throws -> (payloadStruct: Declaration?, enumCase: Declaration, throwingGetter: Declaration) {

) throws -> (
payloadStruct: Declaration?, enumCase: Declaration, staticMember: Declaration?, throwingGetter: Declaration
) {
let typedResponse = try typedResponse(from: outcome, operation: operation)
let responseStructTypeName = typedResponse.typeUsage.typeName
let responseKind = outcome.status.value.asKind
Expand All @@ -55,14 +56,34 @@ extension TypesFileTranslator {
}
associatedValues.append(.init(type: .init(responseStructTypeName)))

let enumCaseDesc = EnumCaseDescription(name: enumCaseName, kind: .nameWithAssociatedValues(associatedValues))
let enumCaseDecl: Declaration = .commentable(
responseKind.docComment(
userDescription: typedResponse.response.description,
jsonPath: operationJSONPath + "/responses/" + responseKind.jsonPathComponent
),
.enumCase(enumCaseDesc)
let enumCaseDocComment = responseKind.docComment(
userDescription: typedResponse.response.description,
jsonPath: operationJSONPath + "/responses/" + responseKind.jsonPathComponent
)
let enumCaseDesc = EnumCaseDescription(name: enumCaseName, kind: .nameWithAssociatedValues(associatedValues))
let enumCaseDecl: Declaration = .commentable(enumCaseDocComment, .enumCase(enumCaseDesc))

var staticMemberDecl: Declaration?
let responseHasNoHeaders = typedResponse.response.headers?.isEmpty ?? true
let responseHasNoContent = typedResponse.response.content.isEmpty
if responseHasNoContent && responseHasNoHeaders && !responseKind.wantsStatusCode {
let staticMemberDesc = VariableDescription(
accessModifier: config.access,
isStatic: true,
kind: .var,
left: .identifier(.pattern(enumCaseName)),
type: .member(["Self"]),
getter: [
.expression(
.functionCall(
calledExpression: .dot(enumCaseName),
arguments: [.functionCall(calledExpression: .dot("init"))]
)
)
]
)
staticMemberDecl = .commentable(enumCaseDocComment, .variable(staticMemberDesc))
}
arthurcro marked this conversation as resolved.
Show resolved Hide resolved

let throwingGetterDesc = VariableDescription(
accessModifier: config.access,
Expand Down Expand Up @@ -113,7 +134,7 @@ extension TypesFileTranslator {
)
let throwingGetterDecl = Declaration.commentable(throwingGetterComment, .variable(throwingGetterDesc))

return (responseStructDecl, enumCaseDecl, throwingGetterDecl)
return (responseStructDecl, enumCaseDecl, staticMemberDecl, throwingGetterDecl)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ extension TypesFileTranslator {
let documentedMembers: [Declaration] = documentedOutcomes.flatMap {
inlineResponseDecl,
caseDecl,
throwingGetter in [inlineResponseDecl, caseDecl, throwingGetter].compactMap { $0 }
staticDecl,
throwingGetter in [inlineResponseDecl, caseDecl, staticDecl, throwingGetter].compactMap { $0 }
}

let allMembers: [Declaration]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,14 @@ public enum Operations {
///
/// HTTP response code: `204 noContent`.
case noContent(Operations.createPetWithForm.Output.NoContent)
/// Successfully created pet using a url form
///
/// - Remark: Generated from `#/paths//pets/create/post(createPetWithForm)/responses/204`.
///
/// HTTP response code: `204 noContent`.
public static var noContent: Self {
.noContent(.init())
}
/// The associated value of the enum case if `self` is `.noContent`.
///
/// - Throws: An error if `self` is not `.noContent`.
Expand Down Expand Up @@ -2499,6 +2507,14 @@ public enum Operations {
///
/// HTTP response code: `202 accepted`.
case accepted(Operations.postStats.Output.Accepted)
/// Accepted data.
///
/// - Remark: Generated from `#/paths//pets/stats/post(postStats)/responses/202`.
///
/// HTTP response code: `202 accepted`.
public static var accepted: Self {
.accepted(.init())
}
/// The associated value of the enum case if `self` is `.accepted`.
///
/// - Throws: An error if `self` is not `.accepted`.
Expand Down Expand Up @@ -2541,6 +2557,14 @@ public enum Operations {
///
/// HTTP response code: `204 noContent`.
case noContent(Operations.probe.Output.NoContent)
/// Ack
///
/// - Remark: Generated from `#/paths//probe//post(probe)/responses/204`.
///
/// HTTP response code: `204 noContent`.
public static var noContent: Self {
.noContent(.init())
}
/// The associated value of the enum case if `self` is `.noContent`.
///
/// - Throws: An error if `self` is not `.noContent`.
Expand Down Expand Up @@ -2626,6 +2650,14 @@ public enum Operations {
///
/// HTTP response code: `204 noContent`.
case noContent(Operations.updatePet.Output.NoContent)
/// Successfully updated
///
/// - Remark: Generated from `#/paths//pets/{petId}/patch(updatePet)/responses/204`.
///
/// HTTP response code: `204 noContent`.
public static var noContent: Self {
.noContent(.init())
}
/// The associated value of the enum case if `self` is `.noContent`.
///
/// - Throws: An error if `self` is not `.noContent`.
Expand Down Expand Up @@ -3098,6 +3130,14 @@ public enum Operations {
///
/// HTTP response code: `202 accepted`.
case accepted(Operations.multipartUploadTyped.Output.Accepted)
/// Successfully accepted the data.
///
/// - Remark: Generated from `#/paths//pets/multipart-typed/post(multipartUploadTyped)/responses/202`.
///
/// HTTP response code: `202 accepted`.
public static var accepted: Self {
.accepted(.init())
}
/// The associated value of the enum case if `self` is `.accepted`.
///
/// - Throws: An error if `self` is not `.accepted`.
Expand Down