You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by tomstark <to...@polycom.com> on 2013/10/09 19:42:00 UTC

Can unordered JSON work with XML sequence with validation

Hello,

We have a REST API that was originally developed using XSDs with CXF 2.5.2
using XML as the payload.  To make the API flexible, objects are created
with a <xs:any> array at the end of each object to allow for backward
compatibility.  Here is a schema example:

  <xs:complexType name="someType">
    <xs:sequence>
       <xs:element name="someInt"  type="xs:int">
        </xs:element>
        <xs:any minOccurs="0" maxOccurs="unbounded"
              namespace="##other" processContents="lax"/>
    </xs:sequence>
  </xs:complexType>

We would like to add JSON support to the same API but have a problem in that
the object definitions in XSD use xs:sequence thus forcing the JSON to be
ordered, which is not guaranteed from the client.

I have researched what else can be done from the XSD and unfortunately, the
<xs:all> cannot be used in place of the <xs:sequence> since it doesn't allow
the <xs:any> element.

I would really like schema validation to be turned on to protect the server
but this seems to be incompatible with allowing unordered JSON.

In our case, the order of construction of the POJOs is not important but
rather element types and restrictions are important.  

Since it does not appear to be possible to solve this problem from a schema
standpoint, is it a bad idea to have the schema validation ignore sequencing
only?

Currently, I have upgraded to CXF 2.7.5, with Jettison 1.3.3.

Looking for some suggestions of how to resolve this issue or is this a case
of trying to make a round peg fit a square hole?

Any help is greatly appreciated.

Thanks,

Tom



--
View this message in context: http://cxf.547215.n5.nabble.com/Can-unordered-JSON-work-with-XML-sequence-with-validation-tp5734874.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Can unordered JSON work with XML sequence with validation

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi


On 09/10/13 18:42, tomstark wrote:
> Hello,
>
> We have a REST API that was originally developed using XSDs with CXF 2.5.2
> using XML as the payload.  To make the API flexible, objects are created
> with a <xs:any> array at the end of each object to allow for backward
> compatibility.  Here is a schema example:
>
>    <xs:complexType name="someType">
>      <xs:sequence>
>         <xs:element name="someInt"  type="xs:int">
>          </xs:element>
>          <xs:any minOccurs="0" maxOccurs="unbounded"
>                namespace="##other" processContents="lax"/>
>      </xs:sequence>
>    </xs:complexType>
>
> We would like to add JSON support to the same API but have a problem in that
> the object definitions in XSD use xs:sequence thus forcing the JSON to be
> ordered, which is not guaranteed from the client.

Do you mean it is not guaranteed from JSON client ? If so, do you mean 
that a new content covered by xsd:any can get in front of someInt ?

>
> I have researched what else can be done from the XSD and unfortunately, the
> <xs:all> cannot be used in place of the <xs:sequence> since it doesn't allow
> the <xs:any> element.
>
> I would really like schema validation to be turned on to protect the server
> but this seems to be incompatible with allowing unordered JSON.
>
> In our case, the order of construction of the POJOs is not important but
> rather element types and restrictions are important.
>
> Since it does not appear to be possible to solve this problem from a schema
> standpoint, is it a bad idea to have the schema validation ignore sequencing
> only?
>
> Currently, I have upgraded to CXF 2.7.5, with Jettison 1.3.3.
>

well, assuming you'd like to support JSON clients which are not 
guaranteed to send JSON with some specific order in place (and 
specifically, they can add the extension data in front of the payloads, 
etc), then what you can try to do is have a schema added to JSONProvider 
which will not allow the extensions, so that the schema validates for a 
very specific content only, like someInt, and then also configure 
JSONProvider to drop the extension data which is known to exist, see

http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-CustomizingJAXBXMLandJSONinputandoutput

(inDropElements map property)

Not sure if it will help in your case though...

Sergey


> Looking for some suggestions of how to resolve this issue or is this a case
> of trying to make a round peg fit a square hole?
>
> Any help is greatly appreciated.
>
> Thanks,
>
> Tom
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Can-unordered-JSON-work-with-XML-sequence-with-validation-tp5734874.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

Re: Can unordered JSON work with XML sequence with validation

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 09/10/13 22:32, tomstark wrote:
> Hi Sergey,
>
> Answers to some of your questions:
>> Do you mean it is not guaranteed from JSON client ? If so, do you mean
>> that a new content covered by xsd:any can get in front of someInt ?
>
> The order of the elements from the JSON from the client is not guaranteed to
> match that of the schema.  The schema example was overly simplified to a
> single field.  A better example would have been to have multiple elements
> along with the xs:any.  In that case, unordered JSON violates the schema and
> is rejected.
>
> In general, the xs:any is in place for clients or servers to be able to
> interact with the other side (server or client, respectively) without having
> to be on the exact version of the API.  Typically, this is useful when
> adding additional fields to the end of a sequence definition for a
> particular object.
>
> Does that affect the answer you gave me regarding modifying the JSON
> provider?
>
Not really, the last suggestion re using xsd:all on JSON path (you can 
explicitly configure JSONProvider with the list of schemas) and dropping 
the extension elements should do, because all xs:any allows is not 
validating but accepting the extra bits with the older data containers, 
so this should work dropping the extensions via inDropElements

Cheers, Sergey

> (I'm looking into your answer now)
>
> Thanks,
> Tom
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Can-unordered-JSON-work-with-XML-sequence-with-validation-tp5734874p5734878.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: Can unordered JSON work with XML sequence with validation

Posted by tomstark <to...@polycom.com>.
Hi Sergey,

Answers to some of your questions:
> Do you mean it is not guaranteed from JSON client ? If so, do you mean
> that a new content covered by xsd:any can get in front of someInt ? 

The order of the elements from the JSON from the client is not guaranteed to
match that of the schema.  The schema example was overly simplified to a
single field.  A better example would have been to have multiple elements
along with the xs:any.  In that case, unordered JSON violates the schema and
is rejected.

In general, the xs:any is in place for clients or servers to be able to
interact with the other side (server or client, respectively) without having
to be on the exact version of the API.  Typically, this is useful when
adding additional fields to the end of a sequence definition for a
particular object.

Does that affect the answer you gave me regarding modifying the JSON
provider?

(I'm looking into your answer now)

Thanks,
Tom



--
View this message in context: http://cxf.547215.n5.nabble.com/Can-unordered-JSON-work-with-XML-sequence-with-validation-tp5734874p5734878.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Can unordered JSON work with XML sequence with validation

Posted by Sergey Beryozkin <sb...@gmail.com>.
Or use xsd:all variant on JSON path if everything can be unordered, but 
drop the new content with inDropElements, if the new content is limited 
then it is doable, if it is completely dynamic then probably not

Sergey
On 09/10/13 21:20, Sergey Beryozkin wrote:
> Hi
>
>
> On 09/10/13 18:42, tomstark wrote:
>> Hello,
>>
>> We have a REST API that was originally developed using XSDs with CXF
>> 2.5.2
>> using XML as the payload.  To make the API flexible, objects are created
>> with a <xs:any> array at the end of each object to allow for backward
>> compatibility.  Here is a schema example:
>>
>>    <xs:complexType name="someType">
>>      <xs:sequence>
>>         <xs:element name="someInt"  type="xs:int">
>>          </xs:element>
>>          <xs:any minOccurs="0" maxOccurs="unbounded"
>>                namespace="##other" processContents="lax"/>
>>      </xs:sequence>
>>    </xs:complexType>
>>
>> We would like to add JSON support to the same API but have a problem
>> in that
>> the object definitions in XSD use xs:sequence thus forcing the JSON to be
>> ordered, which is not guaranteed from the client.
>
> Do you mean it is not guaranteed from JSON client ?
>
>>
>> I have researched what else can be done from the XSD and
>> unfortunately, the
>> <xs:all> cannot be used in place of the <xs:sequence> since it doesn't
>> allow
>> the <xs:any> element.
>>
>> I would really like schema validation to be turned on to protect the
>> server
>> but this seems to be incompatible with allowing unordered JSON.
>>
>> In our case, the order of construction of the POJOs is not important but
>> rather element types and restrictions are important.
>>
>> Since it does not appear to be possible to solve this problem from a
>> schema
>> standpoint, is it a bad idea to have the schema validation ignore
>> sequencing
>> only?
>>
>> Currently, I have upgraded to CXF 2.7.5, with Jettison 1.3.3.
>>
>
> well, assuming you'd like to support JSON clients which are not
> guaranteed to send JSON with some specific order in place, then what you
> can do is have a schema added to JSONProvider which will not allow
>
>
>> Looking for some suggestions of how to resolve this issue or is this a
>> case
>> of trying to make a round peg fit a square hole?
>>
>> Any help is greatly appreciated.
>>
>> Thanks,
>>
>> Tom
>>
>>
>>
>> --
>> View this message in context:
>> http://cxf.547215.n5.nabble.com/Can-unordered-JSON-work-with-XML-sequence-with-validation-tp5734874.html
>>
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>