Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show the display name in the sidebar #154

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/examples/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ tags:
description: Everything about your Pets
- name: store
description: Access to Petstore orders
x-displayName: Petstore orders
- name: user
description: Operations about user
- name: pet_model
Expand Down
27 changes: 26 additions & 1 deletion packages/docusaurus-plugin-openapi/src/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import sdk, { Collection } from "postman-collection";

import { ApiMetadata, ApiPageMetadata, InfoPageMetadata } from "../types";
import { sampleFromSchema } from "./createExample";
import { OpenApiObject, OpenApiObjectWithRef } from "./types";
import { OpenApiObject, OpenApiObjectWithRef, TagObject } from "./types";

/**
* Finds any reference objects in the OpenAPI definition and resolves them to a finalized value.
Expand Down Expand Up @@ -151,6 +151,9 @@ function createItems(openapiData: OpenApiObject): ApiMetadata[] {
frontMatter: {},
api: {
...defaults,
tags: operationObject.tags?.map((tagName) =>
getTagDisplayName(tagName, openapiData.tags ?? [])
),
method,
path,
servers,
Expand Down Expand Up @@ -329,3 +332,25 @@ export async function processOpenapiFile(

return items;
}

// order for picking items as a display name of tags
const tagDisplayNameProperties = ["x-displayName", "name"] as const;

function getTagDisplayName(tagName: string, tags: TagObject[]): string {
// find the very own tagObject
const tagObject = tags.find((tagObject) => tagObject.name === tagName) ?? {
// if none found, just fake one
name: tagName,
};

// return the first found and filled value from the property list
for (const property of tagDisplayNameProperties) {
const displayName = tagObject[property];
if (typeof displayName === "string") {
return displayName;
}
}

// always default to the tagName
return tagName;
}
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-openapi/src/openapi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export interface TagObject {
name: string;
description?: string;
externalDocs?: ExternalDocumentationObject;
"x-displayName"?: string;
}

export interface ReferenceObject {
Expand Down