From 8430e56e9106784b56d67aaaca122dbb51bd063d Mon Sep 17 00:00:00 2001 From: Joel Posti Date: Fri, 29 Oct 2021 16:19:37 +0300 Subject: [PATCH 1/3] Improved SchemaObject interface. Many of its properties can be of type ReferenceObject per OpenAPI specification. --- src/OpenApiDocument.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/OpenApiDocument.ts b/src/OpenApiDocument.ts index a1467b9..8f86e34 100644 --- a/src/OpenApiDocument.ts +++ b/src/OpenApiDocument.ts @@ -36,15 +36,15 @@ export interface InfoObject { export interface SchemaObject { type?: string; - items?: SchemaObject; - properties?: { [property: string]: SchemaObject }; + items?: SchemaObject | ReferenceObject; + properties?: { [property: string]: SchemaObject | ReferenceObject }; nullable?: boolean; required?: string[]; - allOf?: SchemaObject[]; - anyOf?: SchemaObject[]; - oneOf?: SchemaObject[]; - not?: SchemaObject; - additionalProperties?: boolean | SchemaObject; + allOf?: (SchemaObject | ReferenceObject)[]; + anyOf?: (SchemaObject | ReferenceObject)[]; + oneOf?: (SchemaObject | ReferenceObject)[]; + not?: SchemaObject | ReferenceObject; + additionalProperties?: boolean | SchemaObject | ReferenceObject; } export interface ComponentsObject { From ae158743f049cc5a19357c563379035a67db439a Mon Sep 17 00:00:00 2001 From: Joel Posti Date: Fri, 29 Oct 2021 17:25:39 +0300 Subject: [PATCH 2/3] Fixed parameter typing of mapOasSchemaToJsonSchema, mapOasFieldsToJsonSchemaFields, walk and walkSchema: schema parameters can be ReferenceObjects as well as SchemaObjects. --- src/schema-utils.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/schema-utils.ts b/src/schema-utils.ts index fc8d494..3a12135 100644 --- a/src/schema-utils.ts +++ b/src/schema-utils.ts @@ -45,11 +45,12 @@ const arrayFields = ["allOf", "anyOf", "oneOf"]; const schemaFields = ["items", "not", "additionalProperties"]; export const walkSchema = ( - originalSchema: SchemaObject, - mapper: (x: SchemaObject) => SchemaObject, + originalSchema: SchemaObject | ReferenceObject, + mapper: (x: SchemaObject | ReferenceObject) => SchemaObject, ): SchemaObject => { let schema = mapper(originalSchema); - const walk = (s: SchemaObject): SchemaObject => walkSchema(s, mapper); + const walk = (s: SchemaObject | ReferenceObject): SchemaObject => + walkSchema(s, mapper); if (schema.properties !== undefined) { schema = { ...schema, properties: _.mapValues(schema.properties, walk) }; @@ -75,10 +76,12 @@ export const walkSchema = ( }; export const mapOasSchemaToJsonSchema = ( - originalSchema: SchemaObject, + originalSchema: SchemaObject | ReferenceObject, document: OpenApiDocument, ): SchemaObject => { - const mapOasFieldsToJsonSchemaFields = (s: SchemaObject): SchemaObject => { + const mapOasFieldsToJsonSchemaFields = ( + s: SchemaObject | ReferenceObject, + ): SchemaObject => { const schema = resolveReference(document, s); if (Array.isArray(schema.type)) { throw new TypeError("Type field in schema must not be an array"); From 17703644d630cc42657e4213fc4e9feee6ffc7bb Mon Sep 17 00:00:00 2001 From: Joel Posti Date: Fri, 29 Oct 2021 17:46:40 +0300 Subject: [PATCH 3/3] Removed any types from walkSchema. --- src/schema-utils.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/schema-utils.ts b/src/schema-utils.ts index 3a12135..6d43422 100644 --- a/src/schema-utils.ts +++ b/src/schema-utils.ts @@ -41,8 +41,8 @@ export const resolveReference = ( return object; }; -const arrayFields = ["allOf", "anyOf", "oneOf"]; -const schemaFields = ["items", "not", "additionalProperties"]; +const arrayFields = ["allOf", "anyOf", "oneOf"] as const; +const schemaFields = ["items", "not", "additionalProperties"] as const; export const walkSchema = ( originalSchema: SchemaObject | ReferenceObject, @@ -59,17 +59,21 @@ export const walkSchema = ( arrayFields .filter((f) => f in schema) .forEach((f) => { - schema = { ...schema, [f]: (schema as any)[f].map(walk) }; + schema = { ...schema, [f]: schema[f]?.map(walk) }; }); schemaFields .filter((f) => f in schema) .forEach((f) => { - const nestedSchema = (schema as any)[f]; + const nestedSchema = schema[f]; if (f === "additionalProperties" && typeof nestedSchema === "boolean") { return; + } else { + schema = { + ...schema, + [f]: walk(nestedSchema as SchemaObject | ReferenceObject), + }; } - schema = { ...schema, [f]: walk(nestedSchema) }; }); return schema;