cancel
Showing results for 
Search instead for 
Did you mean: 

How to make swagger compliance assertion url dynamic

SOLVED
JoostDG
Frequent Contributor

How to make swagger compliance assertion url dynamic

Hi,

I have been struggling to get some grip on the swagger compliance assertion. I want to provide a dynamic url but ReadyAPI doesn't seem to cope with it in the way I would expect.

Use case: The swagger url might change over time and I don't want to adapt it in all individual swagger compliance assertions.

Current attempt:

In my project setup script I define following properties:

1) 

def swaggerENGvalidationPath = testDataPath + File.separator + "swagger" + File.separator + "ENG" + File.separator + swaggerENGversionForValidation + ".yaml"

project.setPropertyValue("swaggerENGvalidationPath", swaggerENGvalidationPath)

2)

def swaggerAPPvalidationPath = "https://onlineUrlToMyApiSwagger.yaml"
project.setPropertyValue("swaggerAPPvalidationPath", swaggerAPPvalidationPath)

 

So path 1 is pointing to a yaml file present in my project repo. In the swagger validation assertion I set the url to \${#Project#swaggerENGvalidationPath} and this works fine. 

Path 2 should point to a specific url. When I set this url directly in the swagger validation assertion it works. But when I set the swaggerAPPvalidationPath as a dynamic property in the assertion like this: ${#Project#swaggerAPPvalidationPath} it doesn't work.

JoostDG_0-1624612523860.png

I see in the log "Fri Jun 25 09:19:17 UTC 2021: ERROR: attribute is not of type `object`" and I can see the HTTP Log reading the swagger file, but the assertion always fails "Failed to load the swagger definition from [${#Project#swaggerAPPvalidationPath}]

 

With \${#Project#swaggerAPPvalidationPath} it complains like this: Illegal char <:> at index 7. I notice indeed that then it starts with C:/http... which obviously won't work.

 

I tried to also to put some escaping variants in my project property, but to no avail. Tried "https://onlineUrlToMyApiSwagger.yaml", "https:////onlineUrlToMyApiSwagger.yaml", "'https://onlineUrlToMyApiSwagger.yaml'", "\"https://onlineUrlToMyApiSwagger.yaml\"",...

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: How to make swagger compliance assertion url dynamic

@JoostDG 

Here is an approach which you can try.

NOTE:

1. In the Swagger Compliance Assertion set a file path or url (absolute value). The value will be replaced with value passed at run time as project property, SWAGGER_URL

2. Here, property expansion is not used. Instead value is overridden run time.

3. Please follow the comments provided in the script.

4. This is project setup script only.

 

Project Setup Script 

Here is the log message when the script is run

nmrao_0-1625569882099.png

 

 



Regards,
Rao.

View solution in original post

18 REPLIES 18
JoostDG
Frequent Contributor

Re: How to make swagger compliance assertion url dynamic

Ok. On a final attempt it seems I myself got the answer...

 

I set it up WITHOUT the 'https:' part in the property, so like this:

def swaggerAPPvalidationPath = "//onlineUrlToMyApiSwagger.yaml
project.setPropertyValue("swaggerAPPvalidationPath", swaggerAPPvalidationPath)

 

And I add this part then in my assertion like this: https:${#Project#swaggerAPPvalidationPath}

JoostDG_0-1624613733786.png

 

Which now seem to work as expected!

JoostDG_1-1624613766934.png

 

EDIT: above solution did not work properly for files on a linux machine. A proper solution has been given by nmrao below in the form of a setup script.

 

JoostDG
Frequent Contributor

Re: How to make swagger compliance assertion url dynamic

Me again... not trying to make a habit of replying to my own posts to much... but for the sake of being transparent:

Path1 mentioned above, which is a relative path pointing to a yaml file present in my project repo, DOES NOT WORK when run on a Linux machine. The swagger validation assertion url \${#Project#swaggerENGvalidationPath} works on windows, but on linux he can't find that path. Tried double escaping like this \\${#Project#swaggerENGvalidationPath} but nope... 

 

So close... 

 

For now I disabled those swagger validations assertions if the OS is linux via the ReadyTools.isLinux()  (import com.smartbear.ready.util.ReadyTools).

nmrao
Community Hero

Re: How to make swagger compliance assertion url dynamic

What is the value of "testDataPath" in both OS'es?


Regards,
Rao.
JoostDG
Frequent Contributor

Re: How to make swagger compliance assertion url dynamic

Via project setup script:

import java.io.File

def testDataPath = project.path + File.separator + "datasource"

def swaggerENGversionForValidation = "swaggerMyApplicationV1.2.3"

def swaggerENGvalidationPath = testDataPath + File.separator + "swagger" + File.separator + "ENG" + File.separator + swaggerENGversionForValidation + ".yaml"

 

So the actual full path for swaggerENGvalidationPath is for example on windows: C:\git\automated-api-tests\myReadyApiProject\datasource\swagger\ENG\swaggerMyApplicationV1.2.3.yaml. This way of defining the path works, as I successfully use it in my script library. Also on Linux it can find the other files in my datasource folder when I call upon it in a script. But it's just the way the swagger validation assertion input transforms the "url" I provide in there that makes it goes wrong.

 

From linux logs : ASSERTION FAILED -> Swagger Compliance check failed: [Could not find file:

  •  A url like this \${#Project#swaggerENGvalidationPath} goes to:

/linux/folders/bamboo/agent-1/xml-data/build-dir/TEST/%5C//linux/folders/bamboo/agent-1/xml-data/build-dir/TEST/myReadyApiProject/datasource/swagger/ENG/swaggerMyApplicationV1.2.3.yaml

  • A url like this \\${#Project#swaggerENGvalidationPath} goes to:

/linux/folders/bamboo/agent-1/xml-data/build-dir/TEST/%5C%5C/linux/folders/bamboo/agent-1/xml-data/build-dir/TEST/myReadyApiProject/datasource/swagger/ENG/swaggerMyApplicationV1.2.3.yaml.

 

Both work on windows.

nmrao
Community Hero

Re: How to make swagger compliance assertion url dynamic

@JoostDG 

I would use something like below to build the path, more of a convenient. +, " little noisy.

 

 

def SEP = File.separator
def swaggerENGvalidationPath = "${testDataPath}${SEP}swagger${SEP}ENG${SEP}${swaggerENGversionForValidation}.yaml"

 

 

In the assertion, just use below.  No "\"  are required as prefix.

${#Project#swaggerENGvalidationPath} 

 

If the above does not help, then just try to log  and see the path is correct.

log.info ${testDataPath}



Regards,
Rao.
JoostDG
Frequent Contributor

Re: How to make swagger compliance assertion url dynamic

Hi Rao.

Thanks for the suggestions, but when I define it like you say and assert against ${#Project#swaggerENGvalidationPath}  (without \) I get below error on Windows: illegalArgumentException: input "#" is not an absolute JSON Reference. That's the same error I got for my initial attempts when I left out the backslash. 

JoostDG_0-1625044141060.png

I tried to include the "\" into the path itself like this project.setPropertyValue("swaggerENGvalidationPath", "\\"+swaggerENGvalidationPath) and this project.setPropertyValue("swaggerENGvalidationPath", "\\\\"+swaggerENGvalidationPath), but always the same error. It looks like a \ is needed but that only works on windows. Haven't tried on linux yet, but idea is to have a working solution in both OSs.

I can't put code in the url assertion, otherwise I'd try to do there an "if (!isLinux() ) { add a \ in front of the swaggerENGvalidationPath } else { no \}

 

Would be nice if there would be a possibility to do a "get data" in this assertions like in many others, so it's easy to point to a relative path instead of an absolute url.

nmrao
Community Hero

Re: How to make swagger compliance assertion url dynamic

Looks it is not able to identify by the property expansion whether it is file or url as value will be available only after expanding.
At least it will know the other case of yours as the value starts with "http".

I think worth trying to prefix "file://" before. i.e.
file://${#Project#swaggerENGvalidationPath}

Please see below for more details.
https://stackoverflow.com/questions/27845223/whats-the-difference-between-a-resource-uri-url-path-an...


Regards,
Rao.
JoostDG
Frequent Contributor

Re: How to make swagger compliance assertion url dynamic

Unfortunately, no:

JoostDG_0-1625063854701.png

Even when appending swaggerENGvalidationPath property to "//" and then in the assertion setting file:${#Project#swaggerENGvalidationPath} it doesn't do it.

 

nmrao
Community Hero

Re: How to make swagger compliance assertion url dynamic

Thanks for trying that.
Would you like to test it on linux and see if that works there?


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors