eclipse-ee4j / jaxb-ri

Jaxb RI
https://eclipse-ee4j.github.io/jaxb-ri/
BSD 3-Clause "New" or "Revised" License
204 stars 111 forks source link

Default and fixed fields not generated correctly when using restriction to create new types. #515

Open Tomas-Kraus opened 16 years ago

Tomas-Kraus commented 16 years ago

If restriction is used to derive a new complex type from an existing type, and attributes are restricted by having a default or fixed value specified, where none was previously specified on the base type, then the xjc compiler fails to create the default or fixed value. It does manage to do it for default or fixed values on the base type itself.

I've cut this down to a simple example to illustrate. Here is my schema (test.xsd):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://thebadgerset.co.uk/test-0.1" targetNamespace="http://thebadgerset.co.uk/test-0.1" elementFormDefault="qualified">

Here is a sample data file to test this with (test.xml): Here is my bindings file (test.xjb): Here is my main method: public static void main(String[] args) { try { // Create an unmarshaller. JAXBContext jc = JAXBContext.newInstance("uk.co.thebadgerset.xsdrestrictiontest"); Unmarshaller u = jc.createUnmarshaller(); // Set the schema on the unmarshaller. SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema sedaSchema = schemaFactory.newSchema(Main.class.getClassLoader().getResource("test.xsd")); u.setSchema(sedaSchema); // Unmarshall the test file. Reader configReader = new InputStreamReader(Main.class.getClassLoader().getResourceAsStream("test.xml")); JAXBElement element = (JAXBElement) u.unmarshal(configReader); B b = element.getValue(); // Check what values have been set for fixed and default fields. System.out.println("b.getFixedByOverride() = " + b.getFixedByOverride()); System.out.println("b.getFixedInRoot() = " + b.getFixedInRoot()); System.out.println("b.getDefaultInRoot() = " + b.getDefaultInRoot()); System.out.println("b.getDefaultOverride() = " + b.getDefaultOverride()); System.out.println("b.getDefaultOverrideByFixed() = " + b.getDefaultOverrideByFixed()); } catch (Exception e) { e.printStackTrace(); } } The schema is compiled into classes 'A' and 'B' by xjc, I'm using latest released version 2.1.7. Here is the output: b.getFixedByOverride() = null b.getFixedInRoot() = fixedValue b.getDefaultInRoot() = defaultValue b.getDefaultOverride() = null b.getDefaultOverrideByFixed() = defaultValue What you can see from this, is that the fixed and defaulted attributes in the base type 'A' are correctly fixed and defaulted. When I override them by restriction to create a new type 'B' the new fixed or defaulted values are not provided by the class B. Seems to me that the 'get*' methods in A that B alters, could override the methods in 'A' to provide the fixed or defaulted values that I want. I have attached an archive with an example for this bug to the forum topic: [http://forums.java.net/jive/thread.jspa?threadID=42439&tstart=0](http://forums.java.net/jive/thread.jspa?threadID=42439&tstart=0) You should be able to build and run it with: > mvn clean assembly:assembly > java -cp target/xsdrestrictiontest-0.1-SNAPSHOT-all-test-deps.jar uk.co.thebadgerset.xsdrestrictiontest.Main #### Environment Operating System: All Platform: All URL: [http://forums.java.net/jive/thread.jspa?threadID=42439&tstart=0](http://forums.java.net/jive/thread.jspa?threadID=42439&tstart=0) #### Affected Versions [2.1.7]
Tomas-Kraus commented 6 years ago
Tomas-Kraus commented 16 years ago

@glassfishrobot Commented Reported by rupertlssmith

Tomas-Kraus commented 16 years ago

@glassfishrobot Commented rupertlssmith said: Created an attachment (id=256) The example described in the bug report.

Tomas-Kraus commented 15 years ago

@glassfishrobot Commented @pavelbucek said: not in current scope of work, changing to ENHANCEMENT

Tomas-Kraus commented 15 years ago

@glassfishrobot Commented tdesmedt said: see https://jaxb.dev.java.net/issues/show_bug.cgi?id=683 for a workaround through validation

Tomas-Kraus commented 16 years ago

@glassfishrobot Commented File: xsdrestrictiontest.tar.gz Attached By: rupertlssmith

Tomas-Kraus commented 16 years ago

@glassfishrobot Commented Was assigned to jaxb-issues

Tomas-Kraus commented 7 years ago

@glassfishrobot Commented This issue was imported from java.net JIRA JAXB-515