Vapor 3 GET route for sensitive data











up vote
2
down vote

favorite












Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:



protectedRouter.get("users", User.parameter, use: userController.user)


And method in UserController:



func user(_ req: Request) throws -> Future<User> {
return try req.parameters.next(User.self)
}


And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).










share|improve this question


























    up vote
    2
    down vote

    favorite












    Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:



    protectedRouter.get("users", User.parameter, use: userController.user)


    And method in UserController:



    func user(_ req: Request) throws -> Future<User> {
    return try req.parameters.next(User.self)
    }


    And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:



      protectedRouter.get("users", User.parameter, use: userController.user)


      And method in UserController:



      func user(_ req: Request) throws -> Future<User> {
      return try req.parameters.next(User.self)
      }


      And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).










      share|improve this question













      Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:



      protectedRouter.get("users", User.parameter, use: userController.user)


      And method in UserController:



      func user(_ req: Request) throws -> Future<User> {
      return try req.parameters.next(User.self)
      }


      And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).







      swift vapor vapor3






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 5 at 1:38









      faviomob

      2,53712125




      2,53712125
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.



          A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,



          extension User {
          struct Public: Content { ... }

          func makePublic() -> Public { ... }
          }


          Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.



          You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147253%2fvapor-3-get-route-for-sensitive-data%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.



            A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,



            extension User {
            struct Public: Content { ... }

            func makePublic() -> Public { ... }
            }


            Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.



            You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.






            share|improve this answer

























              up vote
              4
              down vote



              accepted










              Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.



              A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,



              extension User {
              struct Public: Content { ... }

              func makePublic() -> Public { ... }
              }


              Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.



              You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.






              share|improve this answer























                up vote
                4
                down vote



                accepted







                up vote
                4
                down vote



                accepted






                Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.



                A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,



                extension User {
                struct Public: Content { ... }

                func makePublic() -> Public { ... }
                }


                Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.



                You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.






                share|improve this answer












                Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.



                A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,



                extension User {
                struct Public: Content { ... }

                func makePublic() -> Public { ... }
                }


                Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.



                You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 5 at 19:09









                tanner0101

                3,0951233




                3,0951233






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147253%2fvapor-3-get-route-for-sensitive-data%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()