Skip to content

Commit

Permalink
remove examples from schema (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio authored Jan 27, 2024
1 parent 40716fb commit 0ad49ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -240,7 +240,7 @@ export namespace OpenAPIV3 {
in: string;
}

export interface HeaderObject extends ParameterBaseObject {}
export interface HeaderObject extends ParameterBaseObject { }

interface ParameterBaseObject {
description?: string;
Expand Down Expand Up @@ -317,6 +317,7 @@ export namespace OpenAPIV3 {
xml?: XMLObject;
externalDocs?: ExternalDocumentationObject;
example?: any;
examples?: any;
deprecated?: boolean;

// Express-openapi-validator specific properties
Expand Down
19 changes: 17 additions & 2 deletions src/middlewares/parsers/schema.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion test/356.campaign.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ components:
format: date-time
endDate:
type: string
format: date-time
format: date-time
example:
type: string
example:
name: 'hi'
description: 'yo'

0 comments on commit 0ad49ec

Please sign in to comment.