Forum Discussion

JoostDG's avatar
JoostDG
Frequent Contributor
4 years ago

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.

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\"",...

 

 

  • nmrao's avatar
    nmrao
    4 years ago

    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

     

     

  • JoostDG's avatar
    JoostDG
    Frequent Contributor

    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}

     

    Which now seem to work as expected!

     

    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's avatar
      JoostDG
      Frequent Contributor

      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's avatar
        nmrao
        Champion Level 3
        What is the value of "testDataPath" in both OS'es?