-
Notifications
You must be signed in to change notification settings - Fork 119
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
Fix a bug where JSON content is not always validated #804
Draft
ikhoon
wants to merge
7
commits into
line:main
Choose a base branch
from
ikhoon:validate-json-content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec74064
Fix a bug where an JSON content is not validated
ikhoon 2fe5aef
Fix test
ikhoon 15893eb
Fix test errors
ikhoon 2173a7e
Update server/src/test/java/com/linecorp/centraldogma/server/internal…
ikhoon 71d7817
Update server/src/test/java/com/linecorp/centraldogma/server/internal…
ikhoon 0310e1e
Update server/src/test/java/com/linecorp/centraldogma/server/internal…
ikhoon 52938e9
Merge branch 'main' of https://github.com/line/centraldogma into vali…
jrhee17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,6 +32,7 @@ | |||||
import org.junit.jupiter.api.Test; | ||||||
import org.junit.jupiter.api.extension.RegisterExtension; | ||||||
|
||||||
import com.fasterxml.jackson.core.JsonParseException; | ||||||
import com.fasterxml.jackson.databind.JsonNode; | ||||||
|
||||||
import com.linecorp.armeria.client.WebClient; | ||||||
|
@@ -44,6 +45,8 @@ | |||||
import com.linecorp.armeria.common.RequestHeaders; | ||||||
import com.linecorp.armeria.common.ResponseHeaders; | ||||||
import com.linecorp.centraldogma.common.ChangeConflictException; | ||||||
import com.linecorp.centraldogma.common.ChangeFormatException; | ||||||
import com.linecorp.centraldogma.common.Entry; | ||||||
import com.linecorp.centraldogma.common.InvalidPushException; | ||||||
import com.linecorp.centraldogma.common.RedundantChangeException; | ||||||
import com.linecorp.centraldogma.internal.Jackson; | ||||||
|
@@ -154,6 +157,85 @@ void pushFileToMetaRepositoryShouldFail() { | |||||
assertThat(res.contentUtf8()).contains(InvalidPushException.class.getName()); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void pushEmbeddedJsonString() throws JsonParseException { | ||||||
final WebClient client = dogma.httpClient(); | ||||||
|
||||||
final String body = | ||||||
'{' + | ||||||
" \"path\" : \"/embedded-string.json\"," + | ||||||
" \"type\" : \"UPSERT_JSON\"," + | ||||||
" \"content\" : \"\\\"json string\\\"\"," + | ||||||
" \"commitMessage\" : {" + | ||||||
" \"summary\" : \"Add embedded string.json\"," + | ||||||
" \"detail\": \"An embedded JSON string.\"," + | ||||||
" \"markup\": \"PLAINTEXT\"" + | ||||||
" }" + | ||||||
'}'; | ||||||
final RequestHeaders headers = | ||||||
RequestHeaders.of(HttpMethod.POST, "/api/v1/projects/myPro/repos/myRepo/contents", | ||||||
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON); | ||||||
final AggregatedHttpResponse res = client.execute(headers, body).aggregate().join(); | ||||||
assertThat(res.status()).isEqualTo(HttpStatus.OK); | ||||||
|
||||||
final Entry<?> entry = dogma.client().forRepo("myPro", "myRepo") | ||||||
.file("/embedded-string.json") | ||||||
.get().join(); | ||||||
assertThat(entry.contentAsText()).isEqualTo("\"json string\""); | ||||||
final JsonNode content = entry.contentAsJson(); | ||||||
assertThat(content.isTextual()).isTrue(); | ||||||
assertThat(content.asText()).isEqualTo("json string"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void pushUnquoteJsonString() throws JsonParseException { | ||||||
final WebClient client = dogma.httpClient(); | ||||||
|
||||||
final String body = | ||||||
'{' + | ||||||
" \"path\" : \"/string.json\"," + | ||||||
" \"type\" : \"UPSERT_JSON\"," + | ||||||
" \"content\" : \"json string\"," + | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: for reviewers
Suggested change
|
||||||
" \"commitMessage\" : {" + | ||||||
" \"summary\" : \"Add string.json\"," + | ||||||
" \"detail\": \"An JSON string.\"," + | ||||||
" \"markup\": \"PLAINTEXT\"" + | ||||||
" }" + | ||||||
'}'; | ||||||
final RequestHeaders headers = | ||||||
RequestHeaders.of(HttpMethod.POST, "/api/v1/projects/myPro/repos/myRepo/contents", | ||||||
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON); | ||||||
final AggregatedHttpResponse res = client.execute(headers, body).aggregate().join(); | ||||||
assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST); | ||||||
// Rejected by ChangesRequestConverter | ||||||
assertThat(res.contentUtf8()).contains(ChangeFormatException.class.getName()); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void pushInvalidEmbeddedJson() { | ||||||
final WebClient client = dogma.httpClient(); | ||||||
|
||||||
// An invalid JSON containing a trailing comma. | ||||||
final String body = | ||||||
'{' + | ||||||
" \"path\" : \"/invalid.json\"," + | ||||||
" \"type\" : \"UPSERT_JSON\"," + | ||||||
" \"content\" : \"{\\\"trailing\\\": \\\"comma\\\", }\"," + | ||||||
" \"commitMessage\" : {" + | ||||||
" \"summary\" : \"Add invalid.json\"," + | ||||||
" \"detail\": \"An invalid JSON must be rejected.\"," + | ||||||
" \"markup\": \"PLAINTEXT\"" + | ||||||
" }" + | ||||||
'}'; | ||||||
final RequestHeaders headers = | ||||||
RequestHeaders.of(HttpMethod.POST, "/api/v1/projects/myPro/repos/myRepo/contents", | ||||||
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON); | ||||||
final AggregatedHttpResponse res = client.execute(headers, body).aggregate().join(); | ||||||
assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST); | ||||||
// Rejected by ChangesRequestConverter | ||||||
assertThat(res.contentUtf8()).contains(ChangeFormatException.class.getName()); | ||||||
} | ||||||
|
||||||
@Nested | ||||||
class FilesTest { | ||||||
|
||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Can we apply this approach that checks if it's textual in the
Change.ofJsonUpsert
method?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"string"
is a valid JSON type. So I don't think we can generally apply it toChange.ofJsonUpsert()
.This is a special case where a web application sends a stringfied JSON content after editing to the server.