-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: Cannot read properties of undefined (reading 'isLazyTypeFunc') #14
Comments
@MihaiVoinea did you find a solution to this problem? |
@therosbif Nope, switched to another package. |
Will be fixed in #18 |
!isZodDto(type) calls defaultExplore, but exploreModelSchema is an instance function that internally refers to undefined 'this' and causes an error. Since the function is extended with 'prototype', this.exploreModelSchema causes a stack over due to recursion 😅 It would be best if nestjs-swagger could pass a callback to exploreModelSchema, but this is how it worked for now! import { Type } from '@nestjs/common';
import { SchemaObjectFactory } from '@nestjs/swagger/dist/services/schema-object-factory';
import { isZodDto } from 'nestjs-zod/dto';
import { zodToOpenAPI } from 'nestjs-zod';
import * as _ from 'lodash';
function getSchemaObjectFactory(): Type<SchemaObjectFactory> {
return SchemaObjectFactory;
}
function isLazyTypeFunc(type) {
return _.isFunction(type) && type.name == 'type';
}
export function patchNestJsSwagger(
SchemaObjectFactory = getSchemaObjectFactory(),
) {
if (SchemaObjectFactory.prototype.__patchedWithLoveByNestjsZod) return;
SchemaObjectFactory.prototype.exploreModelSchema = function (
this: SchemaObjectFactory,
type,
schemas,
schemaRefsStack,
) {
if (isLazyTypeFunc(type)) {
const factory = type as () => Type<unknown>;
type = factory();
}
if (!isZodDto(type)) {
if (isLazyTypeFunc(type)) {
type = type();
}
const propertiesWithType = this.extractPropertiesFromType(
type,
schemas,
schemaRefsStack,
);
if (!propertiesWithType) {
return '';
}
const typeDefinition = {
type: 'object',
properties: _.mapValues(
_.keyBy(propertiesWithType, 'name'),
(property) =>
_.omit(property, ['name', 'isArray', 'required', 'enumName']),
),
};
const typeDefinitionRequiredFields = propertiesWithType
.filter((property) => property.required != false)
.map((property) => property.name);
if (typeDefinitionRequiredFields.length > 0) {
typeDefinition['required'] = typeDefinitionRequiredFields;
}
schemas[type.name] = typeDefinition;
return type.name;
}
schemas[type.name] = zodToOpenAPI(type.schema);
return type.name;
};
SchemaObjectFactory.prototype.__patchedWithLoveByNestjsZod = true;
} |
May be fixed in #42 |
Getting
TypeError: Cannot read properties of undefined (reading 'isLazyTypeFunc')
when I usepatchNestJsSwagger
and I try to apply aApiBadRequestResponse
decorator from @nestjs/swagger. Is there any way to extend the OpenAPI object through @nestjs/swagger decorators when using patchNestJsSwagger?The text was updated successfully, but these errors were encountered: