#Nested querystring objects returning [object Object] string

1 messages · Page 1 of 1 (latest)

wispy moss
#

question with nestjs / class-validator:

I've got a controller method like so:

  @Get('distance')
  distance(@Query() distanceDto: DistanceDto) {
    console.log(distanceDto);

however it logs out ```
{
from: '[object Object]',
to: '[object Object]'
}

```js
import { IsNumber, IsObject, ValidateNested } from 'class-validator';

export class Point {
  @IsNumber()
  lat: any;
  @IsNumber()
  lon: any;
}

export class DistanceDto {
  @ValidateNested()
  from: Point;

  @ValidateNested()
  to: Point;
}

how do I get it to read those nested querystring objects properly?

lilac willow
#

The query is not really meant to hold structured data, as there is no single standard on how the format should look like. Express is probably not recognizing the format that you use, so you might need a custom parser. Look up the OpenAPI format deep-object and try using that in the query, IIRC that deserializes correctly out of the box.

wispy moss
#

how would I use that in the query? I'm not clear on how this would change the server code here

#

I'm using the built-in nestjs tester to do it

import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { GeolocationController } from '../src/geolocation/geolocation.controller';
import { GeolocationService } from '../src/geolocation/geolocation.service';

describe('Geolocation', () => {

  let app: NestFastifyApplication;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      controllers: [GeolocationController],
      providers: [GeolocationService],
    }).compile();

    app = moduleRef.createNestApplication<NestFastifyApplication>(
      new FastifyAdapter(),
    );

    await app.init();
    await app.getHttpAdapter().getInstance().ready();
  });

  it('GET geolocation/distance', async () => {
    return app
      .inject()
      .get('/geolocation/distance')
      .query({
        from: { lat: '...', lon: '...' },
        to: { lat: '...', lon: '...' },
      })
      .then(result => {
        expect(result.statusCode).toEqual(200)
        expect(JSON.parse(result.payload)).toEqual(56);
      });
  });

  afterAll(async () => {
    await app.close();
  });
});

#

so either I need to change the test or change the controller, but I'm not clear how

#

so I looked up openapi spec and deep-object but I've got no idea how it applies here @lilac willow

#

I tried overriding the querystring parser for the fastify adapter, here:

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({ logger: true, querystringParser: (str) => console.log(str) || parse(str) })
  );
  await app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000, '0.0.0.0');
}
bootstrap();

but that function never gets called

#

I'm totally lost how to change or configure the server behavior on these querystrings (nestjs/fastify)

#

weird that this doesn't work at all ```js
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ logger: true, querystringParser: (str) => {
console.log(str);
throw new Error('this never happens')
return qs.parse(str);
} })
);

wispy moss
#

ahah, I got it. the test created it's own FastifyAdapter, different from the one in the main app, so both needed to be updated.

lilac willow
wispy moss
#
    return app
      .inject()
      .get('/geolocation/distance')
      .query({
        from: { lat: '...', lon: '...' },
        to: { lat: '...', lon: '...' },
      })
      .then(result => {
        expect(result.statusCode).toEqual(200)
        expect(JSON.parse(result.payload)).toEqual(56);
      });
  });

using the built in inject tester thing, I'm not sure what that is exactly

lilac willow
#

Can you see what result that actually produces?

wispy moss
#

no idea, I'm not familiar with this library

#

creating the path+querystring manually worked

lilac willow
#

Okay, so I would stick with that, I'm also not too famiar with the inject behavior.