Skip to content

Commit

Permalink
fix(spec): generate defaultTag for operations that has no tags (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
enzonotario authored Nov 1, 2024
1 parent 89c4b1a commit 933cc1b
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 36 deletions.
7 changes: 4 additions & 3 deletions docs/composables/useTheme.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default {
showPathsSummary: true, // Show a summary of the paths when grouping by tags.
avoidCirculars: false, // Avoid circular references when parsing schemas.
lazyRendering: false, // Lazy render Paths and Tags components.
defaultTag: 'Default', // Default tag to use when a path has no tags.
},
})
}
Expand Down Expand Up @@ -150,6 +151,6 @@ export default {

## Spec Configuration

| Function | Description | Default Value | Allowed Values |
|-----------------|------------------------------|-----------------------------------------------------------------------|-------------------------------------------------------------------------------|
| `setSpecConfig` | Sets the spec configuration. | `{ groupByTags: true, collapsePaths: false, showPathsSummary: true, avoidCirculars: false, lazyRendering: false }` | `{ groupByTags: boolean, collapsePaths: boolean, showPathsSummary: boolean, avoidCirculars: boolean, lazyRendering: boolean }` |
| Function | Description | Default Value | Allowed Values |
|-----------------|------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| `setSpecConfig` | Sets the spec configuration. | `{ groupByTags: true, collapsePaths: false, showPathsSummary: true, avoidCirculars: false, lazyRendering: false, defaultTag: 'Default' }` | `{ groupByTags: boolean, collapsePaths: boolean, showPathsSummary: boolean, avoidCirculars: boolean, lazyRendering: boolean, defaultTag: string }` |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vitepress-openapi",
"type": "module",
"version": "0.0.3-alpha.46",
"version": "0.0.3-alpha.47",
"packageManager": "[email protected]",
"homepage": "https://vitepress-openapi.vercel.app/",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Path/OAPathsByTags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const internalTags = ref([
...(pathsWithoutTags.length
? [
{
tag: t('Default'),
tag: t(useTheme().getSpecConfig.defaultTag),
paths: pathsWithoutTags,
isOpen: !themeConfig.getSpecConfig().collapsePaths.value,
},
Expand Down
8 changes: 5 additions & 3 deletions src/composables/useOpenapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export function useOpenapi({
spec: null,
config: null,
}) {
if (config) {
useTheme(config)
}

if (spec !== null) {
setupOpenApi({ spec, config })
}
Expand All @@ -49,9 +53,7 @@ export function useOpenapi({

function addSchema({ id, spec, config }) {
const openapi = createOpenApiInstance({ spec })
if (config) {
useTheme(config)
}

schemas.set(id, {
...openapi,
id,
Expand Down
2 changes: 1 addition & 1 deletion src/composables/usePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function usePaths({
const openapi = OpenApi({ spec, transformedSpec: transformSpec(spec) })

function getTags() {
return openapi.getFilteredTests()
return openapi.getFilteredTags()
}

return {
Expand Down
18 changes: 16 additions & 2 deletions src/composables/useSidebar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { OpenApi, httpVerbs, useOpenapi } from 'vitepress-openapi'
import { transformSpec } from '../lib/transformSpec'
import type { OpenAPI } from './useOpenapi'
import { useTheme } from './useTheme'

interface GenerateSidebarGroupsOptions {
tags?: string[] | null
Expand All @@ -17,26 +19,38 @@ const defaultOptions = {
spec: null,
linkPrefix: '/operations/',
tagLinkPrefix: '/tags/',
defaultTag: 'Default',
}

export function useSidebar({
spec,
linkPrefix,
tagLinkPrefix,
defaultTag,
}: {
spec?: OpenAPI
linkPrefix?: string
tagLinkPrefix?: string
defaultTag?: string
} = {
...defaultOptions,
}) {
useTheme({
spec: {
defaultTag: defaultTag || defaultOptions.defaultTag,
},
})

const options = {
spec: spec || useOpenapi().json,
linkPrefix: linkPrefix || defaultOptions.linkPrefix,
tagLinkPrefix: tagLinkPrefix || defaultOptions.tagLinkPrefix,
}

const openapi = OpenApi({ spec: options.spec })
const openapi = OpenApi({
spec: options.spec,
transformedSpec: transformSpec(options.spec),
})

function sidebarItemTemplate(method: string, title: string) {
return `<span class="OASidebarItem group/oaSidebarItem">
Expand Down Expand Up @@ -136,7 +150,7 @@ export function useSidebar({
tags,
linkPrefix,
}: GenerateSidebarGroupsOptions = {}) {
tags = tags || openapi.getFilteredTests().map(({ name }) => name)
tags = tags || openapi.getFilteredTags().map(({ name }) => name)
linkPrefix = linkPrefix || options.tagLinkPrefix

if (!openapi.getPaths()) {
Expand Down
16 changes: 11 additions & 5 deletions src/composables/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ export interface I18nConfig {
}

export interface SpecConfig {
groupByTags: Ref<boolean>
collapsePaths: Ref<boolean>
showPathsSummary: Ref<boolean>
avoidCirculars: Ref<boolean>
lazyRendering: Ref<boolean>
groupByTags?: Ref<boolean>
collapsePaths?: Ref<boolean>
showPathsSummary?: Ref<boolean>
avoidCirculars?: Ref<boolean>
lazyRendering?: Ref<boolean>
defaultTag?: string
}

export interface UseThemeConfig {
Expand Down Expand Up @@ -168,6 +169,7 @@ const themeConfig: UseThemeConfig = {
showPathsSummary: ref(true),
avoidCirculars: ref(false),
lazyRendering: ref(false),
defaultTag: 'Default',
},
}

Expand Down Expand Up @@ -464,6 +466,10 @@ export function useTheme(config: Partial<UseThemeConfig> = {}) {
if (config.lazyRendering !== undefined) {
themeConfig.spec.lazyRendering.value = config.lazyRendering
}

if (config.defaultTag !== undefined) {
themeConfig.spec.defaultTag = config.defaultTag
}
}

return {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export function OpenApi({
verb,
operationId,
summary,
tags,
tags: tags ?? [],
}
})
})
Expand Down Expand Up @@ -235,7 +235,7 @@ export function OpenApi({
}

function getPathsWithoutTags() {
return filterPaths(operation => !operation?.tags)
return filterPaths(operation => !operation?.tags || operation.tags.length === 0)
}

function getTags() {
Expand All @@ -246,7 +246,7 @@ export function OpenApi({
}))
}

function getFilteredTests() {
function getFilteredTags() {
const operationsTags = getOperationsTags()

const tags = getTags()
Expand Down Expand Up @@ -284,6 +284,6 @@ export function OpenApi({
getPathsByTags,
getPathsWithoutTags,
getTags,
getFilteredTests,
getFilteredTags,
}
}
21 changes: 21 additions & 0 deletions src/lib/generateMissingTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useTheme } from 'vitepress-openapi'

export function generateMissingTags(spec: any) {
const paths = spec.paths

if (!paths) {
return spec
}

const defaultTag = useTheme().getSpecConfig().defaultTag

for (const path of Object.values(paths)) {
for (const verb of Object.keys(path)) {
const operation = path[verb]
const tags = operation.tags || [defaultTag]
operation.tags = tags
}
}

return spec
}
2 changes: 2 additions & 0 deletions src/lib/transformSpec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateMissingOperationIds } from './generateMissingOperationIds'
import { generateMissingSummary } from './generateMissingSummary'
import { generateMissingTags } from './generateMissingTags'

export function transformSpec(spec) {
if (import.meta.env.VITE_DEBUG) {
Expand All @@ -17,6 +18,7 @@ export function transformSpec(spec) {
if (spec?.paths) {
spec = generateMissingOperationIds(spec)
spec = generateMissingSummary(spec)
spec = generateMissingTags(spec)
}

return Object.assign({}, spec)
Expand Down
13 changes: 3 additions & 10 deletions test/composables/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('openapi with spec', () => {
expect(paths).toMatchObject({})
})

it('getPathsWithoutTags returns paths without tags', () => {
it('sets defaultTag for operations without tags', () => {
const api = createOpenApiInstance({
spec: {
...spec,
Expand All @@ -159,16 +159,9 @@ describe('openapi with spec', () => {
},
})

const paths = api.getPathsWithoutTags()
expect(api.getPathsWithoutTags()).toMatchObject({})

expect(paths).toMatchObject({
'/no-tags': {
get: {
operationId: 'getNoTags',
summary: 'GET /no-tags',
},
},
})
expect(api.getOperationsTags()).toEqual(['users', 'Default'])
})

it('getPathsWithoutTags returns empty array if no paths without tags', () => {
Expand Down
6 changes: 0 additions & 6 deletions test/composables/useSidebar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,4 @@ describe('itemsByTags', () => {
{ text: 'pets', link: '/tags/pets' },
])
})

it('returns an empty array when there are no paths', () => {
const emptySidebar = useSidebar({ spec: {} })
const result = emptySidebar.itemsByTags()
expect(result).toEqual([])
})
})
2 changes: 2 additions & 0 deletions test/composables/useTheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('composition API', () => {
showPathsSummary: ref(false),
avoidCirculars: ref(false),
lazyRendering: ref(false),
defaultTag: 'Default',
})
})

Expand Down Expand Up @@ -328,6 +329,7 @@ describe('useTheme', () => {
showPathsSummary: ref(true),
avoidCirculars: ref(false),
lazyRendering: ref(false),
defaultTag: 'Default',
})
})

Expand Down

0 comments on commit 933cc1b

Please sign in to comment.