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

[TT-13109]Generate New Swagger and Update Validator for Gateway #6231

Merged
merged 44 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
06a05b0
add documentaion for generating the swagger
Keithwachira Apr 18, 2024
153d0b0
add the oas defination
Keithwachira Apr 18, 2024
dc76322
change to h2 from h3
Keithwachira Apr 18, 2024
4717967
Update README.md
Keithwachira Apr 18, 2024
ba2a4e2
add github action to lint with redocly
Keithwachira Apr 19, 2024
e61b24a
test if document willl validate
Keithwachira Apr 19, 2024
c3240af
change to push
Keithwachira Apr 19, 2024
9c3c587
change to ubuntu latest
Keithwachira Apr 19, 2024
acd0872
add node and redocly cli
Keithwachira Apr 19, 2024
010a9f7
remove example requirement in scalar items
Keithwachira Apr 19, 2024
095f6ee
Update README.md
Keithwachira Apr 24, 2024
1f03a5e
generate Oas array as an external reference instead of local object
Keithwachira Apr 29, 2024
8628de2
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Apr 29, 2024
53b401c
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Aug 5, 2024
6bc4b1b
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Aug 15, 2024
f21d9aa
add example to swagger file and rename swagger
Keithwachira Aug 15, 2024
1a175ff
Update README.md
Keithwachira Aug 16, 2024
ad26a23
Update tyk-api-documentation/README.md
Keithwachira Aug 16, 2024
a90b8e1
add checkout version 4
Keithwachira Aug 16, 2024
fc1ae06
add checkout version 4
Keithwachira Aug 16, 2024
bc3e323
remove old swagger file
Keithwachira Aug 16, 2024
4c77afe
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Aug 28, 2024
933d5bb
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Sep 5, 2024
bac2ffe
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Sep 5, 2024
438df67
change from oas to tyk oas (#6498)
Keithwachira Sep 11, 2024
fd05934
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Sep 11, 2024
5b05b5c
fix naming of oas (#6501)
Keithwachira Sep 11, 2024
3f3a865
change policy definition (#6503)
Keithwachira Sep 11, 2024
0e383e4
fix grammar issues (#6527)
Keithwachira Sep 17, 2024
cc77d89
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Sep 17, 2024
4d34d0e
replace previous swagger
Keithwachira Sep 17, 2024
05ab257
add redocly to path
Keithwachira Sep 17, 2024
afa4252
remove duplicate files
Keithwachira Sep 17, 2024
af4d3de
Merge branch 'master' into document-process-to-generate-the-oas
Keithwachira Sep 17, 2024
c4a7def
remove multiple clis and leave only one
Keithwachira Sep 17, 2024
ab7e45b
Remove redocly double linter (#6532)
Keithwachira Sep 17, 2024
89b1a00
remove multiple clis and leave only one (#6533)
Keithwachira Sep 17, 2024
6194397
Merge branch 'document-process-to-generate-the-oas' of github.com:Tyk…
Keithwachira Sep 17, 2024
cf1e45f
Add new line (#6536)
Keithwachira Sep 17, 2024
b4746a6
Merge branch 'document-process-to-generate-the-oas' of github.com:Tyk…
Keithwachira Sep 17, 2024
cbd6d3b
Fix grammar in docs file (#6537)
Keithwachira Sep 17, 2024
0b3b319
Update swagger.md
Keithwachira Sep 17, 2024
578bd9c
Merge branch 'document-process-to-generate-the-oas' of github.com:Tyk…
Keithwachira Sep 17, 2024
d2b7d9e
change yaml to yml
Keithwachira Sep 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 95 additions & 9 deletions .github/workflows/lint-swagger.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,103 @@
# yamllint disable rule:line-length rule:truthy
---
name: "Lint swagger schema"

on:
pull_request:
branches:
- master
- release-**
paths:
- 'swagger.yml'

jobs:
test_swagger_editor_validator_remote:
redocly_validator:
runs-on: ubuntu-latest
name: Validate the swagger with redocly cli

steps:
- uses: actions/checkout@v4
titpetric marked this conversation as resolved.
Show resolved Hide resolved
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Validate OpenAPI definition with redocly
run: |
npm install @redocly/cli -g
redocly lint swagger.yml --config=redocly.yml

diff_swagger:
name: Diff swagger yaml for comment
runs-on: ubuntu-latest
name: Swagger Editor Validator Remote

steps:
- uses: actions/checkout@v2
- name: Validate OpenAPI definition
uses: char0n/swagger-editor-validate@v1
- name: Use GitHub Token
env:
TOKEN: ${{ secrets.ORG_GH_TOKEN }}
run: >
git config --global url."https://${TOKEN}@github.com".insteadOf "https://github.com"

- name: Checkout repo
uses: TykTechnologies/github-actions/.github/actions/checkout-pr@main
with:
token: ${{ secrets.ORG_GH_TOKEN }}

- name: Setup Golang
uses: actions/setup-go@v5
with:
go-version: '1.22.x'

- name: Install dyff binary
run: |
# this is the upstream, missing -w (trim whitespace)
# curl --silent --location https://git.io/JYfAY | bash
git clone --depth=1 https://github.com/aoktox/dyff
cd dyff && go install ./cmd/...

- name: Set up comment author
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"

- name: Collect
run: |
cp swagger.yml swagger-current.yml

git checkout -- .
git fetch --depth=1 origin ${{ github.base_ref }}
git checkout ${{ github.base_ref }}

cp swagger.yml swagger-prev.yml

- name: Diff
id: api-check
run: |
set +e
dyff between -c on --ignore-whitespace-changes -i swagger-prev.yml swagger-current.yml
dyff between -c off --ignore-whitespace-changes -i swagger-prev.yml swagger-current.yml | egrep -v '^ . ' | sort > changes.txt

LINE_COUNT=$(wc -l < changes.txt)

echo "diff-output<<EOF" >> $GITHUB_OUTPUT
if [ $LINE_COUNT -gt 200 ]; then
echo "Changes in swagger.yml too large (line count ${LINE_COUNT}), check CI lint action for differences" >> $GITHUB_OUTPUT
else
cat changes.txt >> $GITHUB_OUTPUT
fi
echo "EOF" >> $GITHUB_OUTPUT

- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: Swagger Changes

- name: Create or update comment
uses: peter-evans/create-or-update-comment@v3
with:
definition-file: swagger.yml
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Swagger Changes
```diff
${{ steps.api-check.outputs.diff-output || 'no api changes detected' }}
```
edit-mode: replace
42 changes: 42 additions & 0 deletions .redocly.lint-ignore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# We have an external Swagger file that we use for Tyk OAs Endpoints this file ignores that external file
# We have ambiguous-paths that would require to change the endpoint that the gateway level.
#Those are also ignored (Currently only one of those exists)
swagger.yml:
titpetric marked this conversation as resolved.
Show resolved Hide resolved
no-ambiguous-paths:
- '#/paths/~1tyk~1apis~1oas~1{apiID}'
no-invalid-media-type-examples:
- >-
#/paths/~1tyk~1apis~1oas/get/responses/200/content/application~1json/schema
- '#/paths/~1tyk~1apis~1oas/post/requestBody/content/application~1json/schema'
- >-
#/paths/~1tyk~1apis~1oas~1{apiID}/get/responses/200/content/application~1json/schema
- >-
#/paths/~1tyk~1apis~1oas~1{apiID}/put/requestBody/content/application~1json/schema
- >-
#/paths/~1tyk~1apis~1oas~1{apiID}/patch/requestBody/content/application~1json/schema
- >-
#/paths/~1tyk~1apis~1oas~1import/post/requestBody/content/application~1json/schema
https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.json:
spec:
- '#/id'
- '#/$schema'
- '#/patternProperties'
- '#/definitions'
- '#/definitions/Info/patternProperties'
- '#/definitions/Contact/patternProperties'
- '#/definitions/License/patternProperties'
- '#/definitions/ExternalDocumentation/patternProperties'
- '#/definitions/Server/patternProperties'
- '#/definitions/ServerVariable/patternProperties'
- '#/definitions/Tag/patternProperties'
- '#/definitions/Paths/patternProperties'
- '#/definitions/Components/patternProperties'
- '#/definitions/Components/properties/schemas/patternProperties'
- '#/definitions/Components/properties/responses/patternProperties'
- '#/definitions/Components/properties/parameters/patternProperties'
- '#/definitions/Components/properties/examples/patternProperties'
- '#/definitions/Components/properties/requestBodies/patternProperties'
- '#/definitions/Components/properties/headers/patternProperties'
- '#/definitions/Components/properties/securitySchemes/patternProperties'
- '#/definitions/Components/properties/links/patternProperties'
- '#/definitions/Components/properties/callbacks/patternProperties'
34 changes: 34 additions & 0 deletions docs/swagger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Tyk API Documentation

The **Tyk API Documentation** is an OpenAPI specification that outlines the endpoints you can call on the gateway.

## Notes

The current OAS version is **v3.0.3**, generated from gateway **v5.6.0**.

We are currently using OpenAPI Specification version **v3.0.3**.

For linting the OAS, we use Redocly. You can install Redocly from [here](https://github.com/Redocly/redocly-cli).

## How the OAS was generated

We used the Go library [openapi-go](https://github.com/swaggest/openapi-go) because:
1. It supports generating OAS for version **v3.0.3** (there are not many Go libraries that produce OAS v3.0.3).
2. It is highly customizable, allowing you to define how each field is generated.
3. It lets you write the generation code as functions, making it easier to read and maintain.

## How to generate the Swagger.yml file

1. Ensure that Redocly is installed on your system. You can install Redocly from [here](https://github.com/Redocly/redocly-cli).
2. Clone the gateway repository and check out the branch [generate-swagger](https://github.com/TykTechnologies/tyk/tree/generate-swagger).
3. Navigate to the directory [swagger/cmd](https://github.com/TykTechnologies/tyk/tree/generate-swagger/swagger/cmd). This directory contains a Makefile with a rule (`generate`) used to generate the swagger.yml file.
4. Run the command `make generate` in this directory.
5. After running this command, a `swagger.yml` file containing all the gateway endpoints will be generated in the same directory.
6. What the `make generate` command does:
- The command is defined as: `rm -f swagger.yml && go run main.go && redocly lint swagger.yml`.
- It first removes the existing `swagger.yml` file (if any), generates a new `swagger.yml` file, and finally lints the file using Redocly.

## File Structure

1. In the Swagger folder, there is a file for each OAS tag (e.g., cache tag, key tag, etc.). If you want to add a new endpoint, add it to its specific tag.
2. The `cmd` directory contains the `main.go` file (used to call all the generation functions), a Makefile, and the generated `swagger.yml` file.
56 changes: 56 additions & 0 deletions redocly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
rules:
#Special rules
no-unresolved-refs: error
no-unused-components: warn
security-defined: error
spec: error
spec-components-invalid-map-name: error #All the fixed fields declared below are objects that MUST use keys that match the regular expression
spec-strict-refs: error
#info
info-contact: warn
info-license: warn
info-license-url: warn
#Operations
operation-operationId: error
operation-operationId-unique: error
operation-operationId-url-safe: error
operation-summary: error
operation-2xx-response: error
operation-4xx-response: error
operation-description: error
#Parameters
no-invalid-parameter-examples:
severity: error
allowAdditionalProperties: false
operation-parameters-unique: error
#parameter-description: error #uncomment this
path-declaration-must-exist: error
path-parameters-defined: error
#path rules
no-ambiguous-paths: error
no-http-verbs-in-paths:
severity: warn
splitIntoWords: true
no-identical-paths: error
no-path-trailing-slash: error

#Requests, Responses, and Schemas rules
no-enum-type-mismatch: error
no-example-value-and-externalValue: error
no-invalid-media-type-examples:
severity: error
allowAdditionalProperties: false
#scalar-property-missing-example:
#severity: warn
no-invalid-schema-examples:
severity: error
allowAdditionalProperties: false
#Server rules
no-empty-servers: error
no-server-trailing-slash: error
no-undefined-server-variable: error
#Tags rules
operation-singular-tag: error
#operation-tag-defined: error


Loading
Loading