Skip to content

modifyUris may corrupt the request or response body if it contains a URI without a port and a colon later in the content #790

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
alklid opened this issue Mar 23, 2022 · 5 comments
Labels
Milestone

Comments

@alklid
Copy link

alklid commented Mar 23, 2022

When modifyUris() is used, the URL data in the request body is wrong converted, and PayloadHandlingException is thrown.

[Fatal Error] :1:1: Content is not allowed in prolog.
Cannot handle application/json content as it could not be parsed as JSON or XML
org.springframework.restdocs.payload.PayloadHandlingException: Cannot handle application/json content as it could not be parsed as JSON or XML
    at app//org.springframework.restdocs.payload.ContentHandler.forContentWithDescriptors(ContentHandler.java:69)
    at app//org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:157)
    at app//org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:78)
    at app//org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:191)
    at app//org.springframework.restdocs.mockmvc.RestDocumentationResultHandler$1.handle(RestDocumentationResultHandler.java:77)

my preprocessRequest config

String host = "dev-sample.sample.com"
preprocessRequest(
    prettyPrint(),
    modifyUris().scheme("https").host(host).removePort()
)

my requestbody data

{
  "name": "sample",
  "bannerImageFile": {
    "fileId": 1,
    "name": "image.png",
    "mimeType": "image/png",
    "size": 1024
    "linkUrl": "https://dev-test.test.com"
  },
  "targetUserFile": {
    "fileId": 2,
    "name": "example.csv",
    "mimeType": "text/csv",
    "size": 1024,
    "linkUrl": ""
  }
}

requestbody data after modifyUris()

{
  "name": "sample",
  "bannerImageFile" : {
    "fileId" : 1,
    "name": "image.png",
    "mimeType": "image/png",
    "size" : 1024,
    "uploadStatus" : 2,
    "linkUrl" : "https://dev-sample.sample.com: {
    "fileId" : 2,
    "name" : "example.csv",
    "mimeType" : "text/csv",
    "size" : 1024,
    "linkUrl" : ""
  }
}

I want the modifyUris() setting to only apply to the host part and not to the request body data.

@wilkinsona
Copy link
Member

If you don’t want the body to be changed, you are using the wrong tool for the job.

Which testing client are you using to document your API? If you are using MockMvc, you should configure the URIs up front rather than modifying the requests and responses once they’ve been sent and received.

@wilkinsona wilkinsona added the status: waiting-for-feedback Feedback is required before progress can be made label Mar 23, 2022
@alklid
Copy link
Author

alklid commented Mar 23, 2022

It worked! Thank you.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback Feedback is required before progress can be made labels Mar 23, 2022
@wilkinsona
Copy link
Member

Great. Thanks for letting me know. There is still a bug here, though, as the JSON has been corrupted.

@wilkinsona wilkinsona changed the title When modifyUris() is used, the URL data in the request body is wrong converted modifyUris may corrupt the request or response body if it contains a URI without a port and a colon later in the content Mar 24, 2022
@wilkinsona wilkinsona added type: bug A bug and removed status: waiting-for-triage Untriaged issue status: feedback-provided Feedback has been provided labels Mar 24, 2022
@wilkinsona wilkinsona added this to the 2.0.7.RELEASE milestone Mar 24, 2022
@kubav182
Copy link

@wilkinsona still not working for the latest version 3.0.1

withRequestDefaults(prettyPrint(), modifyUris().scheme("https").host("www.example.com").port(443))

I'm using restdocs with restassured and when there is url in body I'm getting this error.

[Fatal Error] :1:2: The markup in the document preceding the root element must be well-formed.

Cannot handle application/json;charset=UTF-8 content as it could not be parsed as JSON or XML
org.springframework.restdocs.payload.PayloadHandlingException: Cannot handle application/json;charset=UTF-8 content as it could not be parsed as JSON or XML

@wilkinsona
Copy link
Member

@kubav182 please open a new issue with a minimal sample that reproduces the problem and we can investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants