@@ -42,19 +42,25 @@ export const generateSwaggerPath = (path: string): { variables: string[]; path:
42
42
* @see https://swagger.io/docs/specification/data-models/
43
43
* @param {Record<string, any> } attribute Sails model attribute specification as per `Model.js` file
44
44
*/
45
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
- export const generateAttributeSchema = ( attribute : NameKeyMap < any > ) : OpenApi . UpdatedSchema => {
45
+ export const generateAttributeSchema = ( attribute : Sails . AttributeDefinition ) : OpenApi . UpdatedSchema => {
47
46
const ai = attribute || { } , sts = swaggerTypes ;
48
47
49
- const type = attribute . type || 'string' ;
50
- const columnType = get ( attribute , [ ' autoMigrations' , ' columnType' ] ) ;
51
- const autoIncrement = get ( attribute , [ ' autoMigrations' , ' autoIncrement' ] ) ;
48
+ const type = ai . type || 'string' ;
49
+ const columnType = ai . autoMigrations ?. columnType ;
50
+ const autoIncrement = ai . autoMigrations ?. autoIncrement ;
52
51
53
52
let schema : OpenApi . UpdatedSchema = { } ;
54
53
54
+ const formatDesc = ( extra : string ) : string => {
55
+ const ret : string [ ] = [ ] ;
56
+ if ( ai . description ) ret . push ( ai . description ) ;
57
+ if ( extra ) ret . push ( extra ) ;
58
+ return ret . join ( ' ' ) ;
59
+ }
60
+
55
61
if ( ai . model ) {
56
62
assign ( schema , {
57
- description : `JSON dictionary representing the **${ ai . model } ** instance or FK when creating / updating / not populated` ,
63
+ description : formatDesc ( `JSON dictionary representing the **${ ai . model } ** instance or FK when creating / updating / not populated` ) ,
58
64
// '$ref': '#/components/schemas/' + ai.model,
59
65
oneOf : [ // we use oneOf (rather than simple ref) so description rendered (!) (at least in redocly)
60
66
{ '$ref' : '#/components/schemas/' + ai . model } ,
@@ -63,14 +69,14 @@ export const generateAttributeSchema = (attribute: NameKeyMap<any>): OpenApi.Upd
63
69
64
70
} else if ( ai . collection ) {
65
71
assign ( schema , {
66
- description : `Array of **${ ai . collection } **'s or array of FK's when creating / updating / not populated` ,
72
+ description : formatDesc ( `Array of **${ ai . collection } **'s or array of FK's when creating / updating / not populated` ) ,
67
73
type : 'array' ,
68
74
items : { '$ref' : '#/components/schemas/' + ai . collection } ,
69
75
} ) ;
70
76
71
77
} else if ( type == 'number' ) {
72
78
let t = autoIncrement ? sts . integer : sts . double ;
73
- if ( ai . isInteger ) {
79
+ if ( ai . validations ?. isInteger ) {
74
80
t = sts . integer ;
75
81
} else if ( columnType ) {
76
82
const ct = columnType ;
@@ -106,6 +112,7 @@ export const generateAttributeSchema = (attribute: NameKeyMap<any>): OpenApi.Upd
106
112
if ( v . isIP ) { isIP = true ; schema . format = 'ipv4' ; }
107
113
if ( v . isURL ) schema . format = 'uri' ;
108
114
if ( v . isUUID ) schema . format = 'uuid' ;
115
+ if ( v . regex ) schema . pattern = v . regex . toString ( ) . slice ( 1 , - 1 ) ;
109
116
}
110
117
}
111
118
@@ -129,15 +136,17 @@ export const generateAttributeSchema = (attribute: NameKeyMap<any>): OpenApi.Upd
129
136
}
130
137
131
138
// process final autoMigrations: autoIncrement, unique
132
- if ( autoIncrement ) annotations . push ( 'autoIncrement' ) ;
133
- if ( get ( attribute , [ 'autoMigrations' , 'unique' ] ) ) {
139
+ if ( autoIncrement ) {
140
+ annotations . push ( 'autoIncrement' ) ;
141
+ }
142
+ if ( ai . autoMigrations ?. unique ) {
134
143
schema . uniqueItems = true ;
135
144
}
136
145
137
146
// represent Sails `isIP` as one of ipv4/ipv6
138
147
if ( schema . type == 'string' && isIP ) {
139
148
schema = {
140
- description : 'ipv4 or ipv6 address' ,
149
+ description : formatDesc ( 'ipv4 or ipv6 address' ) ,
141
150
oneOf : [
142
151
cloneDeep ( schema ) ,
143
152
assign ( cloneDeep ( schema ) , { format : 'ipv6' } ) ,
@@ -147,9 +156,11 @@ export const generateAttributeSchema = (attribute: NameKeyMap<any>): OpenApi.Upd
147
156
148
157
if ( annotations . length > 0 ) {
149
158
const s = `Note Sails special attributes: ${ annotations . join ( ', ' ) } ` ;
150
- schema . description = schema . description ? `\n\n${ s } ` : s ;
159
+ schema . description = schema . description ? `${ schema . description } \n\n${ s } ` : s ;
151
160
}
152
161
162
+ if ( schema . description ) schema . description = schema . description . trim ( ) ;
163
+
153
164
// note: required --> required[] (not here, needs to be done at model level)
154
165
155
166
return schema ;
0 commit comments