Skip to content

Support for deepObject in api property #3495

@karol71927

Description

@karol71927

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Currently when I do something like this and use it as query param

class Geolocation {
  @ApiProperty()
  @Type(() => Number)
  @IsLatitude()
  latitude: number;

  @ApiProperty()
  @Type(() => Number)
  @IsLongitude()
  longitude: number;

  @ApiProperty({ description: 'Distance in kilometers' })
  @Type(() => Number)
  distance: number;
}

class Query {
  @ApiProperty({
    required: false,
    type: () => Geolocation,
  })
  @IsOptional()
  @Type(() => Geolocation)
  @ValidateNested()
  geolocation?: Geolocation;
}

Swagger will generate url like this
http://localhost:3000/api?latitude=5&longitude=1&distance=10
instead of
http://localhost:3000/api?geolocation[latitude]=5&geolocation[longitude]=1&geolocation[distance]=10

Describe the solution you'd like

solution will be adding to @ApiProperty decorator options style and explode. There are values for style property https://swagger.io/docs/specification/v3_0/serialization/#query-parameters

workaround looks like this

// api-nested-property.decorator.ts
import { ApiProperty, ApiPropertyOptions } from '@nestjs/swagger';

export function ApiQueryNestedProperty(
  options: ApiPropertyOptions = {},
): PropertyDecorator {
  return ApiProperty({
    ...options,
    // @ts-expect-error it is workaround for bugged nestjs decorator
    style: 'deepObject',
    explode: true,
  });
}

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Incorrect mapping of url

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions