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

Add POST codable route support without input object. #1491

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

mbarnach
Copy link
Member

Add a new codable route for POST with no object as input.

Description

This allows to create a resource without arguments, like this:

router.post("/resource") { (respondWith: (Resource?, RequestError?) -> Void) in
  respondWith(resource, nil)
}

Motivation and Context

It is not possible to use the POST verb without argument, making it weird to create a new resource from an endpoint that has no dependencies.

How Has This Been Tested?

External testing done. I'm unsure about where to add the test if needed.

Checklist:

  • If applicable, I have updated the documentation accordingly.
  • If applicable, I have added tests to cover my changes.

This allows to create a resource without arguments.
@ianpartridge
Copy link
Collaborator

Thanks for the PR! We'll get it reviewed.

Copy link
Collaborator

@djones6 djones6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether REST has an opinion on doing this, but I can imagine there may be circumstances where you might want a resource that contains only values generated by the server. Could you elaborate a little on the use-cases for this?

Regarding tests, you can add a new test to TestCodableRouter, copying the example from testBasicPost: https://github.com/IBM-Swift/Kitura/blob/master/Tests/KituraTests/TestCodableRouter.swift#L143

I think it would also be useful to add a test to the SwaggerGenerator to make sure the generated document makes sense - unfortunately the tests here are not quite so well crafted, but you can follow the example of an existing POST route test here:

@mbarnach
Copy link
Member Author

I've updated the tests.
For the use case, I guess it could be seen as a semantically different way of saying GET /resource but with creation, like for a token, a new "element", etc.
For the REST opinion, I've found that (interesting) thread: w3 lists. Long story short, not so common, but can make sense (so an option to have?). There is also some valid arguments in that StackOverflow thread, especially about the caching involved eventually.

@@ -617,6 +623,15 @@ final class TestSwaggerGeneration: KituraTest, KituraTestSuite {
}

func pathContentAssertions3(paths: [String: Any]) {
// Test empty POST
if let path = paths["/me/new"] as? [String: Any] {
if let new = path["new"] as? [String: Any] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line generates a compile warning because new is not used. Could change to let _ = or maybe there's a neater way to check the type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Thanks.

@djones6
Copy link
Collaborator

djones6 commented Feb 7, 2020

@ianpartridge The code for this change looks okay, what do you think?

@djones6
Copy link
Collaborator

djones6 commented Feb 10, 2020

The Travis failure on this job is unrelated to this PR, and has been happening on the CRON builds for master since about 3 weeks ago, eg: https://travis-ci.org/IBM-Swift/Kitura/jobs/648208574
Timeouts in tests when running with the combination of KITURA_NIO on OSX. @harish1992 could you take a look?

@CLAassistant
Copy link

CLAassistant commented Sep 27, 2020

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants