mirror of
https://github.com/Redocly/redoc.git
synced 2024-12-04 22:33:43 +03:00
Add expandSchemas to expand all properties for all Schemas
This commit is contained in:
parent
b4f3f02f8d
commit
08562e7756
8
e2e/expandSchemas.html
Normal file
8
e2e/expandSchemas.html
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<redoc spec-url="../demo/openapi.yaml" expand-schemas="true"></redoc>
|
||||||
|
<script src="../bundles/redoc.standalone.js"></script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
17
e2e/integration/expandSchemas.e2e.ts
Normal file
17
e2e/integration/expandSchemas.e2e.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
describe('Schemas', () => {
|
||||||
|
it('expandSchemas != true', () => {
|
||||||
|
cy.visit('e2e/standalone.html');
|
||||||
|
|
||||||
|
cy.get('.api-content')
|
||||||
|
.find('.expanded')
|
||||||
|
.should('have.length', 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('expandSchemas == true', () => {
|
||||||
|
cy.visit('e2e/expandSchemas.html');
|
||||||
|
|
||||||
|
cy.get('.api-content')
|
||||||
|
.find('.expanded')
|
||||||
|
.should('have.length', 146);
|
||||||
|
});
|
||||||
|
});
|
|
@ -41,6 +41,7 @@ export interface RedocRawOptions {
|
||||||
expandDefaultServerVariables?: boolean;
|
expandDefaultServerVariables?: boolean;
|
||||||
maxDisplayedEnumValues?: number;
|
maxDisplayedEnumValues?: number;
|
||||||
ignoreNamedSchemas?: string[] | string;
|
ignoreNamedSchemas?: string[] | string;
|
||||||
|
expandSchemas?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
|
function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
|
||||||
|
@ -86,6 +87,10 @@ export class RedocNormalizedOptions {
|
||||||
return !!value;
|
return !!value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static normalizeExpandSchemas(value: RedocRawOptions['expandSchemas']): boolean {
|
||||||
|
return !!value;
|
||||||
|
}
|
||||||
|
|
||||||
static normalizeScrollYOffset(value: RedocRawOptions['scrollYOffset']): () => number {
|
static normalizeScrollYOffset(value: RedocRawOptions['scrollYOffset']): () => number {
|
||||||
// just number is not valid selector and leads to crash so checking if isNumeric here
|
// just number is not valid selector and leads to crash so checking if isNumeric here
|
||||||
if (typeof value === 'string' && !isNumeric(value)) {
|
if (typeof value === 'string' && !isNumeric(value)) {
|
||||||
|
@ -194,6 +199,7 @@ export class RedocNormalizedOptions {
|
||||||
maxDisplayedEnumValues?: number;
|
maxDisplayedEnumValues?: number;
|
||||||
|
|
||||||
ignoreNamedSchemas: Set<string>;
|
ignoreNamedSchemas: Set<string>;
|
||||||
|
expandSchemas: boolean;
|
||||||
|
|
||||||
constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
|
constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
|
||||||
raw = { ...defaults, ...raw };
|
raw = { ...defaults, ...raw };
|
||||||
|
@ -252,5 +258,6 @@ export class RedocNormalizedOptions {
|
||||||
this.maxDisplayedEnumValues = argValueToNumber(raw.maxDisplayedEnumValues);
|
this.maxDisplayedEnumValues = argValueToNumber(raw.maxDisplayedEnumValues);
|
||||||
const ignoreNamedSchemas = Array.isArray(raw.ignoreNamedSchemas) ? raw.ignoreNamedSchemas : raw.ignoreNamedSchemas?.split(',').map(s => s.trim());
|
const ignoreNamedSchemas = Array.isArray(raw.ignoreNamedSchemas) ? raw.ignoreNamedSchemas : raw.ignoreNamedSchemas?.split(',').map(s => s.trim());
|
||||||
this.ignoreNamedSchemas = new Set(ignoreNamedSchemas);
|
this.ignoreNamedSchemas = new Set(ignoreNamedSchemas);
|
||||||
|
this.expandSchemas = RedocNormalizedOptions.normalizeExpandSchemas(raw.expandSchemas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/services/__tests__/fixtures/expandSchemas.json
Normal file
20
src/services/__tests__/fixtures/expandSchemas.json
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"openapi": "3.0.0",
|
||||||
|
"info": {
|
||||||
|
"version": "1.0",
|
||||||
|
"title": "Foo"
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"Foo": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"foo": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -40,5 +40,25 @@ describe('Models', () => {
|
||||||
expect(schema.oneOf).toHaveLength(2);
|
expect(schema.oneOf).toHaveLength(2);
|
||||||
expect(schema.displayType).toBe('(Array of strings or numbers) or string');
|
expect(schema.displayType).toBe('(Array of strings or numbers) or string');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('expandSchemas != true', () => {
|
||||||
|
const spec = require('../fixtures/expandSchemas.json');
|
||||||
|
parser = new OpenAPIParser(spec, undefined, opts);
|
||||||
|
const schema = new SchemaModel(parser, spec.components.schemas.Foo, '', opts);
|
||||||
|
expect(schema.fields).toHaveLength(2);
|
||||||
|
expect(schema.fields![0].expanded).toEqual(false);
|
||||||
|
expect(schema.fields![1].expanded).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('expandSchemas == true', () => {
|
||||||
|
const opts = new RedocNormalizedOptions({ expandSchemas: true});
|
||||||
|
|
||||||
|
const spec = require('../fixtures/expandSchemas.json');
|
||||||
|
parser = new OpenAPIParser(spec, undefined, opts);
|
||||||
|
const schema = new SchemaModel(parser, spec.components.schemas.Foo, '', opts);
|
||||||
|
expect(schema.fields).toHaveLength(2);
|
||||||
|
expect(schema.fields![0].expanded).toEqual(true);
|
||||||
|
expect(schema.fields![1].expanded).toEqual(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -348,7 +348,7 @@ function buildFields(
|
||||||
const required =
|
const required =
|
||||||
schema.required === undefined ? false : schema.required.indexOf(fieldName) > -1;
|
schema.required === undefined ? false : schema.required.indexOf(fieldName) > -1;
|
||||||
|
|
||||||
return new FieldModel(
|
const fieldModel = new FieldModel(
|
||||||
parser,
|
parser,
|
||||||
{
|
{
|
||||||
name: fieldName,
|
name: fieldName,
|
||||||
|
@ -361,6 +361,8 @@ function buildFields(
|
||||||
$ref + '/properties/' + fieldName,
|
$ref + '/properties/' + fieldName,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
|
fieldModel.expanded = options.expandSchemas;
|
||||||
|
return fieldModel;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (options.sortPropsAlphabetically) {
|
if (options.sortPropsAlphabetically) {
|
||||||
|
@ -372,22 +374,22 @@ function buildFields(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof additionalProps === 'object' || additionalProps === true) {
|
if (typeof additionalProps === 'object' || additionalProps === true) {
|
||||||
fields.push(
|
const fieldModel = new FieldModel(
|
||||||
new FieldModel(
|
parser,
|
||||||
parser,
|
{
|
||||||
{
|
name: (typeof additionalProps === 'object'
|
||||||
name: (typeof additionalProps === 'object'
|
? additionalProps['x-additionalPropertiesName'] || 'property name'
|
||||||
? additionalProps['x-additionalPropertiesName'] || 'property name'
|
: 'property name'
|
||||||
: 'property name'
|
).concat('*'),
|
||||||
).concat('*'),
|
required: false,
|
||||||
required: false,
|
schema: additionalProps === true ? {} : additionalProps,
|
||||||
schema: additionalProps === true ? {} : additionalProps,
|
kind: 'additionalProperties',
|
||||||
kind: 'additionalProperties',
|
},
|
||||||
},
|
$ref + '/additionalProperties',
|
||||||
$ref + '/additionalProperties',
|
options,
|
||||||
options,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
fieldModel.expanded = options.expandSchemas;
|
||||||
|
fields.push(fieldModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fields;
|
return fields;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user