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

improved schema preprocessor and Date object handling (validation/serialization) for response bodies #499

Merged
merged 33 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a254fc4
CoerceComponents feature as expected in https://github.com/cdimascio/…
pilerou Dec 18, 2020
94c05dc
Issue on Typescript Options inheritance
pilerou Dec 18, 2020
7a27963
Patch AJV cache integration issue. AJV has a cache by schema descript…
pilerou Dec 20, 2020
a7e98f8
Change Test name to "coercecomponents". I missed to change from the e…
pilerou Dec 20, 2020
58675a3
- Changes `coerceComponents` to `schemaObjectMapper`
pilerou Dec 20, 2020
493c8e9
Move example resources `7-coerce-components` to `7-schema-object-mapper`
pilerou Dec 20, 2020
06185f1
update naming and type declarations
cdimascio Dec 21, 2020
99cbaf3
doc: update documentation
cdimascio Dec 21, 2020
f201808
traverse spec
cdimascio Dec 23, 2020
76e1a85
Merge branch 'master' of https://github.com/cdimascio/express-openapi…
cdimascio Dec 23, 2020
7480ea6
Merge branch 'master' into cmd/alt/serdes
cdimascio Dec 23, 2020
037cf9b
update preprocessor
cdimascio Dec 23, 2020
37ee125
travserse via Node abstraction
cdimascio Dec 23, 2020
a396d97
traverse and update both schemas
cdimascio Dec 23, 2020
abce8e1
finish one pass traversal to modified req and res schemas independently
cdimascio Dec 24, 2020
2ea49fd
doc: remove schemaObjectMapper from readme
cdimascio Dec 24, 2020
c04540c
doc: remove schemaObjectMapper from README
cdimascio Dec 24, 2020
991f4f9
fix: update method name
cdimascio Dec 24, 2020
1372bc2
cleanup
cdimascio Dec 24, 2020
cbac269
rename preprocessor
cdimascio Dec 24, 2020
2bbe0d1
rename var
cdimascio Dec 24, 2020
a939c6a
sample built-in date-time formatter
cdimascio Dec 24, 2020
80d9895
fix: types
cdimascio Dec 24, 2020
7ddadb1
feat: provide opt-in option responseValidation serialization
cdimascio Dec 25, 2020
af09f55
feat: handle date and date-time
cdimascio Dec 25, 2020
e6ba70a
doc: update README
cdimascio Dec 25, 2020
f9f3df6
fix: add example - Date serialization in responses
cdimascio Dec 25, 2020
18df83a
doc: update README
cdimascio Dec 25, 2020
7031af4
feat: add date serialization example
cdimascio Dec 25, 2020
751a427
doc: update README
cdimascio Dec 25, 2020
95659c7
chore: remove commented code
cdimascio Dec 25, 2020
63d623c
feat: handle Date or date string automatically
cdimascio Dec 25, 2020
9b03e5c
add format metadata to Serializer
cdimascio Dec 25, 2020
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1219,4 +1219,4 @@ We plan to publicize a variety of links here.

[MIT](LICENSE)

<a href="https://www.buymeacoffee.com/m97tA5c" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
<a href="https://www.buymeacoffee.com/m97tA5c" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
8 changes: 8 additions & 0 deletions assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ app.use((err, req, res, next) => {
```

_**Important:** Ensure express is configured with all relevant body parsers. Body parser middleware functions must be specified prior to any validated routes. See an [example](#example-express-api-server)_.

## [Documentation](https://github.com/cdimascio/express-openapi-validator#readme)

See the [full documentation](https://github.com/cdimascio/express-openapi-validator#readme)

## License

MIT
27 changes: 27 additions & 0 deletions examples/7-response-date-serialization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# example

## Install

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

## Run

From this `7-schema-object-mapper` directory, run:

```shell
npm start
```

## Try

```shell

## call pets
## the call below should return 400 since it requires additional parameters
curl http://localhost:3000/v1/pets?type=dog&limit=3

## Get the first item id
curl http://localhost:3000/v1/pets/<first item id>
```
50 changes: 50 additions & 0 deletions examples/7-response-date-serialization/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore schemaObjectMapper variant
description: A sample API
termsOfService: http://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
- url: /v1
paths:
/date-time:
get:
responses:
200:
description: date-time handler
content:
application/json:
schema:
type: object
properties:
created_at:
type: string
format: date-time
id:
type: number
/date:
get:
responses:
200:
description: date handler
content:
application/json:
schema:
$ref: '#/components/schemas/User'

components:
schemas:
Date:
type: string
format: date
User:
type: object
properties:
id:
type: number
created_at:
$ref: "#/components/schemas/Date"

57 changes: 57 additions & 0 deletions examples/7-response-date-serialization/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const http = require('http');
const OpenApiValidator = require('express-openapi-validator');

const port = 3000;
const app = express();
const apiSpec = path.join(__dirname, 'api.yaml');

// 1. Install bodyParsers for the request types your API will support
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text());
app.use(bodyParser.json());

// Optionally serve the API spec
app.use('/spec', express.static(apiSpec));

// 2. Install the OpenApiValidator on your express app
app.use(
OpenApiValidator.middleware({
apiSpec,
validateResponses: true,
}),
);
// 3. Add routes
app.get('/v1/ping', function (req, res, next) {
res.send('pong');
});

app.get('/v1/date-time', function (req, res, next) {
res.json({
id: 1,
created_at: new Date(),
});
});

app.get('/v1/date', function (req, res, next) {
res.json({
id: 1,
created_at: new Date(),
});
});

// 4. Create a custom error handler
app.use((err, req, res, next) => {
// format errors
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});

http.createServer(app).listen(port);
console.log(`Listening on port ${port}`);

module.exports = app;
Loading