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

fix: case-insensitive charset #503

Merged
merged 5 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions CHANGE_HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
## 4.10.0 (2020-12-26)

* improved schema preprocessor and `Date` object handling (validation/serialization) for response bodi ([e08f45a](https://github.com/cdimascio/express-openapi-validator/commit/e08f45a)), closes [#499](https://github.com/cdimascio/express-openapi-validator/issues/499)
* docs: add electrotype as a contributor (#502) ([e8d6a37](https://github.com/cdimascio/express-openapi-validator/commit/e8d6a37)), closes [#502](https://github.com/cdimascio/express-openapi-validator/issues/502)
* docs: add pilerou as a contributor (#497) ([2b2aa7f](https://github.com/cdimascio/express-openapi-validator/commit/2b2aa7f)), closes [#497](https://github.com/cdimascio/express-openapi-validator/issues/497)
* chore: increment patch version ([5df747c](https://github.com/cdimascio/express-openapi-validator/commit/5df747c))
* chore: increment patch version ([f35b1f2](https://github.com/cdimascio/express-openapi-validator/commit/f35b1f2))
* chore: increment patch version ([a542ece](https://github.com/cdimascio/express-openapi-validator/commit/a542ece))
* chore: launch.json ([3347f5d](https://github.com/cdimascio/express-openapi-validator/commit/3347f5d))
* chore: publish script ([ab392b8](https://github.com/cdimascio/express-openapi-validator/commit/ab392b8))
* chore: remove docs folder ([1592223](https://github.com/cdimascio/express-openapi-validator/commit/1592223))
* chore: update example deps ([1846227](https://github.com/cdimascio/express-openapi-validator/commit/1846227))
* doc: brief summary ([60f2cf4](https://github.com/cdimascio/express-openapi-validator/commit/60f2cf4))
* doc: update README ([1d3dcee](https://github.com/cdimascio/express-openapi-validator/commit/1d3dcee))
* doc: update README ([29a83a7](https://github.com/cdimascio/express-openapi-validator/commit/29a83a7))
* fix: #495 inconsistent validation of multipart request body with $ref (#496) ([832f865](https://github.com/cdimascio/express-openapi-validator/commit/832f865)), closes [#495](https://github.com/cdimascio/express-openapi-validator/issues/495) [#496](https://github.com/cdimascio/express-openapi-validator/issues/496) [#495](https://github.com/cdimascio/express-openapi-validator/issues/495)



## <small>4.9.5 (2020-12-26)</small>

* improved schema preprocessor and `Date` object handling (validation/serialization) for response bodi ([e08f45a](https://github.com/cdimascio/express-openapi-validator/commit/e08f45a)), closes [#499](https://github.com/cdimascio/express-openapi-validator/issues/499)
* docs: add electrotype as a contributor (#502) ([e8d6a37](https://github.com/cdimascio/express-openapi-validator/commit/e8d6a37)), closes [#502](https://github.com/cdimascio/express-openapi-validator/issues/502)
* docs: add pilerou as a contributor (#497) ([2b2aa7f](https://github.com/cdimascio/express-openapi-validator/commit/2b2aa7f)), closes [#497](https://github.com/cdimascio/express-openapi-validator/issues/497)
* chore: increment patch version ([5df747c](https://github.com/cdimascio/express-openapi-validator/commit/5df747c))
* chore: increment patch version ([f35b1f2](https://github.com/cdimascio/express-openapi-validator/commit/f35b1f2))
* chore: increment patch version ([a542ece](https://github.com/cdimascio/express-openapi-validator/commit/a542ece))
* chore: launch.json ([3347f5d](https://github.com/cdimascio/express-openapi-validator/commit/3347f5d))
* chore: publish script ([ab392b8](https://github.com/cdimascio/express-openapi-validator/commit/ab392b8))
* chore: remove docs folder ([1592223](https://github.com/cdimascio/express-openapi-validator/commit/1592223))
* chore: update example deps ([1846227](https://github.com/cdimascio/express-openapi-validator/commit/1846227))
* doc: brief summary ([60f2cf4](https://github.com/cdimascio/express-openapi-validator/commit/60f2cf4))
* doc: update README ([1d3dcee](https://github.com/cdimascio/express-openapi-validator/commit/1d3dcee))
* doc: update README ([29a83a7](https://github.com/cdimascio/express-openapi-validator/commit/29a83a7))
* fix: #495 inconsistent validation of multipart request body with $ref (#496) ([832f865](https://github.com/cdimascio/express-openapi-validator/commit/832f865)), closes [#495](https://github.com/cdimascio/express-openapi-validator/issues/495) [#496](https://github.com/cdimascio/express-openapi-validator/issues/496) [#495](https://github.com/cdimascio/express-openapi-validator/issues/495)



## <small>4.9.5 (2020-12-22)</small>

* fix: #495 inconsistent validation of multipart request body with $ref (#496) ([832f865](https://github.com/cdimascio/express-openapi-validator/commit/832f865)), closes [#495](https://github.com/cdimascio/express-openapi-validator/issues/495) [#496](https://github.com/cdimascio/express-openapi-validator/issues/496) [#495](https://github.com/cdimascio/express-openapi-validator/issues/495)
Expand Down
44 changes: 44 additions & 0 deletions launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Attach",
"port": 9229,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
},
{
"name": "Fastify Attach",
"port": 9320,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
},
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"runtimeExecutable": "/Users/cdimasci/.nvm/versions/node/v15.1.0/bin/node",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-r",
"source-map-support/register",
"-r",
"ts-node/register",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.spec.ts"
],
"env": { "TS_NODE_FILES": "true" },
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector"
}
]
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-openapi-validator",
"version": "4.9.5",
"version": "4.10.0",
"description": "Automatically validate API requests and responses with OpenAPI 3 and Express.",
"main": "dist/index.js",
"scripts": {
Expand Down
5 changes: 0 additions & 5 deletions src/middlewares/parsers/body.parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Ajv } from 'ajv';
import { ContentType } from '../util';

import {
Expand All @@ -7,10 +6,6 @@ import {
UnsupportedMediaType,
} from '../../framework/types';

type SchemaObject = OpenAPIV3.SchemaObject;
type ReferenceObject = OpenAPIV3.ReferenceObject;
type Schema = ReferenceObject | SchemaObject;

export class BodySchemaParser {
constructor() {
}
Expand Down
8 changes: 4 additions & 4 deletions src/middlewares/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export class ContentType {
private constructor(contentType: string | null) {
this.contentType = contentType;
if (contentType) {
this.withoutBoundary = contentType.replace(/;\s{0,}boundary.*/, '');
this.mediaType = this.withoutBoundary.split(';')[0].trim();
this.charSet = this.withoutBoundary.split(';')[1];
this.withoutBoundary = contentType.replace(/;\s{0,}boundary.*/, '').toLowerCase();
this.mediaType = this.withoutBoundary.split(';')[0].toLowerCase().trim();
this.charSet = this.withoutBoundary.split(';')[1]?.toLowerCase();
this.isWildCard = RegExp(/^[a-z]+\/\*$/).test(this.contentType);
if (this.charSet) {
this.charSet = this.charSet.trim();
this.charSet = this.charSet.toLowerCase().trim();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/content.type.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findResponseContent, ContentType } from '../src/middlewares/util';
import { findResponseContent } from '../src/middlewares/util';
import { expect } from 'chai';

describe('contentType', () => {
Expand Down
11 changes: 11 additions & 0 deletions test/headers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,16 @@ describe(packageJson.name, () => {
tag: 'cat',
})
.expect(200));

it('should match mediatype when charset case does not match the case defined in the spec', async () =>
request(app)
.post(`${app.basePath}/pets_charset`)
.set('Content-Type', 'application/json; charset=UTF-8')
.set('Accept', 'application/json; charset=UTF-8')
.send({
name: 'myPet',
tag: 'cat',
})
.expect(200));
});
});
9 changes: 9 additions & 0 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ describe(packageJson.name, () => {
expect(r.body.id).to.be.a('number').that.equals(213);
}));

it('should return 200 on valid responses 200 $ref', async () =>
request(app)
.get(`${app.basePath}/ref_response_body`)
.set('Accept', 'APPLICATION/JSON')
.expect(200)
.then((r: any) => {
expect(r.body.id).to.be.a('number').that.equals(213);
}));

it('should fail if response field has a value of incorrect type', async () =>
request(app)
.get(`${app.basePath}/pets?mode=bad_type`)
Expand Down