Skip to content
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

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

Closed
forlixdev opened this issue May 5, 2021 · 17 comments
Assignees
Milestone

Comments

@forlixdev
Copy link

forlixdev commented May 5, 2021

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

@ptrthomas
Copy link
Member

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

@ptrthomas
Copy link
Member

@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 ?

@forlixdev
Copy link
Author

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.

@ptrthomas
Copy link
Member

@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

@forlixdev
Copy link
Author

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

@dinesh19aug
Copy link
Contributor

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

@ptrthomas
Copy link
Member

@dinesh19aug by all means 👍

@forlixdev
Copy link
Author

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.

@ptrthomas
Copy link
Member

moved to project board: https://github.com/intuit/karate/projects/3#card-61812894

@ptrthomas
Copy link
Member

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

@PerJV
Copy link

PerJV commented Jan 29, 2022

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 ?

@ptrthomas
Copy link
Member

@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

@forlixdev
Copy link
Author

forlixdev commented Jan 29, 2022

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.

@ptrthomas ptrthomas assigned ptrthomas and unassigned dinesh19aug Jan 29, 2022
@ptrthomas ptrthomas added this to the 1.2.0 milestone Jan 29, 2022
@ptrthomas ptrthomas reopened this Jan 29, 2022
@ptrthomas
Copy link
Member

ptrthomas commented Jan 29, 2022

@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>
"""

@forlixdev
Copy link
Author

Thanks for the work and support @PerJV and @ptrthomas

@PerJV
Copy link

PerJV commented Jan 30, 2022

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

@ptrthomas
Copy link
Member

1.2.0 released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants