Coder Social home page Coder Social logo

Comments (4)

Warpten avatar Warpten commented on June 16, 2024

Looking at this again, if the type attribute is missing from an annotation, it's expected to be derived from the property type itself. Now, I don't know codemodel that much (and am having trouble debugging it to inspect it in greater details), but I assume this should rather be fixed like so:

            jfv.annotations().stream()
                    .filter(jau -> Utils.isXmlElements(jau.getAnnotationClass()))
                    .map(jau -> (JAnnotationArrayMember) jau.getAnnotationMembers().get("value"))
                    .flatMap(value -> value.annotations().stream())
                    .forEach(anno -> {
                        JAnnotationValue type = anno.getAnnotationMembers().get("type");
                        if (type != null) {
                            handleXmlElement(outline, directClasses, type);
                        } else {
                            if (jfv.type().elementType() != null) { // Adapt this for List<T>. This only works for T[].
                                handleXmlElement(outline, directClasses, jfv.type().elementType());
                            }
                        }
                    });

And changing the final argument to handleXmlElement to a JGenerable

from jaxb-visitor.

massfords avatar massfords commented on June 16, 2024

@Warpten thanks for reporting this. Can you provide a full example so I can add it as a test case?

I'm wondering if the else should invoke the addIfDirectClass helper like so:

JClass elementType = (JClass) jfv.type().elementType();
List<JClass> typeParameters = elementType.getTypeParameters();
if (typeParameters != null && typeParameters.get(0) != null) {
   addIfDirectClass(directClasses, typeParameters.get(0));
} else {
   addIfDirectClass(directClasses, elementType);
}

It would help to have some schemas and different settings to test with.

from jaxb-visitor.

Warpten avatar Warpten commented on June 16, 2024

Sorry, I forgot about this.

A test case should be as simple as reusing the codehaus unit test with the sample XSD file I provided (it's self sufficient). Alternatively, I think this triggers as soon as the base type in an inheritance layout is prsent in a <xs:choice>'s possible value types:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="DerivedA" type="DerivedA"/>
    <xs:element name="BaseType" type="BaseType"/>

    <xs:element name="TypeList" type="TypeList"/>

    <xs:element name="DerivedD" type="DerivedD" />

    <xs:complexType name="TypeList">
        <xs:sequence>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="DerivedA"/>
                <xs:element ref="BaseType"/>
                <xs:element ref="DerivedD"/>
            </xs:choice>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="BaseType" />

    <xs:complexType name="DerivedA">
        <xs:complexContent>
            <xs:extension base="BaseType">
                <xs:sequence />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="DerivedD">
        <xs:complexContent>
            <xs:extension base="BaseType">
                <xs:sequence />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

This is enough to trigger the NPE when used with the basic-codehaus test.

As far as fixing this goes, I also tried this approach:

            jfv.annotations().stream()
                    .filter(jau -> Utils.isXmlElements(jau.getAnnotationClass()))
                    .map(jau -> (JAnnotationArrayMember) jau.getAnnotationMembers().get("value"))
                    .flatMap(value -> value.annotations().stream())
                    .forEach(anno -> {
                        JAnnotationValue type = anno.getAnnotationMembers().get("type");
                        if (type != null) {
                            handleXmlElement(outline, directClasses, type);
                        } else {
                            JClass jcs = (JClass) jfv.type();
                            if (Objects.equals(outline.getCodeModel().ref(List.class), jcs.erasure())) {
                                JClass elementType = jcs.getTypeParameters().get(0);
                                handleXmlElement(outline, directClasses, elementType);
                            } else {
                               handleXmlElement(outline, directClasses, jcs);
                            }
                        }
                    });

But I don't know that it is correct.

from jaxb-visitor.

massfords avatar massfords commented on June 16, 2024

@Warpten I added your schema as a test case and took the simple approach of checking for the type and then handling an array or collection. Thanks for the report and suggestions.

from jaxb-visitor.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.