Skip to content

Commit

Permalink
fix: 🐛 stringify schemasByName & defsSchemas
Browse files Browse the repository at this point in the history
to prevent nullish values to be skipped by defu
  • Loading branch information
Morgbn committed Oct 10, 2023
1 parent 7a41df4 commit 720f5f5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export default defineNuxtModule<ModuleOptions>({
// Set up runtime configuration
nuxt.options.runtimeConfig.oa = defu(nuxt.options.runtimeConfig.oa, {
...options,
schemasByName,
defsSchemas
stringifiedSchemasByName: JSON.stringify(schemasByName),
stringifiedDefsSchemas: JSON.stringify(defsSchemas)
})

// Transpile runtime
Expand Down Expand Up @@ -147,8 +147,8 @@ export default defineNuxtModule<ModuleOptions>({
declare module 'nuxt/schema' {
interface RuntimeConfig {
oa: ModuleOptions & {
readonly schemasByName: Record<keyof OaModels, Schema>,
readonly defsSchemas: DefsSchema[]
readonly stringifiedSchemasByName: string,
readonly stringifiedDefsSchemas: string
}
}
}
8 changes: 6 additions & 2 deletions src/runtime/server/helpers/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import type { Collection, Document, ObjectId, OptionalUnlessRequiredId, WithId }
import { HookCallback, Hookable } from 'hookable'
import { createError } from 'h3'
import type { H3Event } from 'h3'
import type { Schema, OaModels } from '../../types'
import type { Schema, OaModels, DefsSchema } from '../../types'
import { useCol, useObjectId } from './db'
import { pluralize } from './pluralize'
import { decrypt, encrypt } from './cipher'

import { useRuntimeConfig } from '#imports'

const logger = useLogger('nuxt-oa')
const { cipherAlgo, cipherKey, schemasByName, defsSchemas } = useRuntimeConfig().oa
const { cipherAlgo, cipherKey, stringifiedSchemasByName, stringifiedDefsSchemas } = useRuntimeConfig().oa
const schemasByName = JSON.parse(stringifiedSchemasByName) as Record<keyof OaModels, Schema>
const defsSchemas = JSON.parse(stringifiedDefsSchemas) as DefsSchema[]

const ajv = new Ajv({ removeAdditional: true, schemas: defsSchemas })
addFormats(ajv)
Expand Down Expand Up @@ -347,6 +349,8 @@ export default class Model<T extends keyof OaModels & string> extends Hookable<M
}
}

export type ModelInstance = typeof Model

const modelsCache: Record<string, any> = {}
export function useOaModel<T extends keyof OaModels & string> (name: T): Model<T> {
if (!modelsCache[name]) {
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/server/openApiPage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { defineEventHandler, setHeader } from 'h3'
import type { Schema } from '../types'
import type { DefsSchema, Schema } from '../types'
import { cleanSchema } from './helpers/model'
import { paths, components } from './helpers/router'

import { useRuntimeConfig } from '#imports'

const { openApiGeneralInfo, openApiServers, schemasByName, defsSchemas } = useRuntimeConfig().oa
const { openApiGeneralInfo, openApiServers, stringifiedSchemasByName, stringifiedDefsSchemas } = useRuntimeConfig().oa

const defsSchemas = JSON.parse(stringifiedDefsSchemas) as DefsSchema[]
const hasMultiDefsId = defsSchemas.length > 1
const defsComponents = defsSchemas.reduce<Record<string, Schema>>((o, schema) => {
for (const key in schema.definitions) {
Expand All @@ -17,7 +18,7 @@ const defsComponents = defsSchemas.reduce<Record<string, Schema>>((o, schema) =>

const refRe = /("\$ref": ")(\w+)(?:\.\w+)?#(?:\/(\w+))+"/gm
const refSubst = hasMultiDefsId ? '$1#/components/schemas/$2_$3"' : '$1#/components/schemas/$3"'
const schemas = Object.entries(schemasByName)
const schemas = Object.entries(JSON.parse(stringifiedSchemasByName))
.reduce((o: Record<string, Schema>, [key, schema]) => {
schema = JSON.parse(JSON.stringify(schema, null, 2).replace(refRe, refSubst))
o[key] = cleanSchema(schema as Schema)
Expand Down

0 comments on commit 720f5f5

Please sign in to comment.