Skip to content

JSON Patch uses org.springframework.core.convert.support.DefaultConversionService#getSharedInstance #2233

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

Closed
buckett opened this issue Feb 21, 2023 · 7 comments
Assignees
Labels
type: bug A general bug

Comments

@buckett
Copy link

buckett commented Feb 21, 2023

When a JSON patch request is made the paths are parsed and converted into org.springframework.data.rest.webmvc.json.patch.SpelPath when values are then replaced the SpEL paths are then evaluated using a org.springframework.expression.spel.ExpressionState and this uses a org.springframework.expression.spel.support.SimpleEvaluationContext which then ended up using a org.springframework.expression.spel.support.StandardTypeConverter, this in turn uses org.springframework.core.convert.support.DefaultConversionService#getSharedInstance for it's conversion service.

This works for simple cases (Strings and Ints), but the DefaultConversionService doesn't have complete support for Java 8 date and time classes and the JavaDoc on the method recommends that the service should only be used as a fallback.

There doesn't seem to be a way to override the ConversionService that is used when evaluating SpEL paths in JSON Patch files and it doesn't seem to be possible to add additional converters to the DefaultConversionService.

One solution might be to have org.springframework.data.rest.webmvc.json.patch.SpelPath.TypedSpelPath#CONTEXT setable at runtime and therefore allow a EvaluationContext with a different conversion service to be used.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 21, 2023
@buckett
Copy link
Author

buckett commented Feb 21, 2023

I've got a tiny test that shows this problem in: https://github.com/buckett/spring-data-rest/tree/test-2233

@odrotbohm
Copy link
Member

Would you mind zooming out a bit and describe a user level problem this results in?

@odrotbohm
Copy link
Member

Nevermind, your test case has all I need to think about this.

@buckett
Copy link
Author

buckett commented Feb 21, 2023

Just to check I wasn't missing anything I did also create a test project showing the issue: https://github.com/buckett/json-patch-test

odrotbohm added a commit that referenced this issue Feb 21, 2023
We no pipe the Spring MVC ConversionService into the JSON Patch path binding. That usually is a FormattingConversionService at runtime and also supports the conversion of dates.

The ConversionServices is configured into the BindContext(Factory) we use for binding. The context then exposes the EvaluationContext set up with it.

Fixes #2233.
odrotbohm added a commit that referenced this issue Feb 21, 2023
We no pipe the Spring MVC ConversionService into the JSON Patch path binding. That usually is a FormattingConversionService at runtime and also supports the conversion of dates.

The ConversionServices is configured into the BindContext(Factory) we use for binding. The context then exposes the EvaluationContext set up with it.

Fixes #2233.
@odrotbohm
Copy link
Member

I've just pushed a fix that included the test case you provided with the earlier project now succeeding. Would you mind trying the 4.0.3 snapshots (spring-data-bom.version to 4.0.3-SNAPSHOT)?

@buckett
Copy link
Author

buckett commented Feb 21, 2023

Failing with the BOM

I tried added <spring-data-bom.version>4.0.3-SNAPSHOT</spring-data-bom.version> to the Spring Boot app and then it tries to download org.springframework.data:spring-data-bom:pom:4.0.3-SNAPSHOT but fails.
I tried adding the Spring specific snapshot repository:

	<repositories>
		<repository>
			<id>spring-snapshot</id>
			<name>Spring Snapshot Repository</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>

but this doesn't appear to help and I can't see any similar versions in the UI: https://repo.spring.io/ui/packages/gav:%2F%2Forg.springframework.data:spring-data-bom?name=spring-data-bom&type=packages

Pulling in 4.0.x branch

Instead I've tried pulling <spring-data-bom.version>2022.0.3-SNAPSHOT</spring-data-bom.version> and this seems to depend on 4.0.3-SNAPSHOT after a bit of messing about (mvn dependency:purge) I have it downloading the latest artifacts and the fix looks good, in my test project after creating a person:

## Create a person
POST http://localhost:8080/persons
Content-Type: application/json

{
  "id": 1,
  "name": "Test",
  "birthday": "2000-01-01"
}

I then attempted to patch the birthday field:

PATCH http://localhost:8080/persons/1
Content-Type: application/json-patch+json

[
  {"op": "replace", "path": "/birthday", "value": "1980-02-20"}
]

it worked (HTTP 204) 👍 and double checking the person now has the new birthday.

@odrotbohm
Copy link
Member

Yeez, I messed the version numbers up: it's got to be 2022.0.3-SNAPSHOT for the spring-data-bom.version property. That should give you 4.0.3-SNAPSHOT of SD REST. Looks like you got it working nonetheless. So looking forward to the release! 🥳

@odrotbohm odrotbohm added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 21, 2023
@odrotbohm odrotbohm self-assigned this Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants