Skip to content

Commit 118904e

Browse files
laurentschoelenslukasj
authored andcommitted
[#1788] ignore bindings file on schemaLocation=* + test case
1 parent 1c25a4a commit 118904e

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

jaxb-ri/xjc/src/main/java/com/sun/tools/xjc/reader/internalizer/Internalizer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -171,9 +172,14 @@ private void buildTargetNodeMap( Element bindings, @NotNull Node inheritedTarget
171172
String schemaLocation = bindings.getAttribute("schemaLocation");
172173

173174
// enhancement - schemaLocation="*" = bind to all schemas..
174-
if(schemaLocation.equals("*")) {
175+
if("*".equals(schemaLocation)) {
175176
for(String systemId : forest.listSystemIDs()) {
176177
result.computeIfAbsent(bindings, k -> new ArrayList<>());
178+
Element doc = forest.get(systemId).getDocumentElement();
179+
if (Const.JAXB_NSURI.equals(doc.getNamespaceURI())) {
180+
// this isn't a schema, so do apply bindings here or that will fail
181+
continue;
182+
}
177183
result.get(bindings).add(forest.get(systemId).getDocumentElement());
178184

179185
Element[] children = DOMUtils.getChildElements(bindings, Const.JAXB_NSURI, "bindings");

jaxb-ri/xjc/src/test/java/com/sun/tools/xjc/CodeGenTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2021, 2024 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -184,6 +185,44 @@ public void testIssue1785() throws FileNotFoundException, URISyntaxException, IO
184185
assertNonEmptyJavadocBlocks(cmString);
185186
}
186187

188+
/**
189+
* Test issues #1788 for ignoring bindings files in schemaLocation="*"
190+
*
191+
* @throws FileNotFoundException When the test schema or binding file cannot be read.
192+
* @throws URISyntaxException When the test {@link InputSource} cannot be parsed.
193+
*
194+
* @see <a href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1788">Issue #1788</a>
195+
*/
196+
public void testIssue1788() throws FileNotFoundException, URISyntaxException {
197+
String schemaFileName = "/schemas/issue1788/schema.xsd";
198+
String bindingFileName = "/schemas/issue1788/binding.xjb";
199+
String packageName = "org.example.issue1788";
200+
String someClassName = packageName + ".MyGh1788TypeDto";
201+
202+
ErrorListener errorListener = new ConsoleErrorReporter();
203+
204+
// Parse the XML schema.
205+
SchemaCompiler sc = XJC.createSchemaCompiler();
206+
sc.setErrorListener(errorListener);
207+
sc.getOptions().addBindFile(getInputSource(bindingFileName));
208+
sc.forcePackageName(packageName);
209+
sc.parseSchema(getInputSource(schemaFileName));
210+
211+
// Generate the defined class.
212+
S2JJAXBModel model = sc.bind();
213+
Plugin[] extensions = null;
214+
JCodeModel cm = model.generateCode(extensions, errorListener);
215+
JDefinedClass dc = cm._getClass(someClassName);
216+
assertNotNull(someClassName, dc);
217+
218+
// Assert Class includes narrow type
219+
Iterator<JMethod> conIter = dc.constructors();
220+
while (conIter.hasNext()) {
221+
JMethod con = conIter.next();
222+
assertTrue(toString(con).contains("java.lang.Class<java.lang.String>"));
223+
}
224+
}
225+
187226
private void assertNonEmptyJavadocBlocks(String cmString) throws IOException {
188227
int lineNo = 0;
189228
try ( LineNumberReader lnr = new LineNumberReader(new StringReader(cmString)) ) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Distribution License v. 1.0, which is available at
8+
http://www.eclipse.org/org/documents/edl-v10.php.
9+
10+
SPDX-License-Identifier: BSD-3-Clause
11+
12+
-->
13+
14+
<!-- See https://github.com/eclipse-ee4j/jaxb-ri/issues/1785 -->
15+
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" version="3.0">
16+
<jaxb:bindings schemaLocation="*">
17+
<jaxb:schemaBindings>
18+
<jaxb:nameXmlTransform>
19+
<jaxb:elementName prefix="My" suffix="Dto"/>
20+
<jaxb:typeName prefix="My" suffix="Dto"/>
21+
</jaxb:nameXmlTransform>
22+
</jaxb:schemaBindings>
23+
</jaxb:bindings>
24+
</jaxb:bindings>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Distribution License v. 1.0, which is available at
8+
http://www.eclipse.org/org/documents/edl-v10.php.
9+
10+
SPDX-License-Identifier: BSD-3-Clause
11+
12+
-->
13+
14+
<!-- See https://github.com/eclipse-ee4j/jaxb-ri/issues/1788 -->
15+
<xs:schema
16+
targetNamespace="http://example.org/document"
17+
xmlns:tns="http://example.org/document"
18+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
19+
>
20+
21+
<xs:complexType name="gh1788Type">
22+
<xs:sequence>
23+
<xs:element name="StringAttr" type="xs:string" minOccurs="0" nillable="true" />
24+
</xs:sequence>
25+
</xs:complexType>
26+
27+
<xs:element name="gh1788" type="tns:gh1788Type"/>
28+
29+
</xs:schema>

0 commit comments

Comments
 (0)