Skip to content

Commit

Permalink
Merge pull request #13 from diplodoc-platform/hidden_fields
Browse files Browse the repository at this point in the history
feat: support internal fields
  • Loading branch information
v8tenko authored Aug 18, 2023
2 parents 0299fac + 59f3dc7 commit 29128b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
35 changes: 27 additions & 8 deletions src/includer/generators/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions src/includer/models.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

0 comments on commit 29128b0

Please sign in to comment.