{"action":"created","issue":{"url":"https://api.github.com/repos/FasterXML/jackson-module-jaxb-annotations/issues/38","repository_url":"https://api.github.com/repos/FasterXML/jackson-module-jaxb-annotations","labels_url":"https://api.github.com/repos/FasterXML/jackson-module-jaxb-annotations/issues/38/labels{/name}","comments_url":"https://api.github.com/repos/FasterXML/jackson-module-jaxb-annotations/issues/38/comments","events_url":"https://api.github.com/repos/FasterXML/jackson-module-jaxb-annotations/issues/38/events","html_url":"https://github.com/FasterXML/jackson-module-jaxb-annotations/issues/38","id":52653462,"number":38,"title":"ObejectMapper accepts other elements than specified in @XmlElements","user":{"login":"smolarek999","id":295368,"avatar_url":"https://avatars0.githubusercontent.com/u/295368?v=4","gravatar_id":"","url":"https://api.github.com/users/smolarek999","html_url":"https://github.com/smolarek999","followers_url":"https://api.github.com/users/smolarek999/followers","following_url":"https://api.github.com/users/smolarek999/following{/other_user}","gists_url":"https://api.github.com/users/smolarek999/gists{/gist_id}","starred_url":"https://api.github.com/users/smolarek999/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smolarek999/subscriptions","organizations_url":"https://api.github.com/users/smolarek999/orgs","repos_url":"https://api.github.com/users/smolarek999/repos","events_url":"https://api.github.com/users/smolarek999/events{/privacy}","received_events_url":"https://api.github.com/users/smolarek999/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2014-12-22T14:21:59Z","updated_at":"2018-05-19T17:53:04Z","closed_at":"2018-05-19T17:53:04Z","author_association":"NONE","body":"Hi,\n\nI am working on project where we are using _JSON_ <-> _XML_ conversion. We are using auto-generated _JAXB_ beans (generated by _MOXY_).\n\nI found some inconsistency with `xsd:choice` in xsd. In this case _Moxy_ will generate mapping like this:\n\n``` Java\n@XmlElements({ @XmlElement(name = \"Type1\", type = Type1.class),\n            @XmlElement(name = \"Type2\", type = Type2.class) },\n                        @XmlElement(name = \"TypeN\", type = TypeN.class) })\nList<Objects> type1OrType2OrTypeN;\n```\n\nFor such mapping _Jackson_ is expecting _JSON _like this:\n\n``` javascript\n{\n    \"type1OrType2OrTypeN\":  [       \n             {   \"Type1\": {}      },\n             {   \"Type2\": {}    }      \n      ]\n}\n```\n\nIt expect array which contains wrapping objects, where each object has specified type in key.\n\nMy findings:\n1) Jackon will accept also different values (no expected form XSD perspecive), such us: string `[\"txt\",\"txt1\"]` or Object `[{\"Object\":{}}]` -> could it be fixed and restricted to only specified by `@XmlElement` ones??\n2. When I provide different JsonObject than Object, Type1 or Type2,  it fails with no meaningful error message: `Could not resolve type id 'Type3' into a subtype of [simple type, class java.lang.Object]`. -> Could it be changed to error which will say which types are allowed?\n3. JsonSchema for this elements brings no information about elements types, it only says that it is type of array. I do not have idea how valid JsonSchema should look like it this case. Maybe _anyOf_ with allowed types?\n\nI've checked how moxy handle this scenario, since it has also JSON <-> XML feature, and it  that is handled differently. There is sample of valid Json:\n\n``` javascript\n{\n    \"Type1\":   [ {},  {} ],\n        \"Type2\" : []\n}\n```\n\nInstead of having intermediate array element to store Type1 or Type2, it uses separate arrays for each type. It makes JsonSchema simpler because it simply say that object can contain array of Type1 or/and array of Type2. So maybe it is the way how this `@XmlElements` can be handled also in Jackson?\n\nI've attaching simple testing classes\n\n``` Java\n@XmlRootElement(name = \"foo\")\n@XmlAccessorType(XmlAccessType.FIELD)\npublic class XmlWithChoice {\n\n    @XmlElements({ @XmlElement(name = \"Type1\", type = Type1.class),\n            @XmlElement(name = \"Type2\", type = Type2.class) })\n    private List<Object> items;\n\n    @Override\n    public String toString() {\n        return ToStringBuilder.reflectionToString(this,\n                ToStringStyle.SHORT_PREFIX_STYLE);\n    }\n\n    @XmlAccessorType(XmlAccessType.FIELD)\n    @XmlType(name = \"\")\n    public static class Type1 {\n\n        @XmlAttribute(name = \"Code\", required = true)\n        protected String code;\n\n        public String getCode() {\n            return code;\n        }\n\n        public void setCode(String value) {\n            this.code = value;\n        }\n\n        @Override\n        public String toString() {\n            return ToStringBuilder.reflectionToString(this,\n                    ToStringStyle.SHORT_PREFIX_STYLE);\n        }\n\n    }\n\n    @XmlAccessorType(XmlAccessType.FIELD)\n    @XmlType(name = \"\")\n    public static class Type2 {\n\n        @XmlAttribute(name = \"Code\", required = true)\n        protected String code;\n\n        public String getCode() {\n            return code;\n        }\n\n        public void setCode(String value) {\n            this.code = value;\n        }\n\n        @Override\n        public String toString() {\n            return ToStringBuilder.reflectionToString(this,\n                    ToStringStyle.SHORT_PREFIX_STYLE);\n        }\n\n    }\n\n}\n```\n\n``` Java\npublic class XmlWithChoiceTest {\n\n    ObjectMapper om;\n\n    @Before\n    public void setUp() {\n\n        om = new ObjectMapper();\n\n        AnnotationIntrospector jaxbAnnotationIntrospector = new JaxbAnnotationIntrospector(\n                om.getTypeFactory());\n        AnnotationIntrospector jacksonAnnotationIntrospector = new JacksonAnnotationIntrospector();\n\n        AnnotationIntrospector pair = AnnotationIntrospector.pair(\n                jaxbAnnotationIntrospector, jacksonAnnotationIntrospector);\n        om.setAnnotationIntrospector(pair);\n\n    }\n\n    @Test(expected = JsonMappingException.class)\n    public void shouldNotAcceptObjectAndString() throws Exception {\n        String src = \"{\\\"items\\\":[{\\\"Type1\\\":{\\\"Code\\\":\\\"ABC\\\"}},{\\\"Type2\\\":{\\\"Code\\\":\\\"GHI\\\"}},\\\"ABC\\\",{\\\"Object\\\":{\\\"Code\\\":\\\"GHI\\\"}}]}\";\n        XmlWithChoice readValue = om.readValue(src, XmlWithChoice.class);\n    }\n\n    @Test\n    public void itShouldFailWithSomeMeaningfullError() throws Exception {\n        String src = \"{\\\"items\\\":[{\\\"Type3\\\":{\\\"Code\\\":\\\"ABC\\\"}},{\\\"Type2\\\":{\\\"Code\\\":\\\"GHI\\\"}},\\\"ABC\\\",{\\\"Object\\\":{\\\"Code\\\":\\\"GHI\\\"}}]}\";\n        om.readValue(src, XmlWithChoice.class);\n    }\n\n    @Test\n    public void generateJsonSchema() throws Exception {\n\n        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();\n        om.acceptJsonFormatVisitor(om.constructType(XmlWithChoice.class),\n                visitor);\n        JsonSchema finalSchema = visitor.finalSchema();\n        String schema = om.writeValueAsString(finalSchema);\n        fail(\"It says only that items is type 'array' without any details\");\n\n    }\n}\n```\n\nThanks,\nPawel\n"},"comment":{"url":"https://api.github.com/repos/FasterXML/jackson-module-jaxb-annotations/issues/comments/390421687","html_url":"https://github.com/FasterXML/jackson-module-jaxb-annotations/issues/38#issuecomment-390421687","issue_url":"https://api.github.com/repos/FasterXML/jackson-module-jaxb-annotations/issues/38","id":390421687,"user":{"login":"cowtowncoder","id":55065,"avatar_url":"https://avatars0.githubusercontent.com/u/55065?v=4","gravatar_id":"","url":"https://api.github.com/users/cowtowncoder","html_url":"https://github.com/cowtowncoder","followers_url":"https://api.github.com/users/cowtowncoder/followers","following_url":"https://api.github.com/users/cowtowncoder/following{/other_user}","gists_url":"https://api.github.com/users/cowtowncoder/gists{/gist_id}","starred_url":"https://api.github.com/users/cowtowncoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cowtowncoder/subscriptions","organizations_url":"https://api.github.com/users/cowtowncoder/orgs","repos_url":"https://api.github.com/users/cowtowncoder/repos","events_url":"https://api.github.com/users/cowtowncoder/events{/privacy}","received_events_url":"https://api.github.com/users/cowtowncoder/received_events","type":"User","site_admin":false},"created_at":"2018-05-19T17:53:03Z","updated_at":"2018-05-19T17:53:03Z","author_association":"MEMBER","body":"Porject moved; may be re-filed at\r\n\r\nhttps://github.com/FasterXML/jackson-modules-base/issues\r\n\r\nif issue still exists.\r\n"}}
{"actor":{"display_login":"cowtowncoder"}}