Skip to content

Commit

Permalink
demo: add examples qs warning/add req body properties
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Oct 2, 2024
1 parent a08ac4c commit 7972597
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/example-contracts/EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,60 @@ The de-referenced version of the OAS contract will be output to 'resolved.json'
npm i
npx @pactflow/swagger-mock-validator <path-to-oas-file> <path-to-pact-file> --additionalPropertiesInResponse false
```

## Additional Examples

### Query Strings

Demonstrate consumer query strings encoded in Pact file, but not described in OAD, returns a warning from SMV

Change:- Addition of `id=2` query string in Pact file.

```
$ npx @pactflow/swagger-mock-validator test/example-contracts/products.yml test/example-contracts/queryString/v2/query_param_pact_v2.json
0 error(s)
1 warning(s)
request.query.unknown: 1
{
warnings: [
{
code: 'request.query.unknown',
message: 'Query parameter is not defined in the spec file: id',
mockDetails: {
interactionDescription: 'displays product item by query',
interactionState: '[none]',
location: '[root].interactions[0].request.query.id',
mockFile: 'test/example-contracts/queryString/v2/query_param_pact_v2.json',
value: '2'
},
source: 'spec-mock-validation',
specDetails: {
location: '[root].paths./products.get',
pathMethod: 'get',
pathName: '/products',
specFile: 'test/example-contracts/queryString/products.yml',
value: {
summary: 'List all products',
description: 'Returns all products',
operationId: 'getAllProducts',
responses: [Object]
}
},
type: 'warning'
}
],
errors: []
}
```

### Extra request properties

Demonstrate consumer request body additional fields encoded in Pact file, but not described in OAD, returns **NO** error/warning from SMV

Change:- Addition of `foo` property, with `string` type in Pact file.

```
$ npx @pactflow/swagger-mock-validator test/example-contracts/products.yml test/example-contracts/extraRequestProperty/v2/addtional_request_body_property_v2.json -A true
0 error(s)
0 warning(s)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"consumer": {
"name": "DefaultApi-consumer"
},
"interactions": [
{
"description": "A POST request for createProduct",
"providerStates": [
{
"name": "createProduct is available on DefaultApi service"
}
],
"request": {
"body": {
"id": "1234",
"name": "burger",
"price": 42,
"type": "food",
"foo": "bar"
},
"headers": {
"Content-Type": "application/json"
},
"method": "POST",
"path": "/products"
},
"response": {
"body": {
"id": "1234",
"name": "burger",
"price": 42,
"type": "food"
},
"headers": {
"Content-Type": "application/json;charset=utf-8"
},
"status": 200
}
}
],
"metadata": {
"pact-js": {
"version": "12.5.0"
},
"pactRust": {
"ffi": "0.4.20",
"models": "1.2.0"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "DefaultApi"
}
}
116 changes: 116 additions & 0 deletions test/example-contracts/products.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
openapi: 3.0.1
info:
title: Product API
description: PactFlow Product API demo
version: 1.0.0
paths:
/products:
post:
summary: Create a product
description: Creates a new product
operationId: createProduct
requestBody:
description: Create a new Product
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
id: "1234"
type: "food"
price: 42
responses:
"200":
description: successful operation
content:
"application/json; charset=utf-8":
schema:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
id: "1234"
type: "food"
price: 42
get:
summary: List all products
description: Returns all products
operationId: getAllProducts
responses:
"200":
description: successful operation
content:
"application/json; charset=utf-8":
schema:
type: "array"
items:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
- id: "1234"
type: "food"
price: 42
# name: "pizza"
# version: "1.0.0"
# see https://github.com/apiaryio/dredd/issues/1430 for why
"400":
description: Invalid ID supplied
content: {}
/product/{id}:
get:
summary: Find product by ID
description: Returns a single product
operationId: getProductByID
parameters:
- name: id
in: path
description: ID of product to get
schema:
type: string
required: true
example: 10
responses:
"200":
description: successful operation
content:
"application/json; charset=utf-8":
schema:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
id: "1234"
type: "food"
price: 42
# name: "pizza"
# version: "1.0.0"
# see https://github.com/apiaryio/dredd/issues/1430 for why
"400":
description: Invalid ID supplied
content: {}
"404":
description: Product not found
content: {}
components:
schemas:
Product:
type: object
required:
- id
- name
- price
properties:
id:
type: string
type:
type: string
name:
type: string
version:
type: string
price:
type: number
59 changes: 59 additions & 0 deletions test/example-contracts/queryString/v2/query_param_pact_v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"consumer": {
"name": "pactflow-example-bi-directional-consumer-cypress"
},
"provider": {
"name": "pactflow-example-bi-directional-provider-postman"
},
"interactions": [
{
"description": "displays product item by query",
"providerState": "",
"request": {
"method": "GET",
"path": "/products",
"headers": {
"accept": "application/json, text/plain, */*",
"authorization": "Bearer 2024-03-13T18:05:40.611Z"
},
"body": "",
"query": "id=2"
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": [
{
"id": "09",
"type": "CREDIT_CARD",
"name": "Gem Visa",
"price": 99.99
},
{
"id": "10",
"type": "CREDIT_CARD",
"name": "28 Degrees",
"price": 49.49
},
{
"id": "11",
"type": "PERSONAL_LOAN",
"name": "MyFlexiPay",
"price": 16.5
}
]
}
}
],
"metadata": {
"pactSpecification": {
"version": "2.0.0"
},
"client": {
"name": "pact-cypress-adapter",
"version": "1.3.0"
}
}
}

0 comments on commit 7972597

Please sign in to comment.