docs-util: remove inferring of query parameters (#13834)

This commit is contained in:
Shahed Nasser
2025-10-23 14:15:59 +03:00
committed by GitHub
parent d51ae2768b
commit 2ce0e37e98
2 changed files with 10 additions and 76 deletions
@@ -228,7 +228,10 @@ class OasSchemaHelper {
return clonedSchema return clonedSchema
} }
isSchemaEmpty(schema: OpenApiSchema): boolean { isSchemaEmpty(schema?: OpenApiSchema): boolean {
if (!schema || !Object.keys(schema).length) {
return true
}
switch (schema.type) { switch (schema.type) {
case "object": case "object":
const isPropertiesEmpty = const isPropertiesEmpty =
@@ -135,13 +135,6 @@ class OasKindGenerator extends FunctionKindGenerator {
}, },
] ]
readonly RESPONSE_TYPE_NAMES = ["MedusaResponse"] readonly RESPONSE_TYPE_NAMES = ["MedusaResponse"]
readonly FIELD_QUERY_PARAMS = ["fields", "expand"]
readonly PAGINATION_QUERY_PARAMS = [
"limit",
"offset",
"order",
"with_deleted",
]
/** /**
* This map collects tags of all the generated OAS, then, once the generation process finishes, * This map collects tags of all the generated OAS, then, once the generation process finishes,
@@ -358,13 +351,13 @@ class OasKindGenerator extends FunctionKindGenerator {
}) })
oas.parameters?.push(...queryParameters) oas.parameters?.push(...queryParameters)
if (requestSchema && Object.keys(requestSchema).length > 0) { if (!this.oasSchemaHelper.isSchemaEmpty(requestSchema)) {
oas.requestBody = { oas.requestBody = {
content: { content: {
"application/json": { "application/json": {
schema: schema:
this.oasSchemaHelper.namedSchemaToReference(requestSchema) || this.oasSchemaHelper.namedSchemaToReference(requestSchema!) ||
this.oasSchemaHelper.schemaChildrenToRefs(requestSchema), this.oasSchemaHelper.schemaChildrenToRefs(requestSchema!),
}, },
}, },
} }
@@ -617,10 +610,7 @@ class OasKindGenerator extends FunctionKindGenerator {
parametersUpdated = updatedRequestSchema?.wasUpdated || parametersUpdated parametersUpdated = updatedRequestSchema?.wasUpdated || parametersUpdated
if ( if (this.oasSchemaHelper.isSchemaEmpty(updatedRequestSchema?.schema)) {
!updatedRequestSchema?.schema ||
Object.keys(updatedRequestSchema.schema).length === 0
) {
// if there's no request schema, remove it from the OAS // if there's no request schema, remove it from the OAS
delete oas.requestBody delete oas.requestBody
} else { } else {
@@ -630,10 +620,10 @@ class OasKindGenerator extends FunctionKindGenerator {
"application/json": { "application/json": {
schema: schema:
this.oasSchemaHelper.namedSchemaToReference( this.oasSchemaHelper.namedSchemaToReference(
updatedRequestSchema.schema updatedRequestSchema!.schema!
) || ) ||
this.oasSchemaHelper.schemaChildrenToRefs( this.oasSchemaHelper.schemaChildrenToRefs(
updatedRequestSchema.schema updatedRequestSchema!.schema!
), ),
}, },
}, },
@@ -1224,53 +1214,6 @@ class OasKindGenerator extends FunctionKindGenerator {
const requestType = this.checker.getTypeFromTypeNode( const requestType = this.checker.getTypeFromTypeNode(
node.parameters[0].type node.parameters[0].type
) as ts.TypeReference ) as ts.TypeReference
// TODO for now I'll use the type for validatedQuery until
// we have an actual approach to infer query types
const querySymbol = requestType.getProperty("validatedQuery")
if (querySymbol) {
const { shouldAddFields, shouldAddPagination } =
this.shouldAddQueryParams(node)
const queryType = this.checker.getTypeOfSymbol(querySymbol)
const queryTypeName = this.checker.typeToString(queryType)
queryType.getProperties().forEach((property) => {
const propertyName = property.getName()
// if this is a field / pagination query parameter and
// they're not used in the route, don't add them.
if (
(this.FIELD_QUERY_PARAMS.includes(propertyName) &&
!shouldAddFields) ||
(this.PAGINATION_QUERY_PARAMS.includes(propertyName) &&
!shouldAddPagination)
) {
return
}
const propertyType = this.checker.getTypeOfSymbol(property)
const descriptionOptions: SchemaDescriptionOptions = {
typeStr: propertyName,
parentName: tagName,
rawParentName: queryTypeName,
node: property.valueDeclaration,
symbol: property,
nodeType: propertyType,
}
queryParameters.push(
this.getParameterObject({
name: propertyName,
type: "query",
description: this.getSchemaDescription(descriptionOptions),
required: this.isRequired(property),
schema: this.typeToSchema({
itemType: propertyType,
title: propertyName,
descriptionOptions,
context: "query",
saveSchema: !forUpdate,
symbol: property,
}),
})
)
})
}
const requestTypeArguments = const requestTypeArguments =
requestType.typeArguments || requestType.aliasTypeArguments requestType.typeArguments || requestType.aliasTypeArguments
@@ -2763,18 +2706,6 @@ class OasKindGenerator extends FunctionKindGenerator {
} }
} }
shouldAddQueryParams(node: FunctionNode): {
shouldAddFields: boolean
shouldAddPagination: boolean
} {
const fnText = node.getText()
return {
shouldAddFields: fnText.includes(`req.queryConfig.fields`),
shouldAddPagination: fnText.includes(`req.queryConfig.pagination`),
}
}
hasResponseType(node: FunctionNode, oas: OpenApiOperation): boolean { hasResponseType(node: FunctionNode, oas: OpenApiOperation): boolean {
const oldResponseStatus = Object.keys(oas.responses!).find( const oldResponseStatus = Object.keys(oas.responses!).find(
(status) => !Object.keys(DEFAULT_OAS_RESPONSES).includes(status) (status) => !Object.keys(DEFAULT_OAS_RESPONSES).includes(status)