This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-xmlschema.git
The following commit(s) were added to refs/heads/master by this push:
new b63715c6 Fix walker NPEs
b63715c6 is described below
commit b63715c6d3e5d1dc28ac837251030fe64ddfeb5e
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Sep 25 10:43:13 2026 +0100
Fix walker NPEs
---
.../ws/commons/schema/walker/XmlSchemaScope.java | 51 +++++++++++-------
.../schema/walker/DanglingReferenceWalkerTest.java | 60 ++++++++++++++++++++++
2 files changed, 93 insertions(+), 18 deletions(-)
diff --git
a/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaScope.java
b/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaScope.java
index bb354b0d..4141b7fe 100644
---
a/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaScope.java
+++
b/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaScope.java
@@ -199,6 +199,20 @@ final class XmlSchemaScope {
return (XmlSchemaSimpleType)type;
}
+ /**
+ * The base type of a simple content extension or restriction. Unlike
complex content, simple
+ * content takes its value type from the base, so there is no type to walk
without it.
+ */
+ private XmlSchemaType simpleContentBase(QName baseTypeName) {
+ final XmlSchemaType baseType =
+ (baseTypeName == null) ? null :
schemasByNamespace.getTypeByName(baseTypeName);
+ if (baseType == null) {
+ throw new XmlSchemaException("The simple content base type " +
baseTypeName
+ + " does not resolve to a type in
this collection.");
+ }
+ return baseType;
+ }
+
private void walk(XmlSchemaType type) {
if (type instanceof XmlSchemaSimpleType) {
walk((XmlSchemaSimpleType)type);
@@ -530,17 +544,14 @@ final class XmlSchemaScope {
XmlSchemaSimpleContentExtension ext =
(XmlSchemaSimpleContentExtension)content;
attributes = createAttributeMap(ext.getAttributes());
- XmlSchemaType baseType =
schemasByNamespace.getTypeByName(ext.getBaseTypeName());
+ final XmlSchemaType baseType =
simpleContentBase(ext.getBaseTypeName());
+ final XmlSchemaScope parentScope = getScope(baseType);
+ typeInfo = parentScope.getTypeInfo();
- if (baseType != null) {
- final XmlSchemaScope parentScope = getScope(baseType);
- typeInfo = parentScope.getTypeInfo();
-
- if (attributes == null) {
- attributes = parentScope.attributes;
- } else if (parentScope.attributes != null) {
- attributes.putAll(parentScope.attributes);
- }
+ if (attributes == null) {
+ attributes = parentScope.attributes;
+ } else if (parentScope.attributes != null) {
+ attributes.putAll(parentScope.attributes);
}
anyAttr = ext.getAnyAttribute();
@@ -553,17 +564,15 @@ final class XmlSchemaScope {
if (rstr.getBaseType() != null) {
baseType = rstr.getBaseType();
} else {
- baseType =
schemasByNamespace.getTypeByName(rstr.getBaseTypeName());
+ baseType = simpleContentBase(rstr.getBaseTypeName());
}
- if (baseType != null) {
- XmlSchemaScope parentScope = getScope(baseType);
- typeInfo = restrictTypeInfo(parentScope.getTypeInfo(),
-
mergeFacets(parentScope.getTypeInfo().getFacets(),
- rstr.getFacets()));
+ XmlSchemaScope parentScope = getScope(baseType);
+ typeInfo = restrictTypeInfo(parentScope.getTypeInfo(),
+
mergeFacets(parentScope.getTypeInfo().getFacets(),
+ rstr.getFacets()));
- attributes = mergeAttributes(parentScope.attributes,
attributes);
- }
+ attributes = mergeAttributes(parentScope.attributes, attributes);
anyAttr = rstr.getAnyAttribute();
}
@@ -629,6 +638,12 @@ final class XmlSchemaScope {
private XmlSchemaAttrInfo getAttribute(XmlSchemaAttribute attribute,
boolean forceCopy) {
+ if (!attribute.isRef() && (attribute.getQName() == null)) {
+ // Accepted by the schema reader, but there is no attribute to
walk.
+ throw new XmlSchemaException("An attribute declaration has neither
a name nor a ref,"
+ + " so it cannot be walked.");
+ }
+
if (!attribute.isRef() && (attribute.getSchemaType() != null) &&
!forceCopy) {
if (attribute.getUse().equals(XmlSchemaUse.NONE)) {
diff --git
a/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/walker/DanglingReferenceWalkerTest.java
b/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/walker/DanglingReferenceWalkerTest.java
index dfcf4ea6..1099bb97 100644
---
a/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/walker/DanglingReferenceWalkerTest.java
+++
b/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/walker/DanglingReferenceWalkerTest.java
@@ -102,6 +102,66 @@ public class DanglingReferenceWalkerTest extends Assert {
"has no type");
}
+ @Test
+ public void testAttributeWithNeitherNameNorRefIsRejected() {
+ assertRejected("<xs:complexType name=\"t\">"
+ + "<xs:attribute type=\"xs:int\"/>"
+ + "</xs:complexType>"
+ + "<xs:element name=\"root\" type=\"tns:t\"/>",
+ "neither a name nor a ref");
+ }
+
+ @Test
+ public void testAttributeWithInlineTypeAndNoNameIsRejected() {
+ assertRejected("<xs:complexType name=\"t\">"
+ + "<xs:attribute><xs:simpleType><xs:restriction
base=\"xs:int\"/>"
+ + "</xs:simpleType></xs:attribute>"
+ + "</xs:complexType>"
+ + "<xs:element name=\"root\" type=\"tns:t\"/>",
+ "neither a name nor a ref");
+ }
+
+ @Test
+ public void testDanglingSimpleContentExtensionBaseIsRejected() {
+ assertRejected("<xs:complexType name=\"c\"><xs:simpleContent>"
+ + "<xs:extension base=\"tns:missing\"/>"
+ + "</xs:simpleContent></xs:complexType>"
+ + "<xs:element name=\"root\" type=\"tns:c\"/>",
+ "simple content base type");
+ }
+
+ @Test
+ public void testRestrictionOfADanglingSimpleContentExtensionIsRejected() {
+ assertRejected("<xs:complexType name=\"c\"><xs:simpleContent>"
+ + "<xs:extension base=\"tns:missing\"/>"
+ + "</xs:simpleContent></xs:complexType>"
+ + "<xs:complexType name=\"d\"><xs:simpleContent>"
+ + "<xs:restriction base=\"tns:c\"/>"
+ + "</xs:simpleContent></xs:complexType>"
+ + "<xs:element name=\"root\" type=\"tns:d\"/>",
+ "simple content base type");
+ }
+
+ @Test
+ public void testDanglingSimpleContentRestrictionBaseIsRejected() {
+ assertRejected("<xs:complexType name=\"d\"><xs:simpleContent>"
+ + "<xs:restriction base=\"tns:missing\"/>"
+ + "</xs:simpleContent></xs:complexType>"
+ + "<xs:element name=\"root\" type=\"tns:d\"/>",
+ "simple content base type");
+ }
+
+ @Test
+ public void testResolvedSimpleContentStillWalks() {
+ walkRoot("<xs:complexType name=\"c\"><xs:simpleContent>"
+ + "<xs:extension base=\"xs:int\"><xs:attribute name=\"a\"
type=\"xs:string\"/>"
+ + "</xs:extension></xs:simpleContent></xs:complexType>"
+ + "<xs:complexType name=\"d\"><xs:simpleContent>"
+ + "<xs:restriction base=\"tns:c\"><xs:maxInclusive
value=\"10\"/></xs:restriction>"
+ + "</xs:simpleContent></xs:complexType>"
+ + "<xs:element name=\"root\" type=\"tns:d\"/>");
+ }
+
@Test
public void testResolvableReferencesStillWalk() {
walkRoot("<xs:attributeGroup name=\"ag\">"