Alexey Dolgopolov
Alexey Dolgopolov

Reputation: 700

wso2 micro integrator fails to validate with json schema

I'm trying to validate message with json schema in WSO2 micro integrator 1.2.0.

<validate cache-schema="true">
    <schema key="conf:schema/eip_dit_oko_jsonschema_stage_0_input_params.json"/>
    <on-fail>
        <payloadFactory media-type="json">
            <format>{"Error":"$1","Error Details":"$2"}</format>
            <args>
                <arg evaluator="xml" expression="$ctx:ERROR_MESSAGE"/>
                <arg evaluator="xml" expression="$ctx:ERROR_DETAIL"/>
            </args>
        </payloadFactory>
        <property name="HTTP_SC" scope="axis2" type="STRING" value="500"/>
       <respond/>
    </on-fail>
</validate>

If the schema file is in registry

       <item>
            <file>eip_dit_oko_jsonschema_stage_0_input_params.json</file>
            <path>/_system/config/schema</path>
            <mediaType>application/json</mediaType>
            <properties/>
        </item>

then sequence fails with

[2022-11-22 21:14:33,492] ERROR {ValidateMediator} - {api:eip_dit_oko_api_stage_0} Error creating a new schema objects for schemas : [Value {name ='null', keyValue ='conf:schema/eip_dit_oko_jsonschema_stage_0_input_params.json'}] org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.jaxp.validation.Util.toSAXParseException(Util.java:74)
    at com.sun.org.apache.xerces.internal.jaxp.validation.Util.toSAXException(Util.java:62)
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:258)
    at org.apache.synapse.mediators.builtin.ValidateMediator.mediate(ValidateMediator.java:429)
...

Obviously, integrator tries to read json-schema file as xml.

If I try to follow this answer about using local entry instead of registry

<validate cache-schema="true">
    <schema key="eip_dit_oko_jsonschema_stage_0_input_params"/>
...
</validate>
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="eip_dit_oko_jsonschema_stage_0_input_params" xmlns="http://ws.apache.org/ns/synapse"><![CDATA[{    "$schema": "http://json-schema.org/draft-04/schema",    "id": "http://example.com/example.json",    "type": "object",    "title": "The root schema",    "required": [        "getData"    ],    "properties": {        "getData": {            "id": "#getData",            "type": "object",            "title": "The getData schema",            "required": [                "p_limit",                "p_offset"            ],            "properties": {                "p_limit": {                    "id": "#p_limit",                    "type": "integer",                    "title": "The p_limit schema"                },                "p_offset": {                    "id": "#p_offset",                    "type": "integer",                    "title": "The p_offset schema"                },                "p_defect_text": {    "id": "#/properties/defect_text",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }                },                "p_district_code": {                    "id": "#p_district_code",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "integer"    }                },                "p_okrug_code": {                    "id": "#p_okrug_code",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "integer"    }                },                "p_status": {                    "id": "#p_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }                },                "p_sys_status": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }                },                "p_ticket": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }                },                "p_season": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }                },                "p_critical": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }                },               "p_owner_name": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }               },               "p_address": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }               },               "p_address_like": {                    "id": "#p_address_like",                    "type": "string"               },"p_id_object": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "integer"    }                },               "p_id_300": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }               },               "p_type_object": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }               },               "p_id_systems": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }               },               "p_defect_el1": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }               },               "p_defect_el": {                    "id": "#p_sys_status",    "type": "array",    "items": {"id": "#/properties/defect_text/items","type": "string"    }               },               "p_sys_sla": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_sys_sla_from": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_sys_sla_to": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_data_creation_from": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_data_creation_to": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_view_date_from_from": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_view_date_to_to": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_view_date_from_to": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_view_date_to_from": {                    "id": "#p_sys_status",                    "type": "string",    "format":"date-time",                    "title": "The p_sys_status schema"               },               "p_deadline": {                    "id": "#p_sys_status",                    "type": "number",                    "title": "The p_sys_status schema"               }            }        }    }}]]></localEntry>

then mediation fails as this

[2022-11-22 16:13:17,517]  WARN {SynapseConfigUtils} - Cannot convert object to a StreamSource

EDIT Request:

curl -v http://localhost:8290/api/stage -H 'Content-Type: application/json' -d '{"getData": {"p_season": ["winter"], "p_limit": 10, "p_offset": 0}}'

Upvotes: 1

Views: 282

Answers (1)

ycr
ycr

Reputation: 14584

One reason for the issue is that the Payload you are sending is not a JSON or you are not sending the Content-Type: application/json header. But I would assume if you are sending the incorrect content type it would fail before reaching the validate medaitor. So my guess is that you are not sending any Payload with the request at all. Are you trying to test this with a GET request? As per the code if you don't send a JSON payload it will go down the XML path which can cause the issue you are facing, hence make sure you send a valid JSON payload with your request.

Upvotes: 1

Related Questions