Skip to content

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

Merged
merged 8 commits into from
Jul 26, 2023

Conversation

takeshi-1000
Copy link
Contributor

@takeshi-1000 takeshi-1000 commented Jul 25, 2023

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

  /greet:
    post:
      operationId: postGreeting
      requestBody:
        required: true
        content:
          text/plain:
            schema:
              type: string

when you try the request, runtime error happen.

$ curl localhost:8080/api/greet -X POST \             
> -H "Content-Type: application/json" \
> -d '{ "name": "foo" }'

{"error":true,"reason":"Server error - operationID: postGreeting, request: path: \/api\/greet, query: <nil>, method: HTTPMethod(value: OpenAPIRuntime.HTTPMethod.(unknown context at $106e9fd78).OpenAPIHTTPMethod.POST), header fields: [Host: localhost:8080, User-Agent: curl\/7.88.1, Accept: *\/*, Content-Type: application\/json, Content-Length: 64], body (prefix): { \"name\": \"foo\"}, metadata: path parameters: [:], query parameters: [], operationInput: <nil>, operationOutput: <nil>, underlying error: Unexpected Content-Type header: application\/json"}

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.

Copy link
Contributor

@czechboy0 czechboy0 left a 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).

@takeshi-1000
Copy link
Contributor Author

takeshi-1000 commented Jul 25, 2023

@czechboy0
Thank you for reviewing .
I tried to add, but XCTAssertThrowsError seems not to supported async method.
https://developer.apple.com/documentation/xctest/1500795-xctassertthrowserror

2023-07-26 8 48のイメージ

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)
        }

@czechboy0
Copy link
Contributor

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.

Copy link
Contributor

@czechboy0 czechboy0 left a 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>
@takeshi-1000
Copy link
Contributor Author

Thank you for checking ! I applied your suggestion .

Copy link
Contributor

@czechboy0 czechboy0 left a 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!

@czechboy0
Copy link
Contributor

@swift-server-bot add to allowlist

@czechboy0 czechboy0 requested a review from simonjbeaumont July 26, 2023 13:57
Copy link
Contributor

@czechboy0 czechboy0 left a 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>
@takeshi-1000
Copy link
Contributor Author

Copy link
Collaborator

@simonjbeaumont simonjbeaumont left a 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!

@simonjbeaumont simonjbeaumont enabled auto-merge (squash) July 26, 2023 20:53
@simonjbeaumont simonjbeaumont merged commit 8a007c7 into apple:main Jul 26, 2023
@takeshi-1000 takeshi-1000 deleted the add-validate-content-type branch July 27, 2023 14:10
@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Server should validate Content-Type header
3 participants