From 57bf468d8f2736155733b734ef059c77ce258961 Mon Sep 17 00:00:00 2001 From: DiMascio Date: Sat, 27 Jan 2024 11:55:28 -0500 Subject: [PATCH 1/3] remove examples from schema --- src/framework/types.ts | 5 +++-- .../parsers/schema.preprocessor.ts | 19 +++++++++++++++++-- test/356.campaign.yaml | 7 ++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/framework/types.ts b/src/framework/types.ts index 201e6fbf..eb4f4af0 100644 --- a/src/framework/types.ts +++ b/src/framework/types.ts @@ -43,7 +43,7 @@ export interface Options extends ajv.Options { ajvFormats?: FormatsPluginOptions; } -export interface RequestValidatorOptions extends Options, ValidateRequestOpts {} +export interface RequestValidatorOptions extends Options, ValidateRequestOpts { } export type ValidateRequestOpts = { allowUnknownQueryParameters?: boolean; @@ -240,7 +240,7 @@ export namespace OpenAPIV3 { in: string; } - export interface HeaderObject extends ParameterBaseObject {} + export interface HeaderObject extends ParameterBaseObject { } interface ParameterBaseObject { description?: string; @@ -317,6 +317,7 @@ export namespace OpenAPIV3 { xml?: XMLObject; externalDocs?: ExternalDocumentationObject; example?: any; + examples?: any; deprecated?: boolean; // Express-openapi-validator specific properties diff --git a/src/middlewares/parsers/schema.preprocessor.ts b/src/middlewares/parsers/schema.preprocessor.ts index 283e8d4b..8d9d8948 100644 --- a/src/middlewares/parsers/schema.preprocessor.ts +++ b/src/middlewares/parsers/schema.preprocessor.ts @@ -74,7 +74,7 @@ type Schema = ReferenceObject | SchemaObject; if (!Array.prototype['flatMap']) { // polyfill flatMap // TODO remove me when dropping node 10 support - Array.prototype['flatMap'] = function(lambda) { + Array.prototype['flatMap'] = function (lambda) { return Array.prototype.concat.apply([], this.map(lambda)); }; Object.defineProperty(Array.prototype, 'flatMap', { enumerable: false }); @@ -288,6 +288,7 @@ export class SchemaPreprocessor { this.handleSerDes(pschema, nschema, options); this.handleReadonly(pschema, nschema, options); this.processDiscriminator(pschema, nschema, options); + this.removeExamples(pschema, nschema, options) } } } @@ -443,6 +444,20 @@ export class SchemaPreprocessor { } } + private removeExamples( + parent: OpenAPIV3.SchemaObject, + schema: OpenAPIV3.SchemaObject, + opts, + ) { + if (schema.type !== 'object') return; + if (schema?.example) { + delete schema.example + } + if (schema?.examples) { + delete schema.examples + } + } + private handleReadonly( parent: OpenAPIV3.SchemaObject, schema: OpenAPIV3.SchemaObject, @@ -591,7 +606,7 @@ export class SchemaPreprocessor { ) => // if name or ref exists and are equal (opParam['name'] && opParam['name'] === pathParam['name']) || - (opParam['$ref'] && opParam['$ref'] === pathParam['$ref']); + (opParam['$ref'] && opParam['$ref'] === pathParam['$ref']); // Add Path level query param to list ONLY if there is not already an operation-level query param by the same name. for (const param of parameters) { diff --git a/test/356.campaign.yaml b/test/356.campaign.yaml index d6cedd54..0f01a687 100644 --- a/test/356.campaign.yaml +++ b/test/356.campaign.yaml @@ -68,4 +68,9 @@ components: format: date-time endDate: type: string - format: date-time \ No newline at end of file + format: date-time + example: + type: string + example: + name: 'hi' + description: 'yo' \ No newline at end of file From 1bb17e81e0c16cbfbf7b08e2d450c03716ef941e Mon Sep 17 00:00:00 2001 From: DiMascio Date: Sat, 27 Jan 2024 14:27:07 -0500 Subject: [PATCH 2/3] add cookies to example 1 and 2 --- examples/1-standard/app.js | 2 ++ examples/2-standard-multiple-api-specs/app.js | 3 +++ 2 files changed, 5 insertions(+) diff --git a/examples/1-standard/app.js b/examples/1-standard/app.js index 1de69e01..8b55477f 100644 --- a/examples/1-standard/app.js +++ b/examples/1-standard/app.js @@ -1,6 +1,7 @@ const express = require('express'); const path = require('path'); const http = require('http'); +const cookieParser = require('cookie-parser'); // Add if using cookie auth const { Pets } = require('./services'); const OpenApiValidator = require('express-openapi-validator'); @@ -12,6 +13,7 @@ const apiSpec = path.join(__dirname, 'api.yaml'); app.use(express.urlencoded({ extended: false })); app.use(express.text()); app.use(express.json()); +app.use(cookieParser()); // Add if using cookie auth enables req.cookies // Optionally serve the API spec app.use('/spec', express.static(apiSpec)); diff --git a/examples/2-standard-multiple-api-specs/app.js b/examples/2-standard-multiple-api-specs/app.js index 8f002ea8..2fb9bf07 100644 --- a/examples/2-standard-multiple-api-specs/app.js +++ b/examples/2-standard-multiple-api-specs/app.js @@ -1,12 +1,15 @@ const express = require('express'); +const cookieParser = require('cookie-parser'); const path = require('path'); const http = require('http'); + const OpenApiValidator = require('express-openapi-validator'); app = express(); app.use(express.urlencoded({ extended: false })); app.use(express.text()); app.use(express.json()); +app.use(cookieParser()); // add if using cookie auth const versions = [1, 2]; From c7e6d326457a8dc9419cfe9395820caa759e57cb Mon Sep 17 00:00:00 2001 From: DiMascio Date: Sun, 28 Jan 2024 13:26:43 -0500 Subject: [PATCH 3/3] audit-fix --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 6c3ef9d4..7ad3d618 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3917,7 +3917,7 @@ "node_modules/lodash.zipobject": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/lodash.zipobject/-/lodash.zipobject-4.1.3.tgz", - "integrity": "sha1-s5n1q6j/YqdG9peb8gshT5ZNvvg=" + "integrity": "sha512-A9SzX4hMKWS25MyalwcOnNoplyHbkNVsjidhTp8ru0Sj23wY9GWBKS8gAIGDSAqeWjIjvE4KBEl24XXAs+v4wQ==" }, "node_modules/log-driver": { "version": "1.2.7",