Uploaded image for project: 'Spring Data REST'
  1. Spring Data REST
  2. DATAREST-1405

Using RepositoryRestConfigurer withEntityLookup does not work for a PUT on existing records

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Waiting for Feedback
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 3.1.9 (Lovelace SR9)
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      macOS Mojave, Java 8, Spring Boot 2.1.6

      Description

      When using a RepositoryRestConfigurer with an Entity Lookup to customise the id part of URI, not all methods work as expected.

      In the linked github repo, I've created a project which demonstrates the issue.  In it, I've got a User entity which uses a Generated id as the primary key, but uses the userName as a natural key.

      @Data
      @Entity
      class User {
      
          @Id
          @GeneratedValue
          private long id;
      
          @NaturalId
          private String userName;
      
          private String fullName;
      
      }
      

      I've added a RepositoryRestConfigurer like this:

      @Component
      class RepositoryCustomization implements RepositoryRestConfigurer {
      
          @Override
          public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
      
              config.withEntityLookup().
                      forRepository(UserRepository.class, User::getUserName, UserRepository::findByUserName);
          }
      }
      

      This allows me to create a new record like this:

      Curl:

      curl -X POST \
        http://localhost:8080/users \
        -H 'Content-Type: application/json' \
        -d '{
          "userName": "DAVIDM",
          "fullName": "David MacDonald"
      }'
      

      I get the following good response, with the userId in the self reference:

      {
        "userName" : "DAVIDM",
        "fullName" : "David MacDonald",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8080/users/DAVIDM"
          },
          "user" : {
            "href" : "http://localhost:8080/users/DAVIDM"
          }
        }
      

      I can do a get using the userName:

      curl http://localhost:8080/users/DAVIDM
      

      response:

      {
        "userName" : "DAVIDM",
        "fullName" : "David MacDonald",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8080/users/DAVIDM"
          },
          "user" : {
            "href" : "http://localhost:8080/users/DAVIDM"
          }
        }
      }
      

      However, if I try and update the full name with a PUT:

      curl -X PUT \
        http://localhost:8080/users/DAVIDM \
        -H 'Content-Type: application/json' \
        -d '{
        "userName" : "DAVIDM",
        "fullName" : "David MacDonald Love Spring"
      }'
      

      I get the following response:

      {
        "cause": {
          "cause": null,
          "message": "For input string: \"DAVIDM\""
        },
        "message": "Failed to convert from type [java.lang.String] to type [long] for value 'DAVIDM'; nested exception is java.lang.NumberFormatException: For input string: \"DAVIDM\""
      }
      

      If I use the internally generated primary key, it works:

      curl -X PUT \
        http://localhost:8080/users/1 \
        -H 'Content-Type: application/json' \
        -d '{
        "userName" : "DAVIDM",
        "fullName" : "David MacDonald Love Spring"
      }'
      

      Response:

      {
        "userName" : "DAVIDM",
        "fullName" : "David MacDonald Love Spring",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8080/users/DAVIDM"
          },
          "user" : {
            "href" : "http://localhost:8080/users/DAVIDM"
          }
        }
      }
      

      I can do the delete using the username:

      curl -X DELETE http://localhost:8080/users/DAVIDM
      

      It seems like all methods work as expected with the exception of the PUT.

      Thanks.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              olivergierke Oliver Drotbohm
              Reporter:
              davidm76 davidm76
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: