fix: improve code after review

This commit is contained in:
Alex Varchuk 2021-05-28 12:23:10 +03:00
parent 68fb9e937b
commit 532c30e88c
5 changed files with 71 additions and 24 deletions

View File

@ -9,7 +9,7 @@ const DEFAULT_SPEC = 'openapi.yaml';
const NEW_VERSION_SPEC = 'openapi-3-1.yaml'; const NEW_VERSION_SPEC = 'openapi-3-1.yaml';
const demos = [ const demos = [
{ value: NEW_VERSION_SPEC, label: 'OpenApi 3.1' }, { value: NEW_VERSION_SPEC, label: 'Petstore OpenAPI 3.1' },
{ value: 'https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml', label: 'Instagram' }, { value: 'https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml', label: 'Instagram' },
{ {
value: 'https://api.apis.guru/v2/specs/googleapis.com/calendar/v3/openapi.yaml', value: 'https://api.apis.guru/v2/specs/googleapis.com/calendar/v3/openapi.yaml',

View File

@ -100,8 +100,8 @@ export class ApiInfo extends React.Component<ApiInfoProps> {
)) || )) ||
null} null}
</StyledMarkdownBlock> </StyledMarkdownBlock>
<Markdown source={store.spec.info.description} data-role="redoc-description"/>
<Markdown source={store.spec.info.summary} data-role="redoc-summary"/> <Markdown source={store.spec.info.summary} data-role="redoc-summary"/>
<Markdown source={store.spec.info.description} data-role="redoc-description"/>
{externalDocs && <ExternalDocumentation externalDocs={externalDocs} />} {externalDocs && <ExternalDocumentation externalDocs={externalDocs} />}
</MiddlePanel> </MiddlePanel>
</Row> </Row>

View File

@ -105,7 +105,7 @@ export class SchemaModel {
this.title = this.title =
schema.title || (isNamedDefinition(this.pointer) && JsonPointer.baseName(this.pointer)) || ''; schema.title || (isNamedDefinition(this.pointer) && JsonPointer.baseName(this.pointer)) || '';
this.description = schema.description || ''; this.description = schema.description || '';
this.type = (Array.isArray(schema.type) && schema.type) || (schema.type || detectType(schema)); this.type = schema.type || detectType(schema);
this.format = schema.format; this.format = schema.format;
this.enum = schema.enum || []; this.enum = schema.enum || [];
this.example = schema.example; this.example = schema.example;
@ -122,11 +122,14 @@ export class SchemaModel {
this.const = schema.const || ''; this.const = schema.const || '';
if (!!schema.nullable) { if (!!schema.nullable) {
if (Array.isArray(this.type)) this.type.push('null'); if (Array.isArray(this.type) && !this.type.includes('null')) {
else this.type = [this.type, 'null']; this.type = [...this.type, 'null'];
}
} }
this.displayType = Array.isArray(this.type) ? this.type.join(' or ') : this.type; this.displayType = Array.isArray(this.type)
? this.type.map(item => item === null ? 'null' : item).join(' or ')
: this.type;
if (this.isCircular) { if (this.isCircular) {
return; return;
@ -193,7 +196,7 @@ export class SchemaModel {
const title = const title =
isNamedDefinition(variant.$ref) && !merged.title isNamedDefinition(variant.$ref) && !merged.title
? JsonPointer.baseName(variant.$ref) ? JsonPointer.baseName(variant.$ref)
: merged.title; : (merged.const && JSON.stringify(merged.const)) || merged.title;
const schema = new SchemaModel( const schema = new SchemaModel(
parser, parser,

View File

@ -174,10 +174,56 @@ describe('Utils', () => {
expect(isPrimitiveType(schema)).toEqual(false); expect(isPrimitiveType(schema)).toEqual(false);
}); });
it('Should return false for array of strings', () => { it('should return true for array contains object and schema hasn\'t properties', () => {
const schema = { const schema = {
type: ['object', 'string'], type: ['object', 'string'],
}; };
expect(isPrimitiveType(schema)).toEqual(true);
});
it('should return false for array contains object and schema has properties', () => {
const schema = {
type: ['object', 'string'],
properties: {
a: {
type: 'string',
},
},
};
expect(isPrimitiveType(schema)).toEqual(false);
});
it('should return false for array contains array type and schema has items', () => {
const schema = {
type: ['array'],
items: {
type: 'object',
additionalProperties: true,
},
};
expect(isPrimitiveType(schema)).toEqual(false);
});
it('should return false for array contains object and array types and schema has items', () => {
const schema = {
type: ['array', 'object'],
items: {
type: 'object',
additionalProperties: true,
},
};
expect(isPrimitiveType(schema)).toEqual(false);
});
it('should return false for array contains object and array types and schema has properties', () => {
const schema = {
type: ['array', 'object'],
properties: {
a: {
type: 'string',
},
},
};
expect(isPrimitiveType(schema)).toEqual(false); expect(isPrimitiveType(schema)).toEqual(false);
}); });
@ -185,7 +231,7 @@ describe('Utils', () => {
const schema = { const schema = {
type: ['object', 'string', 'null'], type: ['object', 'string', 'null'],
}; };
expect(isPrimitiveType(schema)).toEqual(false); expect(isPrimitiveType(schema)).toEqual(true);
}); });
it('Should return false for array with non-empty objects', () => { it('Should return false for array with non-empty objects', () => {

View File

@ -69,7 +69,7 @@ export function getOperationSummary(operation: ExtendedOpenAPIOperation): string
operation.operationId || operation.operationId ||
(operation.description && operation.description.substring(0, 50)) || (operation.description && operation.description.substring(0, 50)) ||
operation.pathName || operation.pathName ||
'<no summary>' '<no summary>'
); );
} }
@ -116,22 +116,20 @@ export function isPrimitiveType(schema: OpenAPISchema, type: string | string[] |
return false; return false;
} }
if (type === 'object') { let isPrimitive = true;
return schema.properties !== undefined const isArray = Array.isArray(type);
if (type === 'object' || (isArray && type?.includes('object'))) {
isPrimitive = schema.properties !== undefined
? Object.keys(schema.properties).length === 0 ? Object.keys(schema.properties).length === 0
: schema.additionalProperties === undefined; : schema.additionalProperties === undefined;
} }
if (type === 'array') { if (schema.items !== undefined && (type === 'array' || (isArray && type?.includes('array')))) {
if (schema.items === undefined) { isPrimitive = false;
return true;
}
return false;
} }
if (Array.isArray(type)) return false return isPrimitive;
return true;
} }
export function isJsonLike(contentType: string): boolean { export function isJsonLike(contentType: string): boolean {
@ -589,10 +587,10 @@ export function setSecuritySchemePrefix(prefix: string) {
} }
export const shortenHTTPVerb = verb => export const shortenHTTPVerb = verb =>
({ ({
delete: 'del', delete: 'del',
options: 'opts', options: 'opts',
}[verb] || verb); }[verb] || verb);
export function isRedocExtension(key: string): boolean { export function isRedocExtension(key: string): boolean {
const redocExtensions = { const redocExtensions = {