fix: openapi-to-ts fixes for github openapi spec

pull/701/head
Travis Fischer 2025-03-25 19:16:30 +08:00
rodzic 7a98b3c9dc
commit f867fa812e
4 zmienionych plików z 21116 dodań i 4244 usunięć

Wyświetl plik

@ -60,6 +60,7 @@ Some things you may want to tweak:
- Fix github example issue with `nullable()` zod schema parameter - Fix github example issue with `nullable()` zod schema parameter
- Fix github `json-schema-to-zod` example issue with string enum given default value `true` as a non-string boolean - Fix github `json-schema-to-zod` example issue with string enum given default value `true` as a non-string boolean
- Fix github `gists/get-revision` missing path parameter because of ref - Fix github `gists/get-revision` missing path parameter because of ref
- Fix github `activity_star_repo_for_authenticated_user` path seems incorrect
## License ## License

Wyświetl plik

@ -199,7 +199,7 @@ export async function generateTSFromOpenAPI({
const operationResponseJSONSchemas: Record<string, IJsonSchema> = {} const operationResponseJSONSchemas: Record<string, IJsonSchema> = {}
const operationParamsSources: Record<string, string> = {} const operationParamsSources: Record<string, Set<string>> = {}
let operationParamsUnionSource: string | undefined let operationParamsUnionSource: string | undefined
// eslint-disable-next-line unicorn/consistent-function-scoping // eslint-disable-next-line unicorn/consistent-function-scoping
@ -212,15 +212,21 @@ export async function generateTSFromOpenAPI({
const derefed = dereference(schema, parser.$refs, componentsToProcess) const derefed = dereference(schema, parser.$refs, componentsToProcess)
if (derefed?.properties) { if (derefed?.properties) {
for (const key of Object.keys(derefed.properties)) { for (const key of Object.keys(derefed.properties)) {
assert( // assert(
!operationParamsSources[key], // !operationParamsSources[key],
`Duplicate params key ${key} for operation ${operationName} from ${operationParamsSources[key]} and ${source}` // `Duplicate params key ${key} for operation ${operationName} from ${operationParamsSources[key]} and ${source}`
) // )
operationParamsSources[key] = source operationParamsSources[key] = new Set([
...(operationParamsSources[key] || []),
source
])
} }
} else if (derefed?.anyOf || derefed?.oneOf) { } else if (derefed?.anyOf || derefed?.oneOf) {
const componentName = getComponentDisplayName(schema.$ref) const componentName = getComponentDisplayName(schema.$ref)
operationParamsSources[componentName] = source operationParamsSources[componentName] = new Set([
...(operationParamsSources[componentName] || []),
source
])
// TODO: handle this case // TODO: handle this case
assert( assert(
@ -237,11 +243,14 @@ export async function generateTSFromOpenAPI({
} }
for (const key of Object.keys(schema.properties)) { for (const key of Object.keys(schema.properties)) {
assert( // assert(
!operationParamsSources[key], // !operationParamsSources[key],
`Duplicate params key ${key} for operation ${operationName} from ${operationParamsSources[key]} and ${source}` // `Duplicate params key "${key}" for operation "${operationName}" from "${operationParamsSources[key]}" and "${source}"`
) // )
operationParamsSources[key] = source operationParamsSources[key] = new Set([
...(operationParamsSources[key] || []),
source
])
} }
} }
@ -255,7 +264,10 @@ export async function generateTSFromOpenAPI({
if (schema.anyOf || schema.oneOf) { if (schema.anyOf || schema.oneOf) {
operationParamsJSONSchema.anyOf = schema.anyOf operationParamsJSONSchema.anyOf = schema.anyOf
operationParamsJSONSchema.oneOf = schema.oneOf operationParamsJSONSchema.oneOf = schema.oneOf
operationParamsSources[schema.title || '__union__'] = source operationParamsSources[schema.title || '__union__'] = new Set([
...(operationParamsSources[schema.title || '__union__'] || []),
source
])
// TODO: handle this case // TODO: handle this case
assert( assert(
@ -311,7 +323,10 @@ export async function generateTSFromOpenAPI({
} }
if (operation.parameters) { if (operation.parameters) {
const params = convertParametersToJSONSchema(operation.parameters) const parameters = operation.parameters.map((param) =>
dereference(param, parser.$refs, componentsToProcess)
)
const params = convertParametersToJSONSchema(parameters)
if (params.body) { if (params.body) {
addJSONSchemaParams(params.body, 'body') addJSONSchemaParams(params.body, 'body')
@ -458,28 +473,28 @@ export async function generateTSFromOpenAPI({
// ) // )
// ) // )
const queryParams = Object.keys(operationParamsSources).filter( const queryParams = Object.keys(operationParamsSources).filter((key) =>
(key) => operationParamsSources[key] === 'query' operationParamsSources[key]?.has('query')
) )
const hasQueryParams = queryParams.length > 0 const hasQueryParams = queryParams.length > 0
const bodyParams = Object.keys(operationParamsSources).filter( const bodyParams = Object.keys(operationParamsSources).filter((key) =>
(key) => operationParamsSources[key] === 'body' operationParamsSources[key]?.has('body')
) )
const hasBodyParams = bodyParams.length > 0 const hasBodyParams = bodyParams.length > 0
const formDataParams = Object.keys(operationParamsSources).filter( const formDataParams = Object.keys(operationParamsSources).filter((key) =>
(key) => operationParamsSources[key] === 'formData' operationParamsSources[key]?.has('formData')
) )
const hasFormDataParams = formDataParams.length > 0 const hasFormDataParams = formDataParams.length > 0
const pathParams = Object.keys(operationParamsSources).filter( const pathParams = Object.keys(operationParamsSources).filter((key) =>
(key) => operationParamsSources[key] === 'path' operationParamsSources[key]?.has('path')
) )
const hasPathParams = pathParams.length > 0 const hasPathParams = pathParams.length > 0
const headersParams = Object.keys(operationParamsSources).filter( const headersParams = Object.keys(operationParamsSources).filter((key) =>
(key) => operationParamsSources[key] === 'headers' operationParamsSources[key]?.has('headers')
) )
const hasHeadersParams = headersParams.length > 0 const hasHeadersParams = headersParams.length > 0

Wyświetl plik

@ -123,6 +123,10 @@ function createParserOverride({
withJsdocs: boolean withJsdocs: boolean
}): ParserOverride { }): ParserOverride {
const jsonSchemaToZodParserOverride: ParserOverride = (schema, _refs) => { const jsonSchemaToZodParserOverride: ParserOverride = (schema, _refs) => {
if ('nullable' in schema && schema.nullable) {
delete schema.nullable
}
if ('$ref' in schema) { if ('$ref' in schema) {
const ref = schema.$ref as string const ref = schema.$ref as string
assert(ref, `Invalid schema: $ref not found for ${schema.$ref}`) assert(ref, `Invalid schema: $ref not found for ${schema.$ref}`)