murataydogdu / odata4j

Automatically exported from code.google.com/p/odata4j
0 stars 0 forks source link

Empty tags in metadata don't pass XML validator in .Net #242

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Set up an odata provider, say InMemory
2. Using VS Express on Windows try to create a service reference to your new 
provider. This fails with the attached error message.
3. Try the same against Northwind it works.

It appears that the difference between odata4j metadata XML and Nortwind is 
that Northwind uses empty tags like this: <PropertyRef ... /> while odata4j 
does <PropertyRef...></PropertyRef>

As far as XML is concerned both formats are equivalent. However, the odata4j 
format doesn't seem to pass the .Net xml parser's validation. For what it is 
worth this is more of a .Net issue than odata4j issue, however I am under 
constraints to use standard .Net odata facilities.

odata4j-0.8-SNAPSHOT built from sources, on Linux, using a windows client.

Please provide any additional information below.

Original issue reported on code.google.com by oleg.du...@gmail.com on 28 Feb 2013 at 12:50

Attachments:

GoogleCodeExporter commented 8 years ago
In fact, the following change in StaxXMLWriter2 fixes the problem:

 public StaxXMLWriter2(Writer stream) {
    XMLOutputFactory f = new WstxOutputFactory();
    f.setProperty(WstxOutputFactory.P_AUTOMATIC_EMPTY_ELEMENTS,true);

and adding this to pom.xml:

   <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>wstx-asl</artifactId>
        <version>4.0.6</version>
    </dependency>

Somehow this makes unit tests fail, but at least this unblocks me.

Original comment by oleg.du...@gmail.com on 28 Feb 2013 at 2:36

GoogleCodeExporter commented 8 years ago
An alternative to adding a new build dependency is to insert this code into 
MetadataResource.java:

// inside getMetadata()
      return Response.ok(makeMetadataCompatibleWithDotNet(w.toString()), ODataConstants.APPLICATION_XML_CHARSET_UTF8)
          .header(ODataConstants.Headers.DATA_SERVICE_VERSION, ODataConstants.DATA_SERVICE_VERSION_HEADER)
          .build();

// new method
    private String makeMetadataCompatibleWithDotNet(final String originalMetadataText) {
        // convert tags to be compatible with .NET: <PropertyRef ...></PropertyRef> --> <PropertyRef .../>
        final Pattern pattern = Pattern.compile("<(\\w+)\\s+([^>]+)>\\s*</\\1>");
        final Matcher matcher = pattern.matcher(originalMetadataText);
        StringBuffer sb = new StringBuffer(originalMetadataText.length());
        int lastIndex = 0;
        while (matcher.find()) {
            sb.append(originalMetadataText.substring(lastIndex, matcher.start()));
            sb.append('<').append(matcher.group(1)).append(' ').append(matcher.group(2)).append("/>");
            lastIndex = matcher.end();
        }
        sb.append(originalMetadataText.substring(lastIndex));
        return sb.toString();
    }

Original comment by rndg...@gmail.com on 30 Aug 2013 at 4:30