#DTO referring to another DTO

16 messages · Page 1 of 1 (latest)

sacred yoke
#

I am currently using Swagger and I want to make it so I have 2 parameters From, To, and then they would inherit Coordinate

export class Coordinate {
  @ApiProperty({ type: Number })
  lat: number;
  @ApiProperty({ type: Number })
  lon: number;
}

export class From extends Coordinate {}
export class To extends Coordinate {}

This creates lat and lon, however in the request they would appear as same. Is that possible to do what I want to do without repeating the same code for From and To? As I will be using Coordinate in other places as well.

#

I also tried using this code instead:

export class From {
  @ApiProperty({ type: Coordinate })
  from: Coordinate
}

but it just creates an object field that does not even work in url as it just specify lat and lon without the from, and overwrite each other

rotund tundra
# sacred yoke I am currently using Swagger and I want to make it so I have 2 parameters `From`...

i'm not all to sure what you are trying to do
but take a look at this https://docs.nestjs.com/openapi/mapped-types

sacred yoke
# rotund tundra i'm not all to sure what you are trying to do but take a look at this https://do...

sorry for the confusion, maybe I should have asked it in this way:

This is a working code that kind of achieve what I want

export class Plot {
  fromLat: number;
  fromLon: number;
  toLat: number;
  toLon: number;
}

However, I want it to look like this instead:

export class Coordinate {
  lat: number;
  lon: number;
}
export class Plot {
  from: Coordinate;
  to: Coordinate;
}

This code in swagger will result in the image below:

#

where in the actual query, there will only be "lat" and "lon" option, and the two lat and lon would sync as the same number in Swagger
... ?lat=123&lon=456 ...

#

I am looking for a solution that would group from and to in Swagger, where it would look something like:

From and To as key, and then lat and lon as their "sub-key"

#

I have checked the Mapped Types document you sent but it doesn't seem to have what I want unless I am misunderstanding things

rotund tundra
#

I think i start to know what you mean, to have them grouped in the swagger ui
I remember it is possible to have a dropdown, but i wouldn't know how, sorry

sacred yoke
#

If that's the case, do you know how can I make this code work:

export class From {
  from_lat: number;
  to_lat: number;
}
export class Plot {
  @ApiProperty({ type: Form })
  @Transform((value) => {
    lat: value.from_lat
    lon: value.from_lon
  })
  from: From;
}

Right now, this will just return

{
  from_lat: ...
  from_lon: ...
}

I want it to output

{
  from: {
    lat: ...
    lon: ...
  }
}
#

am I using @Transform incorrectly?

rotund tundra
sacred yoke
#

ah

#

then is there a way I can achieve this?

#

as there are two class From and To, I have to keep their value key separated hence the from_lat from_lon.
But I want the final output of Plot to be something like

{
  from: {
    lat: ...
    lon: ...
  },
  to: {
    lat: ...
    lon: ...
  }
}
rotund tundra
# sacred yoke as there are two class `From` and `To`, I have to keep their value key separated...

That would be this then as far as i would know
https://docs.nestjs.com/techniques/serialization