@penkov/swagger-code-gen 1.7.10 → 1.8.0
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 +9 -1
- package/dist/schemas.js +5 -1
- package/dist/templates/scats-schema.ejs +6 -6
- 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) {
|
|
@@ -32,6 +33,12 @@ export class Property {
|
|
|
32
33
|
.map(x => x.flatMapOption(oneOfItem => option(oneOfItem.$ref)
|
|
33
34
|
.map(ref => ref.substring(SCHEMA_PREFIX.length))
|
|
34
35
|
.orElseValue(option(oneOfItem.type))).mkString(' & ')))
|
|
36
|
+
.orElse(() => option(definition.oneOf)
|
|
37
|
+
.map(x => Collection.from(x))
|
|
38
|
+
.filter(x => x.nonEmpty)
|
|
39
|
+
.map(x => x.flatMapOption(oneOfItem => option(oneOfItem.$ref)
|
|
40
|
+
.map(ref => ref.substring(SCHEMA_PREFIX.length))
|
|
41
|
+
.orElseValue(option(oneOfItem.type))).mkString(' | ')))
|
|
35
42
|
.orElse(() => option(definition.anyOf)
|
|
36
43
|
.map(x => Collection.from(x))
|
|
37
44
|
.filter(x => x.nonEmpty)
|
|
@@ -90,6 +97,7 @@ export class Property {
|
|
|
90
97
|
switch (tpe) {
|
|
91
98
|
case 'integer': return 'number';
|
|
92
99
|
case 'file': return 'File';
|
|
100
|
+
case 'any': return 'any';
|
|
93
101
|
case 'string':
|
|
94
102
|
if (format.contains('binary')) {
|
|
95
103
|
return 'Blob | Buffer';
|
|
@@ -98,7 +106,7 @@ export class Property {
|
|
|
98
106
|
return 'string';
|
|
99
107
|
}
|
|
100
108
|
case 'array': return `ReadonlyArray<${Property.toJsType(itemTpe)}>`;
|
|
101
|
-
default: return tpe;
|
|
109
|
+
default: return NameUtils.normaliseClassname(tpe);
|
|
102
110
|
}
|
|
103
111
|
}
|
|
104
112
|
get itemScatsWrapperType() {
|
package/dist/schemas.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
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
6
|
if (def.type === 'object') {
|
|
@@ -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,8 +8,8 @@ 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) { _%>
|
|
@@ -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
|
<%_ }); -%>
|