Skip to content
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

Closed
MihaiVoinea opened this issue Sep 13, 2022 · 5 comments

Comments

@MihaiVoinea
Copy link

Getting TypeError: Cannot read properties of undefined (reading 'isLazyTypeFunc') when I use patchNestJsSwagger and I try to apply a ApiBadRequestResponse decorator from @nestjs/swagger. Is there any way to extend the OpenAPI object through @nestjs/swagger decorators when using patchNestJsSwagger?

@jackgdll
Copy link
Contributor

@MihaiVoinea did you find a solution to this problem?

@MihaiVoinea
Copy link
Author

@therosbif Nope, switched to another package.

@risen228
Copy link
Collaborator

Will be fixed in #18

@HiroyukiNIshimura
Copy link

!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;
}

@risen228
Copy link
Collaborator

risen228 commented May 2, 2023

May be fixed in #42

@risen228 risen228 closed this as completed May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants