From 0ad49ec78e0a3d36be40b36157be970e649a9a5a Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Sat, 27 Jan 2024 12:01:24 -0500 Subject: [PATCH] remove examples from schema (#890) --- 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