Skip to content

Commit

Permalink
Merge pull request #5 from sergeyklay/feature/examples
Browse files Browse the repository at this point in the history
Reformat OpenAPI spec
  • Loading branch information
sergeyklay authored Feb 6, 2023
2 parents 5a9cb46 + 68e6ece commit 30608a1
Show file tree
Hide file tree
Showing 11 changed files with 7,079 additions and 72 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ updates:
interval: daily
assignees:
- sergeyklay

- package-ecosystem: npm
# package.json stored in repository root.
directory: /
schedule:
interval: daily
assignees:
- sergeyklay
6 changes: 4 additions & 2 deletions .github/workflows/contracts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Check Contracts
on: [push]

jobs:
hello_world_job:
check-contracts:

runs-on: ${{ matrix.os }}
name: Test Contracts
Expand All @@ -20,8 +20,10 @@ jobs:
- windows-latest

steps:
- name: Checkout
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: '0'

- name: Set up Python 3.11
uses: actions/setup-python@v4
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Lint OpenAPI

on:
pull_request:
branches: [main]
paths-ignore:
- '**.md'
push:
paths-ignore:
- '**.md'

jobs:
lint-openapi:
name: Lint OpenAPI spec
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: '0'

- name: Setup Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: npm

- name: Install dependencies
run: npm ci --ignore-scripts

- name: Install linters
run: npm run lint
6 changes: 4 additions & 2 deletions .github/workflows/versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Validate Action
on: [push]

jobs:
hello_world_job:
validate-action:

runs-on: ${{ matrix.os }}
name: Test Specmatic Installation
Expand All @@ -20,8 +20,10 @@ jobs:
- windows-latest

steps:
- name: Checkout
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: '0'

- name: Check cache before install
if: matrix.os != 'windows-latest'
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# Directories to ignore (do not add trailing '/'s, they skip symlinks):
# Dependency directory
/node_modules

# Python cache.
*.py[cod]
__pycache__
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Check Contracts](https://github.com/sergeyklay/specmatic-testing-example/actions/workflows/contracts.yaml/badge.svg)](https://github.com/sergeyklay/specmatic-testing-example/actions/workflows/contracts.yaml)
[![Validate Action](https://github.com/sergeyklay/specmatic-testing-example/actions/workflows/versions.yaml/badge.svg)](https://github.com/sergeyklay/specmatic-testing-example/actions/workflows/versions.yaml)
[![Lint OpenAPI](https://github.com/sergeyklay/specmatic-testing-example/actions/workflows/lint.yaml/badge.svg)](https://github.com/sergeyklay/specmatic-testing-example/actions/workflows/lint.yaml)

## How to try it out

Expand Down
94 changes: 52 additions & 42 deletions contracts/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ openapi: 3.0.3

info:
version: '1.0'
title: Products API
title: Products API Definition
description: Example API to test Specmatic features.
license:
name: MIT License
url: https://opensource.org/licenses/MIT

servers:
- description: Localhost
Expand All @@ -15,46 +18,16 @@ tags:
description: Products related API.

components:
parameters:
AcceptHeader:
in: header
name: Accept
description: Accept Header
schema:
type: string
enum: [application/json, '*/*']
example: application/json
IdParam:
in: path
name: id
description: Entity ID param
schema:
type: integer
format: int32
minimum: 1
required: true
example: 42

schemas:
Product:
title: Product
allOf:
- $ref: '#/components/schemas/ProductId'
- $ref: '#/components/schemas/ProductDetails'
ProductId:
title: Product ID
type: object
properties:
id:
type: integer
format: int32
minimum: 1
example: 42
required: [id]
ProductDetails:
title: Product Details
type: object
properties:
title:
type: string
example: iPhone 9
Expand Down Expand Up @@ -85,6 +58,7 @@ components:
category:
$ref: '#/components/schemas/ProductCategory'
required:
- id
- title
- description
- price
Expand All @@ -104,25 +78,31 @@ components:
paths:
/v1/products:
get:
summary: List of all products.
summary: Get products
description: Returns a list of all products.
operationId: products.index
tags: [products]
parameters:
- $ref: '#/components/parameters/AcceptHeader'
- name: category
- in: query
name: category
description: Filter by product category.
in: query
required: false
schema:
type: string
example: laptops
- name: q
examples:
single:
value: laptops
summary: A sample category to filter products.
- in: query
name: q
description: Search term.
in: query
required: false
schema:
type: string
example: samsung
examples:
term:
value: Samsung
summary: A sample search keyword.
responses:
'200':
description: List of products
Expand All @@ -132,15 +112,45 @@ paths:
type: array
items:
$ref: '#/components/schemas/Product'
examples:
products:
value:
- id: 1
title: iPhone 9
description: An apple mobile which is nothing like apple
price: 549
discount: 12.96
rating: 4.69
stock: 94
brand: Apple
category: smartphones
- id: 6
title: Samsung Galaxy Book
description: Samsung Galaxy Book S (2020) Laptop With Intel Lakefield Chip, 8GB of RAM Launched
price: 1499
discount: 4.15
rating: 4.25
stock: 50
brand: Samsung
category: laptops
summary: A sample list of products.

'/v1/products/{id}':
get:
summary: Get product.
summary: Get single product
description: Returns a single product.
operationId: products.get
tags: [products]
parameters:
- $ref: '#/components/parameters/AcceptHeader'
- $ref: '#/components/parameters/IdParam'
- in: path
name: id
description: Entity ID param
schema:
type: integer
format: int32
minimum: 1
required: true
example: 42
responses:
'200':
description: Get product
Expand Down
40 changes: 14 additions & 26 deletions contracts/products.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,43 @@ Feature: Products API

Scenario Outline: Bad request when getting the product by ID
When GET /v1/products/(id:number)
And request-header Accept? (string)
Then status 400
Examples:
| id | Accept |
| 1.1 | application/json |
| id |
| 1.1 |

Scenario Outline: Product not found
When GET /v1/products/(id:number)
And request-header Accept? (string)
Then status 404
Examples:
| id | Accept |
| 100 | application/json |
| id |
| 100 |

Scenario Outline: Successful getting product by ID
When GET /v1/products/(id:number)
And request-header Accept? (string)
Then status 200
Examples:
| id | Accept |
| 1 | application/json |
| id |
| 1 |

Scenario Outline: Successful getting list of products
When GET /v1/products
And request-header Accept? (string)
Then status 200
Examples:
| Accept |
| application/json |
| */* |

Scenario Outline: Successful getting list of products in a given category
When GET /v1/products?category=(category:string)
And request-header Accept? (string)
Then status 200
Examples:
| Accept | category |
| application/json | smartphones |
| application/json | laptops |
| */* | smartphones |
| */* | |
| category |
| smartphones |
| laptops |
| |

Scenario Outline: Successful getting list of products using search term
When GET /v1/products?q=(q:string)
And request-header Accept? (string)
Then status 200
Examples:
| Accept | q |
| application/json | phone |
| application/json | |
| */* | HP |
| */* | |
| q |
| phone |
| HP |
| |
Loading

0 comments on commit 30608a1

Please sign in to comment.