@penkov/swagger-code-gen 1.7.11 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/method.js +2 -1
- package/dist/name.utils.js +28 -0
- package/dist/property.js +15 -7
- package/dist/schemas.js +6 -2
- package/dist/templates/scats-schema.ejs +7 -7
- package/dist/templates/schema.ejs +2 -2
- package/package.json +1 -1
package/dist/method.js
CHANGED
|
@@ -2,6 +2,7 @@ import { Collection, HashMap, HashSet, identity, Nil, option } from 'scats';
|
|
|
2
2
|
import { Property } from './property.js';
|
|
3
3
|
import { Parameter } from './parameter.js';
|
|
4
4
|
import { SchemaFactory } from './schemas.js';
|
|
5
|
+
import { NameUtils } from './name.utils.js';
|
|
5
6
|
const sortByIn = HashMap.of(['path', 0], ['query', 1], ['header', 2], ['cookie', 3], ['body', 4]);
|
|
6
7
|
const supportedBodyMimeTypes = HashMap.of(['application/json', 'Json'], ['application/x-www-form-urlencoded', 'Form'], ['multipart/form-data', 'File'], ['application/octet-stream', 'Binary']);
|
|
7
8
|
export class Method {
|
|
@@ -89,7 +90,7 @@ export class Method {
|
|
|
89
90
|
this.wrapParamsInObject = this.parameters.size > 2 || (this.body.nonEmpty) && this.parameters.nonEmpty;
|
|
90
91
|
}
|
|
91
92
|
get endpointName() {
|
|
92
|
-
return this.operationId.getOrElse(() => `${this.method}${Method.pathToName(this.path)}`);
|
|
93
|
+
return NameUtils.normaliseMethodName(this.operationId.getOrElse(() => `${this.method}${Method.pathToName(this.path)}`));
|
|
93
94
|
}
|
|
94
95
|
get pathWithSubstitutions() {
|
|
95
96
|
const paramPrefix = `${this.wrapParamsInObject ? 'params.' : ''}`;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export class NameUtils {
|
|
2
|
+
static normaliseClassname(n) {
|
|
3
|
+
let res = '';
|
|
4
|
+
let needUpperCase = true;
|
|
5
|
+
for (let i = 0; i < n.length; i++) {
|
|
6
|
+
const c = n[i];
|
|
7
|
+
if (c === '.' || c === '-') {
|
|
8
|
+
needUpperCase = true;
|
|
9
|
+
}
|
|
10
|
+
else if (needUpperCase) {
|
|
11
|
+
res += c.toUpperCase();
|
|
12
|
+
needUpperCase = false;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
res += c;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return res;
|
|
19
|
+
}
|
|
20
|
+
static normaliseMethodName(n) {
|
|
21
|
+
if (n === 'delete') {
|
|
22
|
+
return `$${n}`;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
return n;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
package/dist/property.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Collection, none, option } from 'scats';
|
|
2
|
+
import { NameUtils } from './name.utils.js';
|
|
2
3
|
const SCHEMA_PREFIX = '#/components/schemas/';
|
|
3
4
|
export class Property {
|
|
4
5
|
constructor(name, type, format, description, defaultValue, nullable, required, items, referencesObject, itemReferencesObject, enumValues) {
|
|
@@ -18,12 +19,12 @@ export class Property {
|
|
|
18
19
|
copy(p) {
|
|
19
20
|
return new Property(option(p.name).getOrElseValue(this.name), option(p.type).getOrElseValue(this.type), option(p.format).getOrElseValue(this.format), option(p.description).getOrElseValue(this.description), option(p.defaultValue).getOrElseValue(this.defaultValue), option(p.nullable).getOrElseValue(this.nullable), option(p.required).getOrElseValue(this.required), option(p.items).getOrElseValue(this.items), option(p.referencesObject).getOrElseValue(this.referencesObject), option(p.itemReferencesObject).getOrElseValue(this.itemReferencesObject), option(p.enumValues).getOrElseValue(this.enumValues));
|
|
20
21
|
}
|
|
21
|
-
static fromDefinition(name, definition,
|
|
22
|
+
static fromDefinition(name, definition, schemaTypes, options) {
|
|
22
23
|
const referencesObject = option(definition.$ref)
|
|
23
|
-
.exists(ref =>
|
|
24
|
+
.exists(ref => schemaTypes.get(ref.substring(SCHEMA_PREFIX.length)).contains('object'));
|
|
24
25
|
const itemReferencesObject = option(definition.items)
|
|
25
26
|
.flatMap(i => option(i.$ref))
|
|
26
|
-
.exists(ref =>
|
|
27
|
+
.exists(ref => schemaTypes.get(ref.substring(SCHEMA_PREFIX.length)).contains('object'));
|
|
27
28
|
const type = option(definition.$ref)
|
|
28
29
|
.map(ref => ref.substring(SCHEMA_PREFIX.length))
|
|
29
30
|
.orElse(() => option(definition.allOf)
|
|
@@ -96,6 +97,7 @@ export class Property {
|
|
|
96
97
|
switch (tpe) {
|
|
97
98
|
case 'integer': return 'number';
|
|
98
99
|
case 'file': return 'File';
|
|
100
|
+
case 'any': return 'any';
|
|
99
101
|
case 'string':
|
|
100
102
|
if (format.contains('binary')) {
|
|
101
103
|
return 'Blob | Buffer';
|
|
@@ -104,13 +106,17 @@ export class Property {
|
|
|
104
106
|
return 'string';
|
|
105
107
|
}
|
|
106
108
|
case 'array': return `ReadonlyArray<${Property.toJsType(itemTpe)}>`;
|
|
107
|
-
default: return tpe;
|
|
109
|
+
default: return NameUtils.normaliseClassname(tpe);
|
|
108
110
|
}
|
|
109
111
|
}
|
|
112
|
+
get normalType() {
|
|
113
|
+
return NameUtils.normaliseClassname(this.type);
|
|
114
|
+
}
|
|
110
115
|
get itemScatsWrapperType() {
|
|
111
116
|
if (this.isArray) {
|
|
112
117
|
if (this.itemReferencesObject) {
|
|
113
|
-
|
|
118
|
+
const cls = NameUtils.normaliseClassname(this.items);
|
|
119
|
+
return `${cls}Dto`;
|
|
114
120
|
}
|
|
115
121
|
else {
|
|
116
122
|
return Property.toJsType(this.items);
|
|
@@ -122,11 +128,13 @@ export class Property {
|
|
|
122
128
|
}
|
|
123
129
|
get scatsWrapperType() {
|
|
124
130
|
if (this.referencesObject) {
|
|
125
|
-
|
|
131
|
+
const cls = NameUtils.normaliseClassname(this.type);
|
|
132
|
+
return !this.nullable && this.required ? `${cls}Dto` : `Option<${cls}Dto>`;
|
|
126
133
|
}
|
|
127
134
|
else if (this.isArray) {
|
|
128
135
|
if (this.itemReferencesObject) {
|
|
129
|
-
|
|
136
|
+
const cls = NameUtils.normaliseClassname(this.items);
|
|
137
|
+
return `Collection<${cls}Dto>`;
|
|
130
138
|
}
|
|
131
139
|
else {
|
|
132
140
|
return `Collection<${Property.toJsType(this.items)}>`;
|
package/dist/schemas.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Collection, Nil, option } from 'scats';
|
|
2
2
|
import { Property } from './property.js';
|
|
3
|
+
import { NameUtils } from './name.utils.js';
|
|
3
4
|
export class SchemaFactory {
|
|
4
5
|
static resolveSchemaType(def) {
|
|
5
|
-
if (def.type === 'object') {
|
|
6
|
+
if (def.type === 'object' || option(def.properties).exists(p => Object.keys(p).length > 0)) {
|
|
6
7
|
return 'object';
|
|
7
8
|
}
|
|
8
9
|
else if (def.enum) {
|
|
@@ -13,7 +14,7 @@ export class SchemaFactory {
|
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
static build(name, def, schemasTypes, options) {
|
|
16
|
-
if (def.type === 'object') {
|
|
17
|
+
if (def.type === 'object' || option(def.properties).exists(p => Object.keys(p).length > 0)) {
|
|
17
18
|
return SchemaObject.fromDefinition(name, def, schemasTypes, options);
|
|
18
19
|
}
|
|
19
20
|
else if (def.enum) {
|
|
@@ -88,4 +89,7 @@ export class SchemaObject {
|
|
|
88
89
|
});
|
|
89
90
|
return new SchemaObject(name, def.title, def.type, properties);
|
|
90
91
|
}
|
|
92
|
+
get normalName() {
|
|
93
|
+
return NameUtils.normaliseClassname(this.name);
|
|
94
|
+
}
|
|
91
95
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<%_ if (schema.schemaType === 'object') { -%>
|
|
2
|
-
export class <%= schema.
|
|
2
|
+
export class <%= schema.normalName %>Dto {
|
|
3
3
|
|
|
4
4
|
constructor(
|
|
5
5
|
<%_ schema.properties.foreach(p => { -%>
|
|
@@ -8,14 +8,14 @@ export class <%= schema.name %>Dto {
|
|
|
8
8
|
) {}
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
static fromJson(json: <%= schema.
|
|
12
|
-
return new <%= schema.
|
|
11
|
+
static fromJson(json: <%= schema.normalName %>): <%= schema.normalName %>Dto {
|
|
12
|
+
return new <%= schema.normalName %>Dto(
|
|
13
13
|
<%_ schema.properties.foreach(p => { _%>
|
|
14
14
|
<%_ if (p.referencesObject) { _%>
|
|
15
15
|
<%_ if (p.required && !p.nullable) { _%>
|
|
16
16
|
<%- p.scatsWrapperType %>.fromJson(json.<%= p.name %>),
|
|
17
17
|
<%_ } else { _%>
|
|
18
|
-
option(json.<%= p.name %>).map(_ => <%- p.
|
|
18
|
+
option(json.<%= p.name %>).map(_ => <%- p.normalType %>Dto.fromJson(_)),
|
|
19
19
|
<%_ } _%>
|
|
20
20
|
<%_ } else if (p.isArray) { _%>
|
|
21
21
|
Collection.from(option(json.<%= p.name %>).getOrElseValue([]))
|
|
@@ -29,15 +29,15 @@ export class <%= schema.name %>Dto {
|
|
|
29
29
|
);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
copy(fields: Partial<<%= schema.
|
|
33
|
-
return new <%= schema.
|
|
32
|
+
copy(fields: Partial<<%= schema.normalName %>Dto>): <%= schema.normalName %>Dto {
|
|
33
|
+
return new <%= schema.normalName %>Dto(
|
|
34
34
|
<%_ schema.properties.foreach(p => { -%>
|
|
35
35
|
option(fields.<%= p.name %>).getOrElseValue(this.<%= p.name %>),
|
|
36
36
|
<%_ }); -%>
|
|
37
37
|
);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
get toJson(): <%= schema.
|
|
40
|
+
get toJson(): <%= schema.normalName %> {
|
|
41
41
|
return {
|
|
42
42
|
<%_ schema.properties.foreach(p => { _%>
|
|
43
43
|
<%_ if (p.referencesObject) { _%>
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
<%_ if (schema.schemaType === 'object') { -%>
|
|
2
|
-
export interface <%= schema.
|
|
2
|
+
export interface <%= schema.normalName %> {
|
|
3
3
|
<%_ schema.properties.foreach(p => { -%>
|
|
4
4
|
readonly <%= p.name %><%= !p.required ? '?' : '' %>: <%- p.jsType %>;
|
|
5
5
|
<%_ }); -%>
|
|
6
6
|
}
|
|
7
7
|
<%_ } if (schema.schemaType === 'enum') { -%>
|
|
8
|
-
export enum <%= schema.
|
|
8
|
+
export enum <%= schema.normalName %> {
|
|
9
9
|
<%_ schema.values.foreach(p => { -%>
|
|
10
10
|
<%= p %> = '<%= p %>',
|
|
11
11
|
<%_ }); -%>
|