Skip to content

Commit

Permalink
enhance SchemaObject type (#697)
Browse files Browse the repository at this point in the history
-  Composition types: allOf, anyOf, oneOf and not are valid SchemaObjects
  • Loading branch information
uwinkelvos authored Jan 7, 2023
1 parent d415425 commit ca43431
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
27 changes: 16 additions & 11 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,29 @@ export namespace OpenAPIV3 {
| 'string'
| 'integer';
export type ArraySchemaObjectType = 'array';
export type SchemaObject = ArraySchemaObject | NonArraySchemaObject;

export interface ArraySchemaObject extends BaseSchemaObject {
type: ArraySchemaObjectType;
export type SchemaObject = ArraySchemaObject | NonArraySchemaObject | CompositionSchemaObject;

export interface ArraySchemaObject extends BaseSchemaObject<ArraySchemaObjectType> {
items: ReferenceObject | SchemaObject;
}

export interface NonArraySchemaObject extends BaseSchemaObject {
type: NonArraySchemaObjectType;
export interface NonArraySchemaObject extends BaseSchemaObject<NonArraySchemaObjectType> {
}

interface BaseSchemaObject {
export interface CompositionSchemaObject extends BaseSchemaObject<undefined> {
// JSON schema allowed properties, adjusted for OpenAPI
allOf?: Array<ReferenceObject | SchemaObject>;
oneOf?: Array<ReferenceObject | SchemaObject>;
anyOf?: Array<ReferenceObject | SchemaObject>;
not?: ReferenceObject | SchemaObject;
// OpenAPI-specific properties
discriminator?: DiscriminatorObject;
}

interface BaseSchemaObject<T> {
// JSON schema allowed properties, adjusted for OpenAPI
type?: T;
title?: string;
description?: string;
format?: string;
Expand All @@ -298,14 +308,9 @@ export namespace OpenAPIV3 {
properties?: {
[name: string]: ReferenceObject | SchemaObject;
};
allOf?: Array<ReferenceObject | SchemaObject>;
oneOf?: Array<ReferenceObject | SchemaObject>;
anyOf?: Array<ReferenceObject | SchemaObject>;
not?: ReferenceObject | SchemaObject;

// OpenAPI-specific properties
nullable?: boolean;
discriminator?: DiscriminatorObject;
readOnly?: boolean;
writeOnly?: boolean;
xml?: XMLObject;
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/parsers/req.parameter.mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class RequestParameterMutator {
): void {
// fetch the keys used for this kind of explode
const type = schema.type;
const hasXOf = schema.allOf || schema.oneOf || schema.anyOf;
const hasXOf = schema['allOf'] || schema['oneOf'] || schema['anyOf'];
const properties = hasXOf
? xOfProperties(schema)
: type === 'object'
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/parsers/schema.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ export class SchemaPreprocessor {

private processDiscriminator(parent: Schema, schema: Schema, opts: any = {}) {
const o = opts.discriminator;
const schemaObj = <SchemaObject>schema;
const schemaObj = <OpenAPIV3.CompositionSchemaObject>schema;
const xOf = schemaObj.oneOf ? 'oneOf' : schemaObj.anyOf ? 'anyOf' : null;

if (xOf && schemaObj?.discriminator?.propertyName && !o.discriminator) {
if (xOf && schemaObj.discriminator?.propertyName && !o.discriminator) {
const options = schemaObj[xOf].flatMap((refObject) => {
if (refObject['$ref'] === undefined) {
return [];
Expand Down

0 comments on commit ca43431

Please sign in to comment.