You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@olingo.apache.org by ye...@sina.com on 2015/04/01 12:47:57 UTC

java.lang.ClassCastException is thrown during request Metadata Service document with V3Client

Hi all,
 
I found a issue during sending request to get Metadata Service Document using org.apache.olingo.client.api.v3.ODataClient.  Not sure if this is a bug.
(Olingo version:4.0.0-beta-02)
 
My code is like this:
 
ODataClient client = ODataClientFactory.getV3();
String serviceRoot = "http://localhost/my/yxy_site/_api/" ;
EdmMetadataRequest request = client.getRetrieveRequestFactory().getMetadataRequest(serviceRoot);
ODataRetrieveResponse<Edm> response = request.execute();
Edm edm = response.getBody();
...
 
But following exception will be thrown while running.
 
Caused by: java.lang.ClassCastException: org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl cannot be cast to org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl
 at org.apache.olingo.client.core.edm.xml.ComplexTypeDeserializer.doDeserialize(ComplexTypeDeserializer.java:51)
 at org.apache.olingo.client.core.edm.xml.ComplexTypeDeserializer.doDeserialize(ComplexTypeDeserializer.java:32)
 at org.apache.olingo.client.core.edm.xml.AbstractEdmDeserializer.deserialize(AbstractEdmDeserializer.java:68)
 
When I checked out ComplexTypeDeserializer.java, I found a weird code snippet.
==========================================================================
protected AbstractComplexType doDeserialize(final JsonParser jp, final DeserializationContext ctxt)
          throws IOException, JsonProcessingException {
    final AbstractComplexType complexType = ODataServiceVersion.V30 == version
            ? new org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl()
            : new org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl();
    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
      final JsonToken token = jp.getCurrentToken();
      if (token == JsonToken.FIELD_NAME) {
        if ("Name".equals(jp.getCurrentName())) {
          complexType.setName(jp.nextTextValue());
        } else if ("Abstract".equals(jp.getCurrentName())) {
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  setAbstractEntityType(BooleanUtils.toBoolean(jp.nextTextValue()));
        } else if ("BaseType".equals(jp.getCurrentName())) {
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  setBaseType(jp.nextTextValue());
        } else if ("OpenType".equals(jp.getCurrentName())) {
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  setOpenType(BooleanUtils.toBoolean(jp.nextTextValue()));
        } else if ("Property".equals(jp.getCurrentName())) {
          jp.nextToken();
          if (complexType instanceof org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl) {
            ((org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl) complexType).
                    getProperties().add(jp.readValueAs(
                                    org.apache.olingo.client.core.edm.xml.v3.PropertyImpl.class));
          } else {
            ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                    getProperties().add(jp.readValueAs(
                                    org.apache.olingo.client.core.edm.xml.v4.PropertyImpl.class));
          }
        } else if ("NavigationProperty".equals(jp.getCurrentName())) {
          jp.nextToken();
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  getNavigationProperties().add(jp.readValueAs(
                                  org.apache.olingo.client.core.edm.xml.v4.NavigationPropertyImpl.class));
        } else if ("Annotation".equals(jp.getCurrentName())) {
          jp.nextToken();
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).getAnnotations().
                  add(jp.readValueAs(AnnotationImpl.class));
        }
      }
    }
    return complexType;
  }
 
======================================================
You will find that if current OData Service Version is 3.0, complexType will be initialized as org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl().
However, if the program goes into the condition, "BaseType".equals(jp.getCurrentName()) , the logic tries to cast v3.ComplexTypeImpl into v4.ComplexTypeImpl, which is obvious incorrect.
 
So, is that a bug?
 
Thanks,
Xianyi Ye

 

RE: java.lang.ClassCastException is thrown during request Metadata Service document with V3Client

Posted by "Amend, Christian" <ch...@sap.com>.
Hi Xianyi Ye,

yes this is a bug. However with the current SNAPSHOT we have massively refactored the client code to only support V4 OData services. So the code you showed will not be in the next release version.

We do plan to provide a way to consume V3 services with another library but I cannot tell you when there will be a release for this.

Best Regards,
Christian

From: yexianyi@sina.com [mailto:yexianyi@sina.com]
Sent: Mittwoch, 1. April 2015 12:48
To: user
Subject: java.lang.ClassCastException is thrown during request Metadata Service document with V3Client


Hi all,



I found a issue during sending request to get Metadata Service Document using org.apache.olingo.client.api.v3.ODataClient.  Not sure if this is a bug.

(Olingo version:4.0.0-beta-02)



My code is like this:



ODataClient client = ODataClientFactory.getV3();

String serviceRoot = "http://localhost/my/yxy_site/_api/" ;

EdmMetadataRequest request = client.getRetrieveRequestFactory().getMetadataRequest(serviceRoot);

ODataRetrieveResponse<Edm> response = request.execute();

Edm edm = response.getBody();

...



But following exception will be thrown while running.



Caused by: java.lang.ClassCastException: org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl cannot be cast to org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl
 at org.apache.olingo.client.core.edm.xml.ComplexTypeDeserializer.doDeserialize(ComplexTypeDeserializer.java:51)
 at org.apache.olingo.client.core.edm.xml.ComplexTypeDeserializer.doDeserialize(ComplexTypeDeserializer.java:32)
 at org.apache.olingo.client.core.edm.xml.AbstractEdmDeserializer.deserialize(AbstractEdmDeserializer.java:68)



When I checked out ComplexTypeDeserializer.java, I found a weird code snippet.

==========================================================================

protected AbstractComplexType doDeserialize(final JsonParser jp, final DeserializationContext ctxt)
          throws IOException, JsonProcessingException {

    final AbstractComplexType complexType = ODataServiceVersion.V30 == version
            ? new org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl()
            : new org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
      final JsonToken token = jp.getCurrentToken();
      if (token == JsonToken.FIELD_NAME) {
        if ("Name".equals(jp.getCurrentName())) {
          complexType.setName(jp.nextTextValue());
        } else if ("Abstract".equals(jp.getCurrentName())) {
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  setAbstractEntityType(BooleanUtils.toBoolean(jp.nextTextValue()));
        } else if ("BaseType".equals(jp.getCurrentName())) {
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  setBaseType(jp.nextTextValue());
        } else if ("OpenType".equals(jp.getCurrentName())) {
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  setOpenType(BooleanUtils.toBoolean(jp.nextTextValue()));
        } else if ("Property".equals(jp.getCurrentName())) {
          jp.nextToken();
          if (complexType instanceof org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl) {
            ((org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl) complexType).
                    getProperties().add(jp.readValueAs(
                                    org.apache.olingo.client.core.edm.xml.v3.PropertyImpl.class));
          } else {
            ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                    getProperties().add(jp.readValueAs(
                                    org.apache.olingo.client.core.edm.xml.v4.PropertyImpl.class));
          }
        } else if ("NavigationProperty".equals(jp.getCurrentName())) {
          jp.nextToken();
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).
                  getNavigationProperties().add(jp.readValueAs(
                                  org.apache.olingo.client.core.edm.xml.v4.NavigationPropertyImpl.class));
        } else if ("Annotation".equals(jp.getCurrentName())) {
          jp.nextToken();
          ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).getAnnotations().
                  add(jp.readValueAs(AnnotationImpl.class));
        }
      }
    }

    return complexType;
  }



======================================================

You will find that if current OData Service Version is 3.0, complexType will be initialized as org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl().

However, if the program goes into the condition, "BaseType".equals(jp.getCurrentName()) , the logic tries to cast v3.ComplexTypeImpl into v4.ComplexTypeImpl, which is obvious incorrect.



So, is that a bug?



Thanks,

Xianyi Ye