mapper icon indicating copy to clipboard operation
mapper copied to clipboard

@nestjs/swagger

Open KIMBEOBWOO opened this issue 2 years ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the issue

If you specify a null property type for an Entity class as a Union type such as "string | null", the property is not automatically mapped.

Models/DTOs/VMs

In Destination class file test.entity.ts

@AutoMap()
@Column({ type: 'text', nullable: true })
test?: string | null;

In Source class file test.dto.ts

@AutoMap()
@IsString()
@IsOptional()
test?: string | null;

Mapping configuration

No response

Steps to reproduce

const dto = new TestDto();
dto.test = "test string";

const entity =  this.mapper.map(dto, TestDto, TestEntity);

console.log(entity);

Expected behavior

The results I expected are as follows

{
  "test": "test string"
}

But in reality,

{ }

Screenshots

No response

Minimum reproduction code

No response

Package

  • [ ] I don't know.
  • [X] @automapper/core
  • [X] @automapper/classes
  • [x] @automapper/nestjs
  • [ ] @automapper/pojos
  • [ ] @automapper/mikro
  • [ ] @automapper/sequelize
  • [ ] Other (see below)

Other package and its version

No response

AutoMapper version

8.7.7

Additional context

No response

KIMBEOBWOO avatar Jun 16 '23 04:06 KIMBEOBWOO

However, if you modify the AutoMap Decorator in the Entity file as follows, it will work fine.

@AutoMap(() => String) // <-- implicit String constructor 
@Column({ type: 'text', nullable: true })
test?: string | null;

KIMBEOBWOO avatar Jun 16 '23 04:06 KIMBEOBWOO

However, if you modify the AutoMap Decorator in the Entity file as follows, it will work fine.

@AutoMap(() => String) // <-- implicit String constructor 
@Column({ type: 'text', nullable: true })
test?: string | null;

Hi, this is doesn't work for me( Only if I add @AutoMap(() => String) both to the entity and dto.

dkhrunov avatar Aug 17 '23 08:08 dkhrunov

What if the value is Enum ?

ghost avatar Mar 07 '24 13:03 ghost