Skip to content

Commit

Permalink
fix: use correct parent section for security definition
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Aug 17, 2018
1 parent 4ef4a97 commit f903406
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
23 changes: 19 additions & 4 deletions src/services/MenuBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
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 { GroupModel, OperationModel } from './models';
import { OpenAPIParser } from './OpenAPIParser';
Expand Down Expand Up @@ -38,7 +42,7 @@ export class MenuBuilder {

const items: ContentItemModel[] = [];
const tagsMap = MenuBuilder.getTagsWithOperations(spec);
items.push(...MenuBuilder.addMarkdownItems(spec.info.description || ''));
items.push(...MenuBuilder.addMarkdownItems(spec.info.description || '', options));
if (spec['x-tagGroups']) {
items.push(
...MenuBuilder.getTagGroupsItems(parser, undefined, spec['x-tagGroups'], tagsMap, options),
Expand All @@ -53,8 +57,11 @@ export class MenuBuilder {
* extracts items from markdown description
* @param description - markdown source
*/
static addMarkdownItems(description: string): ContentItemModel[] {
const renderer = new MarkdownRenderer();
static addMarkdownItems(
description: string,
options: RedocNormalizedOptions,
): ContentItemModel[] {
const renderer = new MarkdownRenderer(options);
const headings = renderer.extractHeadings(description || '');

const mapHeadingsDeep = (parent, items, depth = 1) =>
Expand All @@ -64,6 +71,14 @@ export class MenuBuilder {
if (heading.items) {
group.items = mapHeadingsDeep(group, heading.items, depth + 1);
}
if (
MarkdownRenderer.containsComponent(
group.description || '',
SECURITY_DEFINITIONS_COMPONENT_NAME,
)
) {
setSecuritySchemePrefix(group.id + '/');
}
return group;
});

Expand Down
5 changes: 4 additions & 1 deletion src/services/MenuStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ export class MenuStore {
if (item) {
this.activateAndScroll(item, false);
} 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}"]`);
}
return item !== undefined;
};

/**
Expand Down
6 changes: 3 additions & 3 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types';

import { appendToMdHeading, IS_BROWSER } from '../utils/';
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 { RedocNormalizedOptions } from './RedocNormalizedOptions';

Expand Down Expand Up @@ -74,8 +74,8 @@ export class OpenAPIParser {
) {
// Automatically inject Authentication section with SecurityDefinitions component
const description = spec.info.description || '';
if (!MarkdownRenderer.containsComponent(description, 'security-definitions')) {
const comment = buildComponentComment('security-definitions');
if (!MarkdownRenderer.containsComponent(description, SECURITY_DEFINITIONS_COMPONENT_NAME)) {
const comment = buildComponentComment(SECURITY_DEFINITIONS_COMPONENT_NAME);
spec.info.description = appendToMdHeading(description, 'Authentication', comment);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/models/SecurityRequirement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenAPISecurityRequirement, OpenAPISecurityScheme } from '../../types';
import { SECURITY_SCHEMES_SECTION } from '../../utils/openapi';
import { SECURITY_SCHEMES_SECTION_PREFIX } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser';

export interface SecurityScheme extends OpenAPISecurityScheme {
Expand Down Expand Up @@ -27,7 +27,7 @@ export class SecurityRequirementModel {
return {
...scheme,
id,
sectionId: SECURITY_SCHEMES_SECTION + id,
sectionId: SECURITY_SCHEMES_SECTION_PREFIX + id,
scopes,
};
})
Expand Down
4 changes: 2 additions & 2 deletions src/services/models/SecuritySchemes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenAPISecurityScheme, Referenced } from '../../types';
import { SECURITY_SCHEMES_SECTION } from '../../utils/openapi';
import { SECURITY_SCHEMES_SECTION_PREFIX } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser';

export class SecuritySchemeModel {
Expand All @@ -25,7 +25,7 @@ export class SecuritySchemeModel {
constructor(parser: OpenAPIParser, id: string, scheme: Referenced<OpenAPISecurityScheme>) {
const info = parser.deref(scheme);
this.id = id;
this.sectionId = SECURITY_SCHEMES_SECTION + id;
this.sectionId = SECURITY_SCHEMES_SECTION_PREFIX + id;
this.type = info.type;
this.description = info.description || '';
if (info.type === 'apiKey') {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit f903406

Please sign in to comment.