-
Notifications
You must be signed in to change notification settings - Fork 316
SimpleXsdSchema is not thread-safe #1556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nosan
wants to merge
1
commit into
spring-projects:4.0.x
Choose a base branch
from
nosan:1043
base: 4.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e51b606
to
d7b42c8
Compare
Before this commit, SimpleXsdSchema had a reference to an instance of `org.w3c.dom.Element`, which is not thread safe. This causes issues when multiple clients are requesting the schema file simultaneously. This commit updates SimpleXsdSchema to always create a new `ResourceSource` from an XSD Resource whenever getSource is requested. Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
nosan
commented
May 4, 2025
@@ -127,13 +132,15 @@ public void afterPropertiesSet() throws ParserConfigurationException, IOExceptio | |||
|
|||
private void loadSchema(DocumentBuilder documentBuilder) throws SAXException, IOException { | |||
Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(this.xsdResource)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unsure if DOM is the best way to parse XML to find the targetNamespace
. I believe StAX might be a better choice here to avoid loading the entire XML document into memory.
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.xsdResource, "'xsd' is required");
Assert.isTrue(this.xsdResource.exists(), "xsd '" + this.xsdResource + "' does not exist");
XmlRootElement element = XmlRootElement.of(this.xsdResource);
Assert.isTrue(SCHEMA_NAME.getLocalPart().equals(element.localName()),
this.xsdResource + " has invalid root element : [" + element.localName() + "] instead of [schema]");
Assert.isTrue(SCHEMA_NAME.getNamespaceURI().equals(element.namespaceURI()),
this.xsdResource + " has invalid root element: [" + element.namespaceURI() + "] instead of ["
+ SCHEMA_NAME.getNamespaceURI() + "]");
Assert.hasText(element.targetNamespace(), this.xsdResource + " has no targetNamespace");
this.targetNamespace = element.targetNamespace();
}
private record XmlRootElement(String localName, String namespaceURI,
String targetNamespace) {
static XmlRootElement of(Resource resource) throws Exception {
XMLInputFactory factory = StaxUtils.createDefensiveInputFactory();
factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
try (InputStream is = resource.getInputStream()) {
XMLStreamReader reader = factory.createXMLStreamReader(SaxUtils.getSystemId(resource), is);
try {
while (reader.hasNext()) {
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
return new XmlRootElement(reader.getLocalName(), reader.getNamespaceURI(),
reader.getAttributeValue(null, "targetNamespace"));
}
}
}
finally {
reader.close();
}
}
return new XmlRootElement(null, null, null);
}
}
Thanks @nosan. I want to acknowledge that I've seen this but will need a bit of time to review. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See #1043