I was wondering if there's a way to support complex objects for Nestjs/swagger. I just finished the migration and I am now working on the swagger documentation. A lot of my requests return complex objects and I'm wondering if there's an easier way. Example:
class Foobar{
prop1: {
subprop1: {
subsub1: string;
};
};
}
Becomes:
class SubSub{
@ApiModelProperty()
subsub1: string;
}
class SubProp{
@ApiModelProperty()
subporp1: SubSub;
}
class Foobar {
@ApiModelProperty()
prop1: SubProp;
}
If I do this:
class Foobar{
@ApiModelProperty()
prop1: {
subprop1: {
subsub1: string;
};
};
}
I get this in swagger:
{
"prop1": {}
}
Nestjs, not like any other nodejs frameworks, has many handy tools, libraries and features that let us write our programs following clean code and scalable architecture. In this article we’ll find out how to use swagger for documentation of our application, and also know the best practices for module creation.
So I changed the things you mentioned, but Swagger doesn't seem to be able to serialise the parameters into a query string. The resulting URL is query?size=10&start=0&filters=%5Bobject%20Object%5D
“providers” is an array of classes that are used in our module. On the compiling stage nestjs will create objects automatically with resolving constructor dependencies. “exports” - in this section you can specify providers classes that can be used in providers other modules.
UPDATE 04/2020: ApiModelProperty
now has been changed to ApiProperty
class SubSub{
@ApiProperty()
subsub1: string;
}
class SubProp{
@ApiProperty({ type: SubSub })
subporp1: SubSub;
}
class Foobar {
@ApiProperty({ type: () => SubProp })
prop1: SubProp;
}
In the last ApiProperty
, I used "Lazy Evaluated Function" syntax. This is to prevent Circular Dependency problem. Thought I'd add it in there.
class SubSub{
@ApiModelProperty()
subsub1: string;
}
class SubProp{
@ApiModelProperty({ type: SubSub })
subporp1: SubSub;
}
class Foobar {
@ApiModelProperty({ type: SubProp })
prop1: SubProp;
}
The @ApiModelProperty
takes in an option object where you can specify the type
if it's a complex object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With