Skip to content

Relax Codable Router type requirements#1242

Merged
djones6 merged 16 commits intomasterfrom
issue_1235
May 10, 2018
Merged

Relax Codable Router type requirements#1242
djones6 merged 16 commits intomasterfrom
issue_1235

Conversation

@djones6
Copy link
Copy Markdown
Collaborator

@djones6 djones6 commented Apr 11, 2018

Description

Implementation of Kitura Evolution proposal: Relax Type Constraints for Codable Routing

Resolves #1235 by relaxing the type requirements on the Codable Router to Decodable for input types, and Encodable for output types.

This also requires a corresponding Pull Request in KituraContracts: Kitura/KituraContracts#18

Motivation and Context

See #1235.

The Codable Router was designed with a symmetry in mind for types to be shared between the client and server side (KituraKit <-> Kitura), which requires those types to be both Encodable and Decodable.

In addition, for types that contain purely Codable attributes, conformance to Codable can be synthesized by the compiler. However, there are cases where conformance must be implemented manually.

For cases where the user only wants to use type safety in a single direction (such as Decodable for a type that will only be received), we currently require them to implement conformance to Encodable (at least dummy conformance to satisfy the type requirement).

Notable changes to behaviour

In order to facilitate the receiving of a Decodable type in a PUT or POST, without requiring that we respond with the same type (or some dummy type) conforms to Encodable, it is necessary to allow Codable responses of (nil, .someSuccessStatus).

In the current implementation I have relaxed this to also permit (nil, nil), where the nil RequestError signifies success (with the default code for that method), and the nil Encodable signifies that we wish to return no data.

Previously, such a response would have failed (due to trying to JSON encode a nil), and resulted in logging in error (Could not encode result: invalidValue(nil, ....) and returning .internalServerError status.

How Has This Been Tested?

Checklist:

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

@ianpartridge
Copy link
Copy Markdown
Collaborator

@djones6 has a conflict...

@codecov-io
Copy link
Copy Markdown

codecov-io commented Apr 12, 2018

Codecov Report

Merging #1242 into master will increase coverage by 0.04%.
The diff coverage is 97.67%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1242      +/-   ##
==========================================
+ Coverage   88.11%   88.15%   +0.04%     
==========================================
  Files          37       37              
  Lines        2347     2355       +8     
==========================================
+ Hits         2068     2076       +8     
  Misses        279      279
Flag Coverage Δ
#Kitura 88.15% <97.67%> (+0.04%) ⬆️
Impacted Files Coverage Δ
Sources/Kitura/CodableRouter.swift 81.31% <97.67%> (+0.56%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02b45eb...7fabbf7. Read the comment docs.

@djones6 djones6 changed the title Relax Codable Router type requirements WIP: Relax Codable Router type requirements Apr 30, 2018
@djones6
Copy link
Copy Markdown
Collaborator Author

djones6 commented May 2, 2018

@ianpartridge I've rebased this (and Kitura/KituraContracts#18) on the latest from master, and proposed a solution for the issue of POST / PUT which I have detailed in 'Notable changes to behaviour' in the description above.

@djones6 djones6 requested a review from ianpartridge May 4, 2018 09:19
@djones6 djones6 changed the title WIP: Relax Codable Router type requirements Relax Codable Router type requirements May 4, 2018

// POST
fileprivate func postSafely<I: Codable, O: Codable>(_ route: String, handler: @escaping CodableClosure<I, O>) {
// POST
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please remove the stray space.


// PUT with Identifier
fileprivate func putSafely<Id: Identifier, I: Codable, O: Codable>(_ route: String, handler: @escaping IdentifierCodableClosure<Id, I, O>) {
// PUT with Identifier
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Stray space.

}

enum OnlyEncodable: Encodable {
case data(String)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Extra space needed.

}

enum OnlyDecodable: Decodable {
case data(String)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Extra space needed.

@djones6
Copy link
Copy Markdown
Collaborator Author

djones6 commented May 9, 2018

@ianpartridge done.

Copy link
Copy Markdown
Collaborator

@ianpartridge ianpartridge left a comment

Choose a reason for hiding this comment

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

w00t!

@djones6 djones6 merged commit 91f47de into master May 10, 2018
@djones6 djones6 deleted the issue_1235 branch May 10, 2018 08:30
djones6 added a commit that referenced this pull request May 16, 2018
djones6 added a commit that referenced this pull request May 16, 2018
djones6 added a commit that referenced this pull request May 30, 2018
- and permit `nil` Codable responses when a default or custom success status is returned (building on #1254)
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.

CodableClosure should use Decodable -> Encodable

3 participants