karatelabs/karate

with karate-gatling dependency version 1.0.1 empty attributes are added to the soap request

forlixdev opened this issue ยท 17 comments

It seems that using the following karate-gatling dependency (version 1.0.1)

<dependency>
    <groupId>com.intuit.karate</groupId>
    <artifactId>karate-gatling</artifactId>
    <version>${karate.version}</version>
    <scope>test</scope>
</dependency>

the request are not passed as it is but there's an empty attribute (xmlns) added the the soap request, making my tests failing. That is instead of:
<service>123456789</service>
the request sent is having an empty xmlns attribute:
<service xmlns="">123456789</service>

The problem is quite similar to the one reported in https://stackoverflow.com/questions/57042497/karate-xml-element-changed-to-have-empty-namespace/57042773.
In this case it must be something regarding the xml libraries handled by karate-gatling. With the version 0.96 of Karate (and the equivalent version for karate-gatling) it worked fine.
As attachment the code for replicating the problem, with a README. You have to run it without the dependency and check that it passes. Then decomment the dependency in the pom to see that it fails.

myproject.zip

I definitely don't consider this a priority, tagging as help wanted

@forlixdev here's a way you can help. I think there is a way to force the XML parser used by XmlUtils but it needs some research, e.g. https://stackoverflow.com/a/3316678/143475 - so it would be great if you can investigate, fix and submit a PR. does that make sense ?

Of course, I can do some research about it, even if being only a tester I don't know if I'm able to finalize it.
As you said is not a critical bug, only a bit annoying since it worked with the previous version.

@forlixdev no worries. do try, any bit of research helps - it may be as simple as copying karate xml to string conversion code and trying it in the offending pom / project etc. we can try ask around for volunteers later

Hi, only to say that I tried but still without luck for now. I'll go on trying.

@forlixdev / @ptrthomas I can take a stab at this issue.

@dinesh19aug by all means ๐Ÿ‘

Thanks @dinesh19aug. The last failed attempt I did was to try to use the internal implementation (in karate-core / XmlUtils class / toXmlDoc method):

String provider = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance(provider,XmlUtils.class.getClassLoader());

But the error was always the same mentioned in the issue.

please note that a possible fix has been attempted here: #1882 which may need some community help to complete

PerJV commented

Taging senarios with new tag :
@namespaceAware=true

Doing this in ScenarioEngine.evalKarateExpression :

            Tags  tags = runtime.scenario.getTagsEffective();
            boolean namespaceAware =  tags.valuesFor("namespaceAware").isAnyOf("true");
            Document doc = XmlUtils.toXmlDoc(text,namespaceAware);

and this change in XmlUtils

 public static Document toXmlDoc(String xml,boolean namespaceAware) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(namespaceAware);

could that be be an ok solution ?

@PerJV I really appreciate you following up on this. I would add a config property called xmlNamespaceAware in this class and pass it from the ScenarioEngine: https://github.com/karatelabs/karate/blob/v1.2.0.RC1/karate-core/src/main/java/com/intuit/karate/core/Config.java

but before all that, I have these concerns:

  • this is a problem that happens only when some combination of JARs is on the classpath
  • I'm not willing to accept any PR without a test-case so that we understand the before-after
  • unless we understand what is the root cause, and possibly if there is some other XML config that can be force-set, I am not comfortable to proceed
  • there are many users depending on how the existing XML parsing works, I'm not convinced there is a need to change anything yet

My two cents from a pure user perspective is that if it's only me that I noticed this error, we might find a workaround for this edge case (writing it in the documentation).
Otherwise if the problem is a nagging one, it would be better to handle it in another way for not changing the default xml handling behaviour (that might have a potential big impact), for example changing the behaviour accordingly to a parameter to be set in karate.config.

@forlixdev my bad, I completely missed that you had submitted a simple, clear way to replicate and could verify that the original solution proposed by @PerJV works

I have just implemented the solution. so adding this one line will solve the problem:

* configure xmlNamespaceAware = true

for completeness, this is also a workaround and way to submit XML without parsing it within Karate, note the use of text instead of def:

* text requestBody =
"""
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
</S:Envelope>
"""

Thanks for the work and support @PerJV and @ptrthomas

PerJV commented

Thanks @ptrthomas for adopting this change this really helped me a lot

1.2.0 released