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

Using RepositoryRestConfigurer withEntityLookup does not work for a PUT on existing records [DATAREST-1405] #1764

Closed
spring-projects-issues opened this issue Jun 27, 2019 · 4 comments
Assignees
Labels
status: duplicate type: bug

Comments

@spring-projects-issues
Copy link

spring-projects-issues commented Jun 27, 2019

davidm76 opened DATAREST-1405 and commented

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.


Affects: 3.1.9 (Lovelace SR9)

Reference URL: https://github.com/davidm76/spring-data-rest-issue

Issue Links:

  • DATAREST-1304 PUT and PATCH don't work, when custom entity lookup is configured
@spring-projects-issues
Copy link
Author

spring-projects-issues commented Jun 27, 2019

Oliver Drotbohm commented

Would you mind trying the Moore snapshots? We've just recently tweaked stuff in this area for DATAREST-1304. Unfortunately we cannot port this back as the fix requires new API introduced for Moore in Spring Data Commons

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Jun 27, 2019

davidm76 commented

Thanks Oliver Drotbohm,

I've set this in my pom.xml file:

<spring-data-releasetrain.version>Moore-RC1</spring-data-releasetrain.version>

All builds, but when I run it, it fails to start:

Caused by: java.lang.NoClassDefFoundError: org/springframework/hateoas/server/mvc/UriComponentsContributor

Anything else I need to add to get it to run with the latest release version of Spring Boot?

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Aug 6, 2019

davidm76 commented

I've re-tested with Spring Boot 2.2 M4 and can confirm that the issue is resolved

@spring-projects-issues spring-projects-issues added status: waiting-for-feedback type: bug labels Dec 31, 2020
@spring-projects-issues
Copy link
Author

spring-projects-issues commented Jan 7, 2021

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder label Jan 7, 2021
@gregturn gregturn added status: duplicate and removed status: feedback-reminder status: waiting-for-feedback labels Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate type: bug
Projects
None yet
Development

No branches or pull requests

3 participants