-
Notifications
You must be signed in to change notification settings - Fork 129
Enable the generated server-side code to validate the content-type. #140
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
Enable the generated server-side code to validate the content-type. #140
Conversation
… header in server program
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @takeshi-1000, thanks for taking this up!
Overall, your change looks good, can you just add a unit test? For example, you could create a variant of the test for createPet
(here) that purposefully passes the incorrect content type and verifies that an error was thrown with XCTAssertThrowsError
(but no need to test the contents of the error, as it's an internal type).
@czechboy0 I am considering the following code, but is there any better alternative? client = .init(
createPetBlock: { input in
return .created(
.init(
headers: .init(
X_Extra_Arguments: .init(code: 1)
),
body: .json(
.init(id: 1, name: "Fluffz")
)
)
)
}
)
do {
_ = try await server.createPet(
.init(
path: "/api/pets",
method: .post,
headerFields: [
.init(name: "x-extra-arguments", value: #"{"code":1}"#),
.init(name: "content-type", value: "text/plain; charset=utf-8"),
],
encodedBody: #"""
{
"name" : "Fluffz"
}
"""#
),
.init()
)
XCTFail("The method should have thrown an error.")
} catch {
XCTAssertNotNil(error)
} |
Ah you're right. Yeah, a manual do/catch block, where you XCTFail at the end of the do block, and do nothing in the catch block is how I've worked around this limitation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two more suggestions, just to simplify the tests a bit more, otherwise looks good. 👍
Co-authored-by: Honza Dvorsky <czechboy0@gmail.com>
Thank you for checking ! I applied your suggestion . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thank you!
@swift-server-bot add to allowlist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the change looks good, just added a suggestion on how to fix the red CI.
Co-authored-by: Honza Dvorsky <czechboy0@gmail.com>
I missed https://github.com/apple/swift-openapi-generator/blob/main/CONTRIBUTING.md#run-scriptssoundnesssh, and am confirming . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @takeshi-1000 for getting involved!
Motivation
I am interested this tool and have selected something that I can work on.
Resolve #16
Modifications
To validate content-type header string in server program, I modify
traslateServerDesirializer
method .Result
For example you will define below opeapi yaml
when you try the request, runtime error happen.
Test Plan
I have made the necessary changes to adapt to the newly generated code on the reference server side and have confirmed that it passes the tests locally.