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

remove examples from schema #890

Merged
merged 1 commit into from
Jan 27, 2024
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
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'
Loading