Uploaded image for project: 'Mule'
  1. Mule
  2. MULE-10716

object-to-json-transformer transforms date to millis instead of string

    Details

    • Type: Bug
    • Status: Open
    • Priority: To be reviewed
    • Resolution: Unresolved
    • Affects Version/s: 3.8.1
    • Fix Version/s: None
    • Component/s: Module: JSON
    • Labels:
      None
    • Configuration:
      Hide

      <?xml version="1.0" encoding="UTF-8"?>

      <mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
      http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
      http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
      http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
      http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
      <http:request-config name="HTTP_Request_Configuration" host="localhost" port="8080" basePath="/api" doc:name="HTTP Request Configuration">
      <http:raml-api-configuration location="to-json-fails-with-date-and-apikit.raml"/>
      </http:request-config>
      <flow name="pollFlow-dw">
      <poll doc:name="Poll1">
      <fixed-frequency-scheduler frequency="1000" startDelay="1" timeUnit="SECONDS"/>
      <logger message="poll" level="INFO" category="dw" doc:name="Logger"/>
      </poll>
      <custom-transformer class="com.canda.mule.TestTransformer" doc:name="Java"/>
      <dw:transform-message doc:name="Transform Message">
      <dw:set-payload><![CDATA[%dw 1.0
      %output application/json

      payload]]></dw:set-payload>
      </dw:transform-message>
      <object-to-string-transformer doc:name="Object to String"/>
      <logger message="Before http=#[payload]" level="INFO" category="dw" doc:name="Logger"/>
      <http:request config-ref="HTTP_Request_Configuration" path="/push" method="POST" doc:name="HTTP-to-other-mule-app (same now for test-app)"/>
      <object-to-string-transformer doc:name="Object to String"/>
      <logger message="After http=#[payload]" level="INFO" category="dw" doc:name="Logger"/>
      </flow>
      <flow name="pollFlow-object-to-json">
      <poll doc:name="Poll2">
      <fixed-frequency-scheduler frequency="1000" startDelay="10" timeUnit="SECONDS"/>
      <logger message="poll" level="INFO" doc:name="Logger" category="object-to-json"/>
      </poll>
      <custom-transformer class="com.canda.mule.TestTransformer" doc:name="Java"/>
      <json:object-to-json-transformer doc:name="Object to JSON"/>
      <object-to-string-transformer doc:name="Object to String"/>
      <logger message="Before http=#[payload]" level="INFO" doc:name="Logger" category="object-to-json"/>
      <http:request config-ref="HTTP_Request_Configuration" path="/push" method="POST" doc:name="HTTP-to-other-mule-app (same now for test-app)"/>
      <object-to-string-transformer doc:name="Object to String"/>
      <logger message="After http=#[payload]" level="INFO" doc:name="Logger" category="object-to-json"/>
      </flow>

      </mule>

      Show
      <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd "> <http:request-config name="HTTP_Request_Configuration" host="localhost" port="8080" basePath="/api" doc:name="HTTP Request Configuration"> <http:raml-api-configuration location="to-json-fails-with-date-and-apikit.raml"/> </http:request-config> <flow name="pollFlow-dw"> <poll doc:name="Poll1"> <fixed-frequency-scheduler frequency="1000" startDelay="1" timeUnit="SECONDS"/> <logger message="poll" level="INFO" category="dw" doc:name="Logger"/> </poll> <custom-transformer class="com.canda.mule.TestTransformer" doc:name="Java"/> <dw:transform-message doc:name="Transform Message"> <dw:set-payload><![CDATA[%dw 1.0 %output application/json — payload]]></dw:set-payload> </dw:transform-message> <object-to-string-transformer doc:name="Object to String"/> <logger message="Before http=# [payload] " level="INFO" category="dw" doc:name="Logger"/> <http:request config-ref="HTTP_Request_Configuration" path="/push" method="POST" doc:name="HTTP-to-other-mule-app (same now for test-app)"/> <object-to-string-transformer doc:name="Object to String"/> <logger message="After http=# [payload] " level="INFO" category="dw" doc:name="Logger"/> </flow> <flow name="pollFlow-object-to-json"> <poll doc:name="Poll2"> <fixed-frequency-scheduler frequency="1000" startDelay="10" timeUnit="SECONDS"/> <logger message="poll" level="INFO" doc:name="Logger" category="object-to-json"/> </poll> <custom-transformer class="com.canda.mule.TestTransformer" doc:name="Java"/> <json:object-to-json-transformer doc:name="Object to JSON"/> <object-to-string-transformer doc:name="Object to String"/> <logger message="Before http=# [payload] " level="INFO" doc:name="Logger" category="object-to-json"/> <http:request config-ref="HTTP_Request_Configuration" path="/push" method="POST" doc:name="HTTP-to-other-mule-app (same now for test-app)"/> <object-to-string-transformer doc:name="Object to String"/> <logger message="After http=# [payload] " level="INFO" doc:name="Logger" category="object-to-json"/> </flow> </mule>
    • Log Output:
      Hide
      INFO 2016-10-06 16:50:40,158 [pool-24-thread-1] dw: poll
      INFO 2016-10-06 16:50:40,850 [[to-json-fails-with-date-and-apikit].pollFlow-dw.stage1.02] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java.
      INFO 2016-10-06 16:50:42,575 [[to-json-fails-with-date-and-apikit].pollFlow-dw.stage1.02] dw: Before http={
        "qualityAssuranceDate": "1970-01-01T01:00:00"
      }
      INFO 2016-10-06 16:50:43,800 [[to-json-fails-with-date-and-apikit].pollFlow-dw.stage1.02] dw: After http={
        "qualityAssuranceDate": "1970-01-01T01:00:00"
      }
      INFO 2016-10-06 16:50:49,134 [pool-25-thread-1] object-to-json: poll
      INFO 2016-10-06 16:50:49,237 [[to-json-fails-with-date-and-apikit].pollFlow-object-to-json.stage1.02] object-to-json: Before http={"quality-assurance-date":15}
      INFO 2016-10-06 16:50:49,260 [[to-json-fails-with-date-and-apikit].to-json-fails-with-date-and-apikit-httpListenerConfig.worker.01] org.mule.module.apikit.validation.RestJsonSchemaValidator: Schema validation failed: instance type (integer) does not match any allowed primitive type (allowed: ["string"])
      ERROR 2016-10-06 16:50:49,292 [[to-json-fails-with-date-and-apikit].to-json-fails-with-date-and-apikit-httpListenerConfig.worker.01] org.mule.module.apikit.MappingExceptionListener:
      ********************************************************************************
      Message : instance type (integer) does not match any allowed primitive type (allowed: ["string"])
      Payload : java.io.ByteArrayInputStream@7a5d4029
      FlowStack : at to-json-fails-with-date-and-apikit-main(/to-json-fails-with-date-and-apikit-main/processors/0 @ to-json-fails-with-date-and-apikit:to-json-fails-with-date-and-apikit.xml:10 (APIkit Router))
      Element : /to-json-fails-with-date-and-apikit-main/processors/0 @ to-json-fails-with-date-and-apikit:to-json-fails-with-date-and-apikit.xml:10 (APIkit Router)
      Element XML : <apikit:router xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" config-ref="to-json-fails-with-date-and-apikit-config" doc:name="APIkit Router"></apikit:router>
      --------------------------------------------------------------------------------
      Root Exception stack trace:
      org.mule.module.apikit.exception.BadRequestException: instance type (integer) does not match any allowed primitive type (allowed: ["string"])
      at org.mule.module.apikit.validation.RestJsonSchemaValidator.validate(RestJsonSchemaValidator.java:86)
      at org.mule.module.apikit.HttpRestRequest.validateSchema(HttpRestRequest.java:569)
      at org.mule.module.apikit.HttpRestRequest.validateBody(HttpRestRequest.java:352)
      at org.mule.module.apikit.HttpRestRequest.negotiateInputRepresentation(HttpRestRequest.java:323)
      at org.mule.module.apikit.HttpRestRequest.validate(HttpRestRequest.java:124)
      at org.mule.module.apikit.AbstractRouter.processRouterRequest(AbstractRouter.java:187)
      at org.mule.module.apikit.AbstractRouter.processBlockingRequest(AbstractRouter.java:83)
      at org.mule.module.apikit.AbstractRouter.processBlocking(AbstractRouter.java:77)
      Show
      INFO 2016-10-06 16:50:40,158 [pool-24-thread-1] dw: poll INFO 2016-10-06 16:50:40,850 [[to-json-fails-with-date-and-apikit].pollFlow-dw.stage1.02] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java. INFO 2016-10-06 16:50:42,575 [[to-json-fails-with-date-and-apikit].pollFlow-dw.stage1.02] dw: Before http={   "qualityAssuranceDate": "1970-01-01T01:00:00" } INFO 2016-10-06 16:50:43,800 [[to-json-fails-with-date-and-apikit].pollFlow-dw.stage1.02] dw: After http={   "qualityAssuranceDate": "1970-01-01T01:00:00" } INFO 2016-10-06 16:50:49,134 [pool-25-thread-1] object-to-json: poll INFO 2016-10-06 16:50:49,237 [[to-json-fails-with-date-and-apikit].pollFlow-object-to-json.stage1.02] object-to-json: Before http={"quality-assurance-date":15} INFO 2016-10-06 16:50:49,260 [[to-json-fails-with-date-and-apikit].to-json-fails-with-date-and-apikit-httpListenerConfig.worker.01] org.mule.module.apikit.validation.RestJsonSchemaValidator: Schema validation failed: instance type (integer) does not match any allowed primitive type (allowed: ["string"]) ERROR 2016-10-06 16:50:49,292 [[to-json-fails-with-date-and-apikit].to-json-fails-with-date-and-apikit-httpListenerConfig.worker.01] org.mule.module.apikit.MappingExceptionListener: ******************************************************************************** Message : instance type (integer) does not match any allowed primitive type (allowed: ["string"]) Payload : java.io.ByteArrayInputStream@7a5d4029 FlowStack : at to-json-fails-with-date-and-apikit-main(/to-json-fails-with-date-and-apikit-main/processors/0 @ to-json-fails-with-date-and-apikit:to-json-fails-with-date-and-apikit.xml:10 (APIkit Router)) Element : /to-json-fails-with-date-and-apikit-main/processors/0 @ to-json-fails-with-date-and-apikit:to-json-fails-with-date-and-apikit.xml:10 (APIkit Router) Element XML : <apikit:router xmlns:apikit=" http://www.mulesoft.org/schema/mule/apikit " config-ref="to-json-fails-with-date-and-apikit-config" doc:name="APIkit Router"></apikit:router> -------------------------------------------------------------------------------- Root Exception stack trace: org.mule.module.apikit.exception.BadRequestException: instance type (integer) does not match any allowed primitive type (allowed: ["string"]) at org.mule.module.apikit.validation.RestJsonSchemaValidator.validate(RestJsonSchemaValidator.java:86) at org.mule.module.apikit.HttpRestRequest.validateSchema(HttpRestRequest.java:569) at org.mule.module.apikit.HttpRestRequest.validateBody(HttpRestRequest.java:352) at org.mule.module.apikit.HttpRestRequest.negotiateInputRepresentation(HttpRestRequest.java:323) at org.mule.module.apikit.HttpRestRequest.validate(HttpRestRequest.java:124) at org.mule.module.apikit.AbstractRouter.processRouterRequest(AbstractRouter.java:187) at org.mule.module.apikit.AbstractRouter.processBlockingRequest(AbstractRouter.java:83) at org.mule.module.apikit.AbstractRouter.processBlocking(AbstractRouter.java:77)

      Description

      As

       <json:object-to-json-transformer doc:name="Object to JSON"/>
      

      uses the default Json ObjectMapper all Dates coming out of Java Objects:

          @JsonProperty("quality-assurance-date")
          private Date qualityAssuranceDate;
      

      are serialized as numbers.
      (Because org.codehaus.jackson.map.SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS defaults to true)

      As "normally" Json expect a date to be

      		"quality-assurance-date": {
      			"type": "string",
      			"format": "date-time"
      		}
      

      a follow up API Kit Router fails with

      org.mule.module.apikit.exception.BadRequestException: instance type (integer) does not match any allowed primitive type (allowed: ["string"])
      	at org.mule.module.apikit.validation.RestJsonSchemaValidator.validate(RestJsonSchemaValidator.java:86)
      

      See attached project.

      Workaround 1: (EE only: Use DataWeave).

      Workaround 2: Use Java Class and implement ObjectMapper as needed.
      (not the "mule-way").

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              C-A-StefanCordes Stefan Cordes
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:

                Zendesk Support

                  Backbone Issue Sync

                  • It looks like Backbone Issue Sync is not enabled for your project.