fix: use correct parent section for security definition

This commit is contained in:
Roman Hotsiy 2018-08-17 14:17:16 +03:00
parent 4ef4a971ce
commit f903406c14
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
6 changed files with 35 additions and 13 deletions

View File

@ -1,5 +1,9 @@
import { OpenAPIOperation, OpenAPIParameter, OpenAPISpec, OpenAPITag, Referenced } from '../types'; import { OpenAPIOperation, OpenAPIParameter, OpenAPISpec, OpenAPITag, Referenced } from '../types';
import { isOperationName } from '../utils'; import {
isOperationName,
SECURITY_DEFINITIONS_COMPONENT_NAME,
setSecuritySchemePrefix,
} from '../utils';
import { MarkdownRenderer } from './MarkdownRenderer'; import { MarkdownRenderer } from './MarkdownRenderer';
import { GroupModel, OperationModel } from './models'; import { GroupModel, OperationModel } from './models';
import { OpenAPIParser } from './OpenAPIParser'; import { OpenAPIParser } from './OpenAPIParser';
@ -38,7 +42,7 @@ export class MenuBuilder {
const items: ContentItemModel[] = []; const items: ContentItemModel[] = [];
const tagsMap = MenuBuilder.getTagsWithOperations(spec); const tagsMap = MenuBuilder.getTagsWithOperations(spec);
items.push(...MenuBuilder.addMarkdownItems(spec.info.description || '')); items.push(...MenuBuilder.addMarkdownItems(spec.info.description || '', options));
if (spec['x-tagGroups']) { if (spec['x-tagGroups']) {
items.push( items.push(
...MenuBuilder.getTagGroupsItems(parser, undefined, spec['x-tagGroups'], tagsMap, options), ...MenuBuilder.getTagGroupsItems(parser, undefined, spec['x-tagGroups'], tagsMap, options),
@ -53,8 +57,11 @@ export class MenuBuilder {
* extracts items from markdown description * extracts items from markdown description
* @param description - markdown source * @param description - markdown source
*/ */
static addMarkdownItems(description: string): ContentItemModel[] { static addMarkdownItems(
const renderer = new MarkdownRenderer(); description: string,
options: RedocNormalizedOptions,
): ContentItemModel[] {
const renderer = new MarkdownRenderer(options);
const headings = renderer.extractHeadings(description || ''); const headings = renderer.extractHeadings(description || '');
const mapHeadingsDeep = (parent, items, depth = 1) => const mapHeadingsDeep = (parent, items, depth = 1) =>
@ -64,6 +71,14 @@ export class MenuBuilder {
if (heading.items) { if (heading.items) {
group.items = mapHeadingsDeep(group, heading.items, depth + 1); group.items = mapHeadingsDeep(group, heading.items, depth + 1);
} }
if (
MarkdownRenderer.containsComponent(
group.description || '',
SECURITY_DEFINITIONS_COMPONENT_NAME,
)
) {
setSecuritySchemePrefix(group.id + '/');
}
return group; return group;
}); });

View File

@ -145,9 +145,12 @@ export class MenuStore {
if (item) { if (item) {
this.activateAndScroll(item, false); this.activateAndScroll(item, false);
} else { } else {
if (hash.startsWith(SECURITY_SCHEMES_SECTION_PREFIX)) {
item = this.flatItems.find(i => SECURITY_SCHEMES_SECTION_PREFIX.startsWith(i.id));
this.activate(item);
}
this.scroll.scrollIntoViewBySelector(`[${SECTION_ATTR}="${hash}"]`); this.scroll.scrollIntoViewBySelector(`[${SECTION_ATTR}="${hash}"]`);
} }
return item !== undefined;
}; };
/** /**

View File

@ -4,7 +4,7 @@ import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types';
import { appendToMdHeading, IS_BROWSER } from '../utils/'; import { appendToMdHeading, IS_BROWSER } from '../utils/';
import { JsonPointer } from '../utils/JsonPointer'; import { JsonPointer } from '../utils/JsonPointer';
import { isNamedDefinition } from '../utils/openapi'; import { isNamedDefinition, SECURITY_DEFINITIONS_COMPONENT_NAME } from '../utils/openapi';
import { buildComponentComment, MarkdownRenderer } from './MarkdownRenderer'; import { buildComponentComment, MarkdownRenderer } from './MarkdownRenderer';
import { RedocNormalizedOptions } from './RedocNormalizedOptions'; import { RedocNormalizedOptions } from './RedocNormalizedOptions';
@ -74,8 +74,8 @@ export class OpenAPIParser {
) { ) {
// Automatically inject Authentication section with SecurityDefinitions component // Automatically inject Authentication section with SecurityDefinitions component
const description = spec.info.description || ''; const description = spec.info.description || '';
if (!MarkdownRenderer.containsComponent(description, 'security-definitions')) { if (!MarkdownRenderer.containsComponent(description, SECURITY_DEFINITIONS_COMPONENT_NAME)) {
const comment = buildComponentComment('security-definitions'); const comment = buildComponentComment(SECURITY_DEFINITIONS_COMPONENT_NAME);
spec.info.description = appendToMdHeading(description, 'Authentication', comment); spec.info.description = appendToMdHeading(description, 'Authentication', comment);
} }
} }

View File

@ -1,5 +1,5 @@
import { OpenAPISecurityRequirement, OpenAPISecurityScheme } from '../../types'; import { OpenAPISecurityRequirement, OpenAPISecurityScheme } from '../../types';
import { SECURITY_SCHEMES_SECTION } from '../../utils/openapi'; import { SECURITY_SCHEMES_SECTION_PREFIX } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
export interface SecurityScheme extends OpenAPISecurityScheme { export interface SecurityScheme extends OpenAPISecurityScheme {
@ -27,7 +27,7 @@ export class SecurityRequirementModel {
return { return {
...scheme, ...scheme,
id, id,
sectionId: SECURITY_SCHEMES_SECTION + id, sectionId: SECURITY_SCHEMES_SECTION_PREFIX + id,
scopes, scopes,
}; };
}) })

View File

@ -1,5 +1,5 @@
import { OpenAPISecurityScheme, Referenced } from '../../types'; import { OpenAPISecurityScheme, Referenced } from '../../types';
import { SECURITY_SCHEMES_SECTION } from '../../utils/openapi'; import { SECURITY_SCHEMES_SECTION_PREFIX } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
export class SecuritySchemeModel { export class SecuritySchemeModel {
@ -25,7 +25,7 @@ export class SecuritySchemeModel {
constructor(parser: OpenAPIParser, id: string, scheme: Referenced<OpenAPISecurityScheme>) { constructor(parser: OpenAPIParser, id: string, scheme: Referenced<OpenAPISecurityScheme>) {
const info = parser.deref(scheme); const info = parser.deref(scheme);
this.id = id; this.id = id;
this.sectionId = SECURITY_SCHEMES_SECTION + id; this.sectionId = SECURITY_SCHEMES_SECTION_PREFIX + id;
this.type = info.type; this.type = info.type;
this.description = info.description || ''; this.description = info.description || '';
if (info.type === 'apiKey') { if (info.type === 'apiKey') {

View File

@ -275,4 +275,8 @@ export function normalizeServers(
}); });
} }
export const SECURITY_SCHEMES_SECTION = 'section/Authentication/'; export const SECURITY_DEFINITIONS_COMPONENT_NAME = 'security-definitions';
export let SECURITY_SCHEMES_SECTION_PREFIX = 'section/Authentication/';
export function setSecuritySchemePrefix(prefix: string) {
SECURITY_SCHEMES_SECTION_PREFIX = prefix;
}