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

Problem with bool variable setted by pre-request script #286

Closed
jdevosMagento opened this issue Mar 22, 2022 · 13 comments
Closed

Problem with bool variable setted by pre-request script #286

jdevosMagento opened this issue Mar 22, 2022 · 13 comments
Assignees
Labels
enhancement New feature or request

Comments

@jdevosMagento
Copy link

I have a new problem.

This time, it is the use of a variable defined by a prerequest script and boolean type.

My API documentation:

attribute_1:
           type: boolean
           example: true

My overwriteRequestBody:

{
             "key": "data[0].attribute_1",
             "value": "{{attribute_1_value}}",
             "overwrite": true
},

I am forced to define value as a string, otherwise the json is invalid and portman shows json format error.

But by doing this, it integrates the attribute as a string also in postman and the call is therefore in failure because the API expects a boolean and not a string.

Postman:

"data": [
{
     "attribute_1": "{{attribute_1_value}}"
}

The API normally expects: (without quotes)

"data": [
{
     "attribute_1": {{attribute_1_value}}
}

I think it would be necessary, when generating the postman collection, to check if value is a variable and if so, cast according to a "type" attribute for example

@thim81
Copy link
Collaborator

thim81 commented Mar 22, 2022

Hi @jdevosMagento

We have support for "{{$randomInt}}" which gets converted into number (not a string).
I'll have to investigate how the booleans are handled, in combination with {{postman_variables}}.
The problem is that there is no way that Portman can know the type of a variable, unless we use the OpenAPI definition.

@thim81
Copy link
Collaborator

thim81 commented Mar 23, 2022

@jdevosMagento What do you think about this proposal:

Postman Variable: marco_polo (which is a Boolean or Number, not string) is used like {{marco_polo}}
If you define the variable in an overwrite in Portman, you would set it like "{{{marco_polo}}}" surrounded by 3x {{{ and 3x }}}.
Portman can use this to detect the variable as a non-string variable and converts it into {{marco_polo}} with " with only 2x {{}} making it a valid Postman variable.

I ll still explore type detection based on OpenApi but this {{{}}} syntax allows the users to indicate a non-string type manually.

Cc @nicklloyd, what is your opinion?

@jdevosMagento
Copy link
Author

Hello @thim81 yes sounds good to me!

Otherwise why not add a "type" variable when you want to force the type :

{
             "key": "data[0].attribute_1",
             "value": "{{attribute_1_value}}",
             "type": "boolean"
             "overwrite": true
},

In this case, if "type" is specified portman uses this value to define how it should return the data.

But your proposal seems just as good

@thim81
Copy link
Collaborator

thim81 commented Mar 23, 2022

Hi @jdevosMagento,

I thought about the "type" setting option, but that would mean more configuration and complexity for the Portman users to learn and setup, while the goal is always to facilitate easy configuration of Portman but with enough flexibility.

The {{{}}} provides the control by using an "escape" syntax, where you tell Portman to escape the "string" type and use it as a raw type.

I'll turn this into a feature request and see when there is time to introduce the feature.

@thim81 thim81 added the enhancement New feature or request label Mar 23, 2022
@thim81 thim81 self-assigned this Mar 23, 2022
@jdevosMagento
Copy link
Author

It's perfect, thank you

@thim81
Copy link
Collaborator

thim81 commented Mar 25, 2022

@jdevosMagento The PR is ready and will be reviewed.

An example of the result of the conversion with the escaped number/boolean types.

"overwrites": [
    {
      "openApiOperation": "*::/*",
      "overwriteRequestBody": [
        {
          "key": "data[0].key_1",
          "value": "{{attributeString}}",
          "overwrite": true
        },
        {
          "key": "data[0].key_2",
          "value": "{{{attributeNumber}}}",
          "overwrite": true
        },
        {
          "key": "data[0].key_3",
          "value": "{{{attributeBoolean}}}",
          "overwrite": true
        },
        {
          "key": "data[0].key_4",
          "value": "Marco Polo {{attributeNumber}}",
          "overwrite": true
        },
        {
          "key": "data[1]",
          "remove": true
        }
      ]
    }
  ]

2022-03-25 at 13 13 07@2x

@jdevosMagento
Copy link
Author

It's great the result is perfect, thank you for the efficiency!

@thim81
Copy link
Collaborator

thim81 commented Mar 28, 2022

@jdevosMagento We just released version 1.15.0 of Portman, where this enhancement is included.
Closing this issue.

@thim81 thim81 closed this as completed Mar 28, 2022
@jdevosMagento
Copy link
Author

jdevosMagento commented Mar 29, 2022

Hello, i think there is a problem when using variationTests and overwriteRequestBody

    "overwrites": {
        "openApiOperationId": "Xxxx",
        "overwriteRequestBody": [
            {
                "key": "data[0].yyy",
                "value": "{{yyy}}",
                "overwrite": true
            },
            {
                "key": "data[0].xxx",
                "value": "{{{xxx}}}",
                "overwrite": true
            }
        ]
    },
    "variationTests": {
        "openApiOperationId": "Xxxx",
        "variations": [
            {
                "name": "BadRequest",
                "openApiResponse": "400",
                "overwrites": [
                    {
                        "overwriteRequestBody": [
                            {
                                "key": "data[0].yyy",
                                "remove": true
                            }
			]
                    }
                ],
                "tests": {
                    "contractTests": [
                        {
                            "statusCode": {
                                "enabled": true
                            },
                            "jsonBody": {
                                "enabled": true
                            },
                            "schemaValidation": {
                                "enabled": true
                            }
                        }
                    ]
                }
            },
        ]
    }
}

i have this error :

(node:3391829) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token { in JSON at position 116
    at JSON.parse (<anonymous>)
    at Object.overwriteRequestBody (/home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/overwrites/overwriteRequestBody.js:17:25)
    at /home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/overwrites/applyOverwrites.js:8:16
    at Array.map (<anonymous>)
    at Object.applyOverwrites (/home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/overwrites/applyOverwrites.js:6:25)
    at /home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/TestSuite.js:228:20
    at Array.map (<anonymous>)
    at TestSuite.injectOverwrites (/home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/TestSuite.js:226:22)
    at VariationWriter.injectVariations (/home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/VariationWriter.js:84:28)
    at /home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/VariationWriter.js:44:23
    at Array.map (<anonymous>)
    at VariationWriter.add (/home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/VariationWriter.js:25:20)
    at /home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/TestSuite.js:46:47
    at Array.map (<anonymous>)
    at /home/jdevos/.nvm/versions/node/v14.18.1/lib/node_modules/@apideck/portman/dist/application/TestSuite.js:43:30
    at Array.map (<anonymous>)

No problem without variationTests, the boolean xxx its OK

@thim81
Copy link
Collaborator

thim81 commented Mar 29, 2022

@jdevosMagento Are you able to share the OpenAPI specification (in a private way if required)? So we can try to reproduce the issue?

@jdevosMagento
Copy link
Author

Its Ok with this ?

---
openapi: 3.0.3
info:
  version: "1.0.0"
  title: Xxx
  description: xxx
tags:
  - name: xxxx
    description: xxxx
paths:
/nnnnn/vvvvv:
	post:
	  operationId: xxxx
	  description: "xxxxx"
	  tags:
	    - xxxx
	  requestBody:
	    content:
	      application/json:
		schema:
			type: object
			properties:
			  data:
			    type: array
			    items:
			      type: object
			      properties:
				yyy:
				  type: string
				  example: "YYY"
				xxx:
				  type: integer
				  example: 10
			      required:
				- yyy
				- xxx
			required:
			  - data

	  responses:
	    "200":
	      description: Successful operation
	      content:
		application/json:
		  schema:
			type: object
			properties:
			  aaaa:
			    type: boolean
			    example: true
			  bbbb:
			    type: array
			    items:
			      $ref: "#/components/schemas/bbbbbb"
			  cccc:
			    type: array
			    items:
			      $ref: "#/components/schemas/cccccc"
	    "400":
	      description: 400 Bad Request
	      content:
		application/json:
		  schema:
		    $ref: "#/components/schemas/dddddddd"

@thim81 thim81 reopened this Mar 29, 2022
@thim81
Copy link
Collaborator

thim81 commented Apr 1, 2022

@jdevosMagento We have found the cause and the PR with a fix will be part of the next release.

@thim81
Copy link
Collaborator

thim81 commented Apr 1, 2022

Portman 1.15.1 has just been released with the fix for this behaviour included.

@thim81 thim81 closed this as completed Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants