Skip to content

Commit

Permalink
feat(openapi): support OpenAPI version 3.1 (extends PR #882) (#1027)
Browse files Browse the repository at this point in the history
* feat(openapi): support version 3.1

* test(openapi_3.1): ensure that an API with webhooks and no routes is supported

* feat(openapi_3.1): adds open api 3.1 type

* chore(test-scripts): run mocha with --extension instead of glob to pick up subdirectories

Mocha was not picking up the tests in subdirectories with the provided glob. Adding --extension with
the tests extension and setting the root test folder in tests fixed it

* test(openapi-3.1): adds test to ensure an API with only components is considered valid

* test(openapi-3.1): remove unnecessary import

* test(openapi-3.1): add support for summary in info object

* test(openapi-3.1): add support for identifier in license

* test(openapi_3.1): ensure API with type set to null works correctly

* test(open_api3.1): ensure that methods with non-explicit semantics allow request body

* test(open_api3.1): ensure 500 is returned when server variable has no default

* feat(openapi_3.1): ensure API supports an endpoint without response

* feat(openapi_3.1): add full type support for open api 3.1

* test(openapi_3.1): adds test for path item support in components

* fix(openapi_3.1_schema): update schema to fix bug

* feat(openapi_3.1): support reusable path items

* style(linting): fix linting issues

* style(openapi): improve readability of version validation

* docs(schema-validator): clearly state why media-range attribute is not defined

* version 6.0.0-alpha.1 with initial OAS-3.1 support (from PR #882)

* v6.0.0-alpha.2

* feat(openapi): support version 3.1

* test(openapi_3.1): ensure that an API with webhooks and no routes is supported

* feat(openapi_3.1): adds open api 3.1 type

* chore(test-scripts): run mocha with --extension instead of glob to pick up subdirectories

Mocha was not picking up the tests in subdirectories with the provided glob. Adding --extension with
the tests extension and setting the root test folder in tests fixed it

* test(openapi-3.1): adds test to ensure an API with only components is considered valid

* test(openapi-3.1): remove unnecessary import

* test(openapi-3.1): add support for summary in info object

* test(openapi-3.1): add support for identifier in license

* test(openapi_3.1): ensure API with type set to null works correctly

* test(open_api3.1): ensure that methods with non-explicit semantics allow request body

* test(open_api3.1): ensure 500 is returned when server variable has no default

* feat(openapi_3.1): ensure API supports an endpoint without response

* feat(openapi_3.1): add full type support for open api 3.1

* test(openapi_3.1): adds test for path item support in components

* fix(openapi_3.1_schema): update schema to fix bug

* feat(openapi_3.1): support reusable path items

* style(linting): fix linting issues

* style(openapi): improve readability of version validation

* docs(schema-validator): clearly state why media-range attribute is not defined

* version 6.0.0-alpha.1 with initial OAS-3.1 support (from PR #882)

* v6.0.0-alpha.2

* alpha.3

* feat(openapi): support version 3.1

* feat(openapi_3.1): adds open api 3.1 type

* fix(openapi_3.1_schema): update schema to fix bug

* style(linting): fix linting issues

* fix: instantiate Ajv2020 for OAS 3.1 (#1009)

* chore: create factories for ajvInstance and schema

* test: writing some tests

* chore: removing ts from editorconfig

* chore: add eslint

* update alpha 3.1 version

* fix: examples/4-eov-operations-babel/package.json & examples/4-eov-operations-babel/package-lock.json to reduce vulnerabilities (#1021)

The following vulnerabilities are fixed with an upgrade:
- https://snyk.io/vuln/SNYK-JS-PATHTOREGEXP-8482416

Co-authored-by: snyk-bot <[email protected]>

* fix: package.json & package-lock.json to reduce vulnerabilities (#1017)

The following vulnerabilities are fixed with an upgrade:
- https://snyk.io/vuln/SNYK-JS-PATHTOREGEXP-8482416

Co-authored-by: snyk-bot <[email protected]>

* chore(deps): bump path-to-regexp and express in /examples/1-standard (#1016)

Bumps [path-to-regexp](https://github.com/pillarjs/path-to-regexp) and [express](https://github.com/expressjs/express). These dependencies needed to be updated together.

Updates `path-to-regexp` from 0.1.10 to 0.1.12
- [Release notes](https://github.com/pillarjs/path-to-regexp/releases)
- [Changelog](https://github.com/pillarjs/path-to-regexp/blob/master/History.md)
- [Commits](pillarjs/path-to-regexp@v0.1.10...v0.1.12)

Updates `express` from 4.21.0 to 4.21.2
- [Release notes](https://github.com/expressjs/express/releases)
- [Changelog](https://github.com/expressjs/express/blob/4.21.2/History.md)
- [Commits](expressjs/express@4.21.0...4.21.2)

---
updated-dependencies:
- dependency-name: path-to-regexp
  dependency-type: indirect
- dependency-name: express
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* fix: upgrade express-openapi-validator from 5.3.7 to 5.3.8 (#1011)

Snyk has created this PR to upgrade express-openapi-validator from 5.3.7 to 5.3.8.

See this package in npm:
express-openapi-validator

See this project in Snyk:
https://app.snyk.io/org/cdimascio/project/53639b22-8ff0-4bd5-97c3-ae30b20a20f4?utm_source=github&utm_medium=referral&page=upgrade-pr

Co-authored-by: snyk-bot <[email protected]>

* fix: upgrade express-openapi-validator from 5.3.6 to 5.3.7 (#1008)

Snyk has created this PR to upgrade express-openapi-validator from 5.3.6 to 5.3.7.

See this package in npm:
express-openapi-validator

See this project in Snyk:
https://app.snyk.io/org/cdimascio/project/0ac9a5bd-9a7f-4c0e-bf8b-51d0bd4c4448?utm_source=github&utm_medium=referral&page=upgrade-pr

Co-authored-by: snyk-bot <[email protected]>

* fix: upgrade @apidevtools/json-schema-ref-parser from 11.7.0 to 11.7.2 (#1006)

Snyk has created this PR to upgrade @apidevtools/json-schema-ref-parser from 11.7.0 to 11.7.2.

See this package in npm:
@apidevtools/json-schema-ref-parser

See this project in Snyk:
https://app.snyk.io/org/cdimascio/project/f63fb44e-f154-45ba-b1f0-20d49ea578ce?utm_source=github&utm_medium=referral&page=upgrade-pr

Co-authored-by: snyk-bot <[email protected]>

* adds standard example for oas-3.1

* v6.0.0.alpha.6

* update oas3.1 example

* v5.4.0 - adds initial oas3.1 support

* updates readme for oas3.1

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Sergio Ferreira <[email protected]>
Co-authored-by: carmine <[email protected]>
Co-authored-by: Luis Philipe Fidelis <[email protected]>
Co-authored-by: snyk-bot <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
6 people authored Dec 25, 2024
1 parent a67802b commit fc876a3
Show file tree
Hide file tree
Showing 66 changed files with 5,673 additions and 168 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@
[NestJS](https://github.com/cdimascio/express-openapi-validator/tree/master/examples/9-nestjs)
[Koa](https://github.com/cdimascio/express-openapi-validator/tree/lerna-fastify/packages/koa-openapi-validator) and [Fastify](https://github.com/cdimascio/express-openapi-validator/tree/lerna-fastify/packages/fastify-openapi-validator) now available! 🚀

[OAS 3.1 (experimental)](https://github.com/cdimascio/express-openapi-validator/pull/882)
[OAS 3.1 (beta)](https://github.com/cdimascio/express-openapi-validator/pull/882) is available in v5.4.0!

## Install

```shell
npm install express-openapi-validator

# experimental OAS 3.1 in alpha (contributions welcome - see branch `oas-3.1` and pr-882
# please provide feedback on (issue-573)
npm install [email protected]
```

## Usage
Expand Down
172 changes: 172 additions & 0 deletions examples/1-standard-oas-3.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# example

simple example using express-openapi-validator

## Install

```shell
npm run deps && npm i
```

## Run

From this `1-standard` directory, run:

```shell
npm start
```

## Try

```shell
## call ping
curl http://localhost:3000/v1/ping

## call pets
## the call below should return 400 since it requires additional parameters
curl http://localhost:3000/v1/pets
```

## Text examples

### GET /pets

success
```shell
curl -s 'localhost:3000/v1/pets?limit=5' |jq
[
{
"id": 1,
"name": "sparky",
"type": "dog",
"tags": [
"sweet"
]
},
{
"id": 2,
"name": "buzz",
"type": "cat",
"tags": [
"purrfect"
]
},
{
"id": 3,
"name": "max",
"type": "dog",
"tags": []
}
]
```

error
```shell
curl -s 'localhost:3000/v1/pets' |jq
{
"message": "request/query must have required property 'limit'",
"errors": [
{
"path": "/query/limit",
"message": "must have required property 'limit'",
"errorCode": "required.openapi.validation"
}
],
"code": 400
}
```

### POST /pets

success
```shell
curl -s -XPOST 'localhost:3000/v1/pets' -d '{"id": 1, "name": "jobe"}' -H 'Content-type: application/json'|jq
{
"id": 1,
"name": "jobe"
}
```

error
```shell
curl -s -XPOST 'localhost:3000/v1/pets' -d '{"id": "sdfsf", "name": "jobe"}' -H 'Content-type: application/json'|jq
{
"message": "request/body/id must be integer",
"errors": [
{
"path": "/body/id",
"message": "must be integer",
"errorCode": "type.openapi.validation"
}
],
"code": 400
}
```

### GET /pets/:id

success
```shell
curl -s 'localhost:3000/v1/pets/1' |jq
{
"id": 1,
"name": "sparky",
"type": "dog",
"tags": [
"sweet"
]
}
```

error: bad id type
```shell
curl -s 'localhost:3000/v1/pets/lkl' |jq
{
"message": "request/params/petId must be number",
"errors": [
{
"path": "/params/petId",
"message": "must be number",
"errorCode": "type.openapi.validation"
}
],
"code": 400
}
```


### /v1/pets/1/photos

success
```shell
curl -XPOST 'localhost:3000/v1/pets/1/photos' -H 'Content-type: multipart/form-data' -F '[email protected]'|jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 304 100 90 100 214 25996 61813 --:--:-- --:--:-- --:--:-- 98k
{
"files_metadata": [
{
"originalname": "test.txt",
"encoding": "7bit",
"mimetype": "text/plain"
}
]
}
```

error: no file
```shell
curl -s -XPOST 'localhost:3000/v1/pets/1/photos' -H 'Content-type: multipart/form-data' |jq
{
"message": "request/body must have required property 'file'",
"errors": [
{
"path": "/body/file",
"message": "must have required property 'file'",
"errorCode": "required.openapi.validation"
}
],
"code": 400
}
```

157 changes: 157 additions & 0 deletions examples/1-standard-oas-3.1/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
openapi: 3.1.0
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
url: https://opensource.org/licenses/MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: true
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/Pets'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
post:
summary: Create a pet
operationId: createPets
tags:
- pets
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'201':
description: Null response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pets/{id}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: id
in: path
required: true
description: The id of the pet to retrieve
schema:
type: number
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

/pets/{id}/photos:
post:
summary: upload a photo of the pet
operationId: uploadPetPhoto
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
schema:
type: integer
format: int64
requestBody:
content:
multipart/form-data:
schema:
type: object
required:
- file
properties:
file:
description: The photo
type: string
format: binary
required: true
responses:
'200':
description: Null response
content:
application/json:
schema:
type: object
properties:
success:
type: boolean
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Pets:
type: array
items:
$ref: '#/components/schemas/Pet'
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Loading

0 comments on commit fc876a3

Please sign in to comment.