Skip to content

Commit 7904f78

Browse files
danielsharveytheoomoregbee
authored andcommitted
feat: Add support for 'Any Type' model attributes.
OpenAPI 3 specifies the ***Any Type*** by the absence of the `type` property in a schema; this may be achieved by setting a model attribute's `meta.swagger.type` value to `null`.
1 parent 1a6653b commit 7904f78

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

README.MD

+4
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ blueprint action from the generated Swagger. See example in [OldPet.js](api/mode
319319
Individual model attributes may be excluded from the generated Swagger by setting
320320
`meta.swagger.exclude` to `true`. See example in [Pet.js](api/models/Pet.js).
321321

322+
OpenAPI 3 specifies the ***Any Type*** by the absence of the `type` property in a schema;
323+
this may be achieved by setting a model attribute's `meta.swagger.type` value to `null`.
324+
See example in [User.js](api/models/User.js).
325+
322326
The Swagger definition for each action is merged in the order above to form the final
323327
definition, with `config/routes.js` taking highest precendence and **earlier** definitions
324328
above taking precedence over later.

lib/generators.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export const generateAttributeSchema = (attribute: Sails.AttributeDefinition): O
3939
return ret.join(' ');
4040
}
4141

42-
if (ai.model) {
42+
if (ai.meta?.swagger && 'type' in ai.meta.swagger) {
43+
// OpenAPI 3 stipulates NO type as 'any', allow this by 'type' present but null to achieve this
44+
if(ai.meta.swagger.type) schema.type = ai.meta.swagger.type;
45+
} else if (ai.model) {
4346
assign(schema, {
4447
description: formatDesc(`JSON dictionary representing the **${ai.model}** instance or FK when creating / updating / not populated`),
4548
// '$ref': '#/components/schemas/' + ai.model,
@@ -146,7 +149,8 @@ export const generateAttributeSchema = (attribute: Sails.AttributeDefinition): O
146149

147150
// finally, overwrite in custom swagger
148151
if(ai.meta?.swagger) {
149-
assign(schema, omit(ai.meta.swagger, 'exclude'));
152+
// note: 'type' handled above
153+
assign(schema, omit(ai.meta.swagger, 'exclude', 'type'));
150154
}
151155

152156
return schema;

0 commit comments

Comments
 (0)