I am facing one issue. The application is Kotlin +...
# jackson-kotlin
h
I am facing one issue. The application is Kotlin + Java. I am calling a Kotlin REST Resource passing the escaped backslash in the payload. but I am getting the below error:
Unable to Parse JSON.
Request Payload:
Copy code
{
  "eventTypeId": "USR-05 \" "
}
REST Controller
Copy code
@Produces(MediaType.APPLICATION_JSON)
@Path("/audit/api/1/events")
open class WriteAuditEventResource {
	
	@POST
    @Consumes(MediaType.APPLICATION_JSON)
    fun writeEvent(eventRequest: AuditEventRequest): Response {
        return Response.status(Response.Status.CREATED).build()
    }

}
Problem: 1. In HTTP Logs I can see that the escaped backslash is converted to double backslash (making the JSON invalid). Below are the logs:
Copy code
{
    "eventTypeId": "USR-05 \\" "
}
Copy code
[o.e.jetty.http.HttpParser     ] - parseNext s=CONTENT HeapByteBuffer@64870bee[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n    "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00} 
[o.e.jetty.server.HttpChannel  ] - onContent HttpChannelOverHttp@f084976{s=HttpChannelState@4962b62c{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=1},r=1,c=false/false,a=HANDLING,uri=//localhost:8686/audit/api/1/events/,age=35} Content@2bcdd012{HeapByteBufferR@793bf03f[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n    "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}} 
[o.e.jetty.server.HttpInput    ] - HttpInputOverHTTP@3716942e[c=0,q=0,[0]=null,s=STREAM] addContent Content@2bcdd012{HeapByteBufferR@793bf03f[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n    "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}} 
[o.e.jetty.http.HttpParser     ] - CONTENT --> CONTENT_END 
[o.e.jetty.server.HttpChannel  ] - onContentComplete HttpChannelOverHttp@f084976{s=HttpChannelState@4962b62c{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=1},r=1,c=false/false,a=HANDLING,uri=//localhost:8686/audit/api/1/events/,age=36} 
[o.e.jetty.http.HttpParser     ] - CONTENT_END --> END
2. In Java Servlet Filter, if I extract the request payload from HttpServlet then I am getting below string as input:
Copy code
{
    "eventTypeId": "USR-05 " "
}
StackTrace:
Copy code
[.JsonProcessingExceptionMapper] - Unable to process JSON ! com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
!  at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: *, column: ***] (through reference chain: com.anaplan.audit.write.api.request.AuditEventRequest["eventTypeId"])
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:402)
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:361)
! at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1826)
! at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:280)
! at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserialize(SuperSonicBeanDeserializer.java:155)
! Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
!  at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 395]
! at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2418)
! at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:749)
! at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:673)
! at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:1061)
! at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:608)
More Details:
Copy code
Kotlin Version: 1.3.0
JDK version: 1.8
jackson-module-kotlin: 2.8.9
t
does it work with jackson if you do it with just java?
h
Yes this is working in Java Spring Boot application
t
interesting...i wouldn't have expected kotlin to affect how a string is written to json
h
Any suggestion for the solution?
t
sorry - haven't had a chance to dig on what a solution might be. i'll see if i can find some time this week
h
thanks
t
ok, finally got a chance to actually look into it. i can't reproduce it at all. it's mostly likely related to the fact that you are using a jvm that was EOL'd 6 years ago, a kotlin version from 7 years ago, and a jackson version from 8 years ago. to be honest, you are running so far behind that's it's wildly unreasonable to expect anyone to really do anything about it if it is in fact a problem for that confluence of ancient versions.
i doubt the java version is really relevant here, it's most likely a combination of the kotlin and jackson versions. update those to something more recent and see if you still have the same problem
using kotlin 2.1, jackson 2.18, and java 17/21, when i run `
Copy code
@Test
fun `do the thing`() {
  data class Test(val str: String)
  val mapper = jacksonObjectMapper()
  print(mapper.writeValueAsString(Test("escaped \" quote \" ")))
}
i get
Copy code
{
  "str": "escaped \" quote \" "
}
which is exactly what it should be
h
Hi... Yes, you are absolutely right that the versions are way too old. but if I use your test, it is working fine in my code too... the issue is when the request is coming from a client on Kotlin resource... This is the issue I am facing... I will also try to replicate this using a unit test case.
t
if the value serialized with jackson is correct, then jackson probably isn't the problem. your snippets seem to show that something is putting an extra slash in somewhere, but i'm pretty sure it's not jackson that's the culprit
h
Yes... I found the issue. there was one filter (I was unaware) that was sanitizing the request payload and making this invalid. Thanks for all the help