Skip to content

Commit

Permalink
fix(openapi): silence ignore dangerous error when generate openapi spec
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Jan 2, 2025
1 parent 9487fcd commit 73eb96a
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions packages/openapi/src/openapi-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { CompositeSchemaConverter } from './schema-converter'
import { type PublicSchemaUtils, SchemaUtils } from './schema-utils'
import { forEachAllContractProcedure, standardizeHTTPPath } from './utils'

type ErrorHandlerStrategy = 'throw' | 'log' | 'ignore'

export interface OpenAPIGeneratorOptions {
contentBuilder?: PublicOpenAPIContentBuilder
parametersBuilder?: PublicOpenAPIParametersBuilder
Expand Down Expand Up @@ -44,11 +46,11 @@ export interface OpenAPIGeneratorOptions {
ignoreUndefinedPathProcedures?: boolean

/**
* Throw error when you have error in OpenAPI generator
* What to do when we found an error with our router
*
* @default false
* @default 'throw'
*/
throwOnError?: boolean
errorHandlerStrategy?: ErrorHandlerStrategy
}

export class OpenAPIGenerator {
Expand All @@ -60,8 +62,11 @@ export class OpenAPIGenerator {
private readonly pathParser: PublicOpenAPIPathParser
private readonly inputStructureParser: PublicOpenAPIInputStructureParser
private readonly outputStructureParser: PublicOpenAPIOutputStructureParser
private readonly errorHandlerStrategy: ErrorHandlerStrategy
private readonly ignoreUndefinedPathProcedures: boolean
private readonly considerMissingTagDefinitionAsError: boolean

constructor(private readonly options?: OpenAPIGeneratorOptions) {
constructor(options?: OpenAPIGeneratorOptions) {
this.parametersBuilder = options?.parametersBuilder ?? new OpenAPIParametersBuilder()
this.schemaConverter = new CompositeSchemaConverter(options?.schemaConverters ?? [])
this.schemaUtils = options?.schemaUtils ?? new SchemaUtils()
Expand All @@ -71,6 +76,10 @@ export class OpenAPIGenerator {

this.inputStructureParser = options?.inputStructureParser ?? new OpenAPIInputStructureParser(this.schemaConverter, this.schemaUtils, this.pathParser)
this.outputStructureParser = options?.outputStructureParser ?? new OpenAPIOutputStructureParser(this.schemaConverter, this.schemaUtils)

this.errorHandlerStrategy = options?.errorHandlerStrategy ?? 'throw'
this.ignoreUndefinedPathProcedures = options?.ignoreUndefinedPathProcedures ?? false
this.considerMissingTagDefinitionAsError = options?.considerMissingTagDefinitionAsError ?? false
}

async generate(router: ContractRouter | ANY_ROUTER, doc: Omit<OpenAPI.OpenAPIObject, 'openapi'>): Promise<OpenAPI.OpenAPIObject> {
Expand All @@ -86,7 +95,7 @@ export class OpenAPIGenerator {
// TODO: inputExample and outputExample ???
const def = contract['~orpc']

if (this.options?.ignoreUndefinedPathProcedures && def.route?.path === undefined) {
if (this.ignoreUndefinedPathProcedures && def.route?.path === undefined) {
return
}

Expand Down Expand Up @@ -133,7 +142,7 @@ export class OpenAPIGenerator {
: undefined,
}

if (this.options?.considerMissingTagDefinitionAsError && def.route?.tags) {
if (this.considerMissingTagDefinitionAsError && def.route?.tags) {
const missingTag = def.route?.tags.find(tag => !rootTags.includes(tag))

if (missingTag !== undefined) {
Expand Down Expand Up @@ -167,11 +176,16 @@ export class OpenAPIGenerator {
Happened at path: ${path.join('.')}
`, { cause: e })

if (this.options?.throwOnError) {
if (this.errorHandlerStrategy === 'throw') {
throw error
}

console.error(error)
if (this.errorHandlerStrategy === 'log') {
console.error(error)
}
}
else {
throw e
}
}
})
Expand Down

0 comments on commit 73eb96a

Please sign in to comment.