Hi Shameera,
Since I'm in dev service, didn't have much time to work on this. I have
created unit test cases for this scenario and attached the new diff.
Thanks,
On Mon, Sep 29, 2014 at 7:16 AM, Shameera Rathnayaka <[email protected]
> wrote:
> Hi Rajith,
>
> Unfortunately, for me there is no way to test above patch, if you could
> attach a patch for current axis2 trunk then I can have a look. I tried with
> the attached patch with axis2 trunk it gave me conflicts. And it is good to
> add an integration test to cover this improvement then we won't break this
> in future.
>
> Regards,
> Shameera.
>
> On Mon, Sep 29, 2014 at 12:11 AM, Rajith Vitharana <[email protected]>
> wrote:
>
>> ping
>>
>> On Tue, Sep 23, 2014 at 3:12 PM, Rajith Vitharana <[email protected]>
>> wrote:
>>
>>> Hi All,
>>>
>>> Regarding the jira [1].
>>> In DSS when using json output mapping we can filter out the output
>>> fields according to the person's role who invokes the service. If he have
>>> permission, those fields will be visible to them. If he doesn't have
>>> permission, only other fields will be visible to him. This is explained in
>>> [2]
>>>
>>> But as the jira says, this feature doesn't work in standalone pack.
>>> In wsdl file of the data service, these fields are marked as optional by
>>> adding 'minOccurs="0"' attribute. But the "GsonXMLStreamWriter.java" class
>>> in "axis2-json" module doesn't consider this "minOccurs" attribute when
>>> converting the xml output to json. This causes the error mentioned in the
>>> jira [1].
>>> I have changed the source to avoid this situation and consider the
>>> "minOccurs" attribute when doing the conversion part. Is this the better
>>> way of resolving this problem? If so please review the code and commit to
>>> kernel patch 009.
>>>
>>> [1] - https://wso2.org/jira/browse/DS-963
>>> [2] - https://docs.wso2.com/display/DSS321/JSON+Mapping
>>>
>>> Thanks
>>>
>>> --
>>> Rajith Vitharana
>>>
>>> Software Engineer,
>>> WSO2 Inc. : wso2.com
>>> Mobile : +94715883223
>>> Blog : http://lankavitharana.blogspot.com/
>>>
>>
>>
>>
>> --
>> Rajith Vitharana
>>
>> Software Engineer,
>> WSO2 Inc. : wso2.com
>> Mobile : +94715883223
>> Blog : http://lankavitharana.blogspot.com/
>>
>
>
>
> --
> Best Regards,
> Shameera Rathnayaka.
>
> email: shameera AT apache.org , shameerainfo AT gmail.com
> Blog : http://shameerarathnayaka.blogspot.com/
>
--
Rajith Vitharana
Software Engineer,
WSO2 Inc. : wso2.com
Mobile : +94715883223
Blog : http://lankavitharana.blogspot.com/
Index: src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java
===================================================================
--- src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java (revision
207992)
+++ src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java (working copy)
@@ -131,7 +131,11 @@
private void writeStartJson(JsonObject jsonObject) throws IOException {
if (jsonObject.getType() == JSONType.OBJECT) {
- jsonWriter.name(jsonObject.getName());
+ try {
+ jsonWriter.name(jsonObject.getName());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
} else if (jsonObject.getType() == JSONType.ARRAY) {
jsonWriter.name(jsonObject.getName());
jsonWriter.beginArray();
@@ -208,6 +212,11 @@
if (miniStack.isEmpty()) {
if (!queue.isEmpty()) {
JsonObject queObj = queue.peek();
+ if (!queObj.getName().equals(localName) &&
queObj.getMinOccurs() == 0
+ && (stack.isEmpty() ||
!stack.peek().getName().equals(localName))){
+ queue.poll();
+ queObj = queue.peek();
+ }
if (queObj.getName().equals(localName)) {
if (flushObject != null) {
if (topNestedArrayObj != null &&
flushObject.getType() == JSONType.NESTED_ARRAY
Index: src/org/apache/axis2/json/gson/factory/JsonObject.java
===================================================================
--- src/org/apache/axis2/json/gson/factory/JsonObject.java (revision
207992)
+++ src/org/apache/axis2/json/gson/factory/JsonObject.java (working copy)
@@ -25,6 +25,7 @@
private JSONType type;
private String valueType;
private String namespaceUri;
+ private long minOccurs;
public JsonObject(String name, JSONType type, String valueType , String
namespaceUri) {
this.name = name;
@@ -48,4 +49,12 @@
public String getNamespaceUri() {
return namespaceUri;
}
+
+ public void setMinOccurs(long minOccurs) {
+ this.minOccurs = minOccurs;
+ }
+
+ public long getMinOccurs() {
+ return minOccurs;
+ }
}
Index: src/org/apache/axis2/json/gson/factory/XmlNode.java
===================================================================
--- src/org/apache/axis2/json/gson/factory/XmlNode.java (revision 207992)
+++ src/org/apache/axis2/json/gson/factory/XmlNode.java (working copy)
@@ -30,6 +30,7 @@
private List<XmlNode> childrenList = new ArrayList<XmlNode>();
private String valueType;
private String namespaceUri;
+ private long minOccurs;
public XmlNode(String name,String namespaceUri, boolean attribute, boolean
array , String valueType) {
this.name = name;
@@ -68,4 +69,12 @@
public String getNamespaceUri() {
return namespaceUri;
}
+
+ public void setMinOccurs(long minOccurs) {
+ this.minOccurs = minOccurs;
+ }
+
+ public long getMinOccurs() {
+ return minOccurs;
+ }
}
Index: src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java
===================================================================
--- src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java
(revision 207992)
+++ src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java
(working copy)
@@ -90,6 +90,7 @@
QName refName = element.getRefName();
if (schemaTypeName != null) {
xmlNode = new XmlNode(element.getName(), targetNamespace, false,
(element.getMaxOccurs() != 1), schemaTypeName.getLocalPart());
+ xmlNode.setMinOccurs(element.getMinOccurs());
parentNode.addChildToList(xmlNode);
if
(("http://www.w3.org/2001/XMLSchema").equals(schemaTypeName.getNamespaceURI()))
{
} else {
@@ -106,6 +107,7 @@
}
}else if (schemaType != null) {
xmlNode = new XmlNode(element.getName(), targetNamespace, false,
(element.getMaxOccurs() != 1), schemaType.getQName().getLocalPart());
+ xmlNode.setMinOccurs(element.getMinOccurs());
parentNode.addChildToList(xmlNode);
processSchemaType(schemaType, xmlNode, schema);
}else if (refName != null) {
@@ -175,17 +177,25 @@
private void generateQueue(XmlNode node) {
if (node.isArray()) {
if (node.getChildrenList().size() > 0) {
- queue.add(new JsonObject(node.getName(),
JSONType.NESTED_ARRAY, node.getValueType() , node.getNamespaceUri()));
+ JsonObject obj = new JsonObject(node.getName(),
JSONType.NESTED_ARRAY, node.getValueType() , node.getNamespaceUri());
+ obj.setMinOccurs(node.getMinOccurs());
+ queue.add(obj);
processXmlNodeChildren(node.getChildrenList());
} else {
- queue.add(new JsonObject(node.getName(), JSONType.ARRAY ,
node.getValueType() , node.getNamespaceUri()));
+ JsonObject obj = new JsonObject(node.getName(), JSONType.ARRAY
, node.getValueType() , node.getNamespaceUri());
+ obj.setMinOccurs(node.getMinOccurs());
+ queue.add(obj);
}
} else {
if (node.getChildrenList().size() > 0) {
- queue.add(new JsonObject(node.getName(),
JSONType.NESTED_OBJECT, node.getValueType() , node.getNamespaceUri()));
+ JsonObject obj = new JsonObject(node.getName(),
JSONType.NESTED_OBJECT, node.getValueType() , node.getNamespaceUri());
+ obj.setMinOccurs(node.getMinOccurs());
+ queue.add(obj);
processXmlNodeChildren(node.getChildrenList());
} else {
- queue.add(new JsonObject(node.getName(), JSONType.OBJECT ,
node.getValueType() , node.getNamespaceUri()));
+ JsonObject obj = new JsonObject(node.getName(),
JSONType.OBJECT , node.getValueType() , node.getNamespaceUri());
+ obj.setMinOccurs(node.getMinOccurs());
+ queue.add(obj);
}
}
}
Index: test/org/apache/axis2/json/gson/GsonXMLStreamWriterTest.java
===================================================================
--- test/org/apache/axis2/json/gson/GsonXMLStreamWriterTest.java
(revision 207992)
+++ test/org/apache/axis2/json/gson/GsonXMLStreamWriterTest.java
(working copy)
@@ -68,6 +68,26 @@
String actualString = baos.toString();
outputStreamWriter.close();
Assert.assertEquals(jsonString, actualString);
+
+
+ //test for when we have elements which may or may not included in the
output
+ //for those kinds of elements, wsdl file includes attribute "minOccurs"
+ //so if "minOccurs" is 0 then that element may not be in the output
xml document.
+ //so those kinds of outputs should be converted to json successfully
+ //this is the test for that
+ ByteArrayOutputStream baosFormo = new ByteArrayOutputStream();
+ OutputStreamWriter outputStreamWriterFormo = new
OutputStreamWriter(baosFormo, "UTF-8");
+ JsonWriter jsonWriterFormo = new JsonWriter(outputStreamWriterFormo);
+
+ GsonXMLStreamWriter gsonXMLStreamWriterFormo = new
GsonXMLStreamWriter(jsonWriterFormo, elementQName, schemaList, configCtxt);
+ OMElement omElementFormo = getResponseOMElementFormo();
+ gsonXMLStreamWriterFormo.writeStartDocument();
+ omElementFormo.serialize(gsonXMLStreamWriterFormo);
+ gsonXMLStreamWriterFormo.writeEndDocument();
+
+ String actualStringFormo = baosFormo.toString();
+ outputStreamWriterFormo.close();
+
Assert.assertEquals("{\"response\":{\"return\":{\"age\":\"35\",\"gender\":\"female\"}}}",
actualStringFormo);
}
@@ -89,4 +109,20 @@
response.addChild(ret);
return response;
}
+
+ private OMElement getResponseOMElementFormo() {
+ OMFactory omFactory = OMAbstractFactory.getOMFactory();
+ OMNamespace ns = omFactory.createOMNamespace("", "");
+
+ OMElement response = omFactory.createOMElement("response", ns);
+ OMElement ret = omFactory.createOMElement("return", ns);
+ OMElement age = omFactory.createOMElement("age", ns);
+ age.setText("35");
+ OMElement gender = omFactory.createOMElement("gender", ns);
+ gender.setText("female");
+ ret.addChild(age);
+ ret.addChild(gender);
+ response.addChild(ret);
+ return response;
+ }
}
Index: test-resources/custom_schema/testSchema_1.xsd
===================================================================
--- test-resources/custom_schema/testSchema_1.xsd (revision 207992)
+++ test-resources/custom_schema/testSchema_1.xsd (working copy)
@@ -35,7 +35,7 @@
<xs:complexType name="Person">
<xs:sequence>
- <xs:element name="name" type="xs:string"/>
+ <xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element name="age" type="xs:string"/>
<xs:element name="gender" type="xs:string"/>
</xs:sequence>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev