From 59f3dc7f3ee4fd6ff4e701690aabae23e841a9b2 Mon Sep 17 00:00:00 2001 From: v8tenko Date: Thu, 17 Aug 2023 11:44:18 +0300 Subject: [PATCH] feat: support internal fields --- src/includer/generators/traverse.ts | 35 ++++++++++++++++++++++------- src/includer/models.ts | 10 +++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/includer/generators/traverse.ts b/src/includer/generators/traverse.ts index c069487..a27b746 100644 --- a/src/includer/generators/traverse.ts +++ b/src/includer/generators/traverse.ts @@ -331,33 +331,36 @@ function prepareSampleElement( // type: object // eslint-disable-next-line complexity function merge( - value: OpenJSONSchemaDefinition, + schema: OpenJSONSchemaDefinition, allRefs?: Refs, needToSaveRef = true, ): OpenJSONSchema { - if (typeof value === 'boolean') { + if (typeof schema === 'boolean') { throw Error('Boolean value isn\'t supported'); } - if (value.additionalProperties) { - const result = value.additionalProperties; + if (schema.additionalProperties) { + const result = schema.additionalProperties; if (typeof result === 'boolean') { throw Error('Boolean in additionalProperties isn\'t supported'); } - result.description = value.description; + result.description = schema.description; return merge(result); } - if (value.items) { - const result = value.items; + if (schema.items) { + const result = schema.items; if (Array.isArray(result)) { throw Error('Array in items isn\'t supported'); } - return {...value, items: merge(result)}; + return {...schema, items: merge(result)}; } + + const value = removeInternalProperty(schema); + if (value.oneOf?.length && value.allOf?.length) { throw Error('Object can\'t have both allOf and oneOf'); } @@ -413,6 +416,22 @@ function merge( }; } + +function removeInternalProperty(schema: OpenJSONSchema): OpenJSONSchema { + const internalPropertyTag = 'x-internal'; + const cleared = {...schema}; + + cleared.properties = {}; + + Object.keys(schema.properties || {}).forEach((key) => { + if (!schema.properties?.[key][internalPropertyTag]) { + cleared.properties![key] = schema.properties![key]; + } + }); + + return cleared; +} + function createOneOfDescription( allRefs: Refs | undefined, item: OpenJSONSchema, diff --git a/src/includer/models.ts b/src/includer/models.ts index b0a9a62..8a65db1 100644 --- a/src/includer/models.ts +++ b/src/includer/models.ts @@ -1,4 +1,4 @@ -import {JSONSchema6} from 'json-schema'; +import {JSONSchema6, JSONSchema6Definition} from 'json-schema'; import {LeadingPageMode, SPEC_RENDER_MODE_DEFAULT, SPEC_RENDER_MODE_HIDDEN, SUPPORTED_ENUM_TYPES} from './constants'; export type VarsPreset = 'internal'|'external'; @@ -273,5 +273,11 @@ export type OpenApiIncluderParams = { }; }; -export type OpenJSONSchema = JSONSchema6 & { example?: any }; +export type OpenJSONSchema = JSONSchema6 & { example?: any } & { + properties?: { + [key: string]: JSONSchema6Definition & { + 'x-internal'?: boolean; + }; + }; +}; export type OpenJSONSchemaDefinition = OpenJSONSchema | boolean;