@penkov/swagger-code-gen 1.6.4 → 1.6.6

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 CHANGED
@@ -37,6 +37,7 @@ export class Method {
37
37
  });
38
38
  this.body = option(def.requestBody).flatMap(body => option(body.content))
39
39
  .flatMap(body => {
40
+ const bodyRequired = option(def.requestBody.required).contains(true);
40
41
  const mimeTypes = Collection.from(Object.keys(body));
41
42
  return mimeTypes
42
43
  .find(_ => _ === 'application/json')
@@ -47,7 +48,8 @@ export class Method {
47
48
  if (res.schemaType === 'property') {
48
49
  const bProperty = res;
49
50
  return bProperty.copy({
50
- nullable: bProperty.referencesObject ? option(bodySchemaDef['nullable']).contains(true) : bProperty.nullable
51
+ nullable: bProperty.referencesObject ? option(bodySchemaDef['nullable']).contains(true) : bProperty.nullable,
52
+ required: bodyRequired
51
53
  });
52
54
  }
53
55
  else {
@@ -69,7 +71,8 @@ export class Method {
69
71
  this.response = mimeTypes.get('application/json')
70
72
  .orElseValue(mimeTypes.values.headOption)
71
73
  .map(p => Property.fromDefinition('', p.schema, schemasTypes, options).copy({
72
- nullable: false
74
+ nullable: false,
75
+ required: true
73
76
  }))
74
77
  .map(r => ({
75
78
  asProperty: r,
package/dist/property.js CHANGED
@@ -34,7 +34,7 @@ export class Property {
34
34
  const nullable = option(definition.nullable).contains(true) ||
35
35
  (referencesObject && options.referencedObjectsNullableByDefault && !option(definition.nullable).contains(false));
36
36
  const description = option(definition.description);
37
- const required = option(definition.required).contains(false);
37
+ const required = option(definition.required).contains(true);
38
38
  const items = option(definition.items?.$ref)
39
39
  .map(ref => ref.substring(SCHEMA_PREFIX.length))
40
40
  .orElseValue(option(definition.items?.type))
@@ -48,7 +48,11 @@ export class Property {
48
48
  return new Property(name, type, description, null, nullable, required, items, referencesObject, itemReferencesObject);
49
49
  }
50
50
  get jsType() {
51
- return Property.toJsType(this.type, this.items);
51
+ let res = Property.toJsType(this.type, this.items);
52
+ if (this.nullable) {
53
+ res = res + '| null';
54
+ }
55
+ return res;
52
56
  }
53
57
  get isArray() {
54
58
  return this.type === 'array';
@@ -75,7 +79,7 @@ export class Property {
75
79
  }
76
80
  get scatsWrapperType() {
77
81
  if (this.referencesObject) {
78
- return this.nullable ? `Option<${this.type}Dto>` : `${this.type}Dto`;
82
+ return !this.nullable && this.required ? `${this.type}Dto` : `Option<${this.type}Dto>`;
79
83
  }
80
84
  else if (this.isArray) {
81
85
  if (this.itemReferencesObject) {
@@ -86,7 +90,7 @@ export class Property {
86
90
  }
87
91
  }
88
92
  else {
89
- return this.nullable ? `Option<${this.jsType}>` : this.jsType;
93
+ return !this.nullable && this.required ? this.jsType : `Option<${this.jsType}>`;
90
94
  }
91
95
  }
92
96
  }
@@ -31,7 +31,7 @@ export async function <%= method.endpointName %>(
31
31
  },
32
32
  <%_ } -%>
33
33
  <%_ if (method.body.nonEmpty) { -%>
34
- body: <%- method.body.get.jsType %>,
34
+ body<%= method.body.get.required ? '' : '?'%>: <%- method.body.get.jsType %>,
35
35
  <%_ } -%>
36
36
  requestOptions: RequestOptions = defReqOpts()
37
37
  ): Promise<<%- method.response.responseType %>> {
@@ -39,8 +39,12 @@ export class ApiClient {
39
39
  body.map(_ => _.toJson).toArray,
40
40
  <%_ } else if (body.isArray && !body.itemReferencesObject) { -%>
41
41
  body.toArray,
42
+ <%_ } else if (body.referencesObject && !body.required) { -%>
43
+ body.map(b => b.toJson).orUndefined,
42
44
  <%_ } else if (body.referencesObject) { -%>
43
45
  body.toJson,
46
+ <%_ } else if (!body.required) { -%>
47
+ body.orUndefined,
44
48
  <%_ } else { -%>
45
49
  body,
46
50
  <%_ } -%>
@@ -12,7 +12,7 @@ export class <%= schema.name %>Dto {
12
12
  return new <%= schema.name %>Dto(
13
13
  <%_ schema.properties.foreach(p => { _%>
14
14
  <%_ if (p.referencesObject) { _%>
15
- <%_ if (p.required || !p.nullable) { _%>
15
+ <%_ if (p.required && !p.nullable) { _%>
16
16
  <%- p.scatsWrapperType %>.fromJson(json.<%= p.name %>),
17
17
  <%_ } else { _%>
18
18
  option(json.<%= p.name %>).map(_ => <%- p.type %>Dto.fromJson(_)),
@@ -20,7 +20,7 @@ export class <%= schema.name %>Dto {
20
20
  <%_ } else if (p.isArray) { _%>
21
21
  Collection.from(option(json.<%= p.name %>).getOrElseValue([]))
22
22
  <% if (p.itemReferencesObject) { _%>.map(i => <%- p.itemScatsWrapperType %>.fromJson(i))<%_ } _%>,
23
- <%_ } else if (p.required || !p.nullable) { _%>
23
+ <%_ } else if (p.required && !p.nullable) { _%>
24
24
  json.<%= p.name %>,
25
25
  <%_ } else { _%>
26
26
  option(json.<%= p.name %>),
@@ -41,7 +41,7 @@ export class <%= schema.name %>Dto {
41
41
  return {
42
42
  <%_ schema.properties.foreach(p => { _%>
43
43
  <%_ if (p.referencesObject) { _%>
44
- <%_ if (p.required || !p.nullable) { _%>
44
+ <%_ if (p.required && !p.nullable) { _%>
45
45
  <%= p.name %>: this.<%= p.name %>.toJson,
46
46
  <%_ } else { _%>
47
47
  <%= p.name %>: this.<%= p.name %>.map(_ => _.toJson).orUndefined,
@@ -50,7 +50,7 @@ export class <%= schema.name %>Dto {
50
50
  <%= p.name %>: this.<%= p.name %>
51
51
  <% if (p.itemReferencesObject) { _%>.map(_ => _.toJson)<%_ } _%>
52
52
  .toArray,
53
- <%_ } else if (p.required || !p.nullable) { _%>
53
+ <%_ } else if (p.required && !p.nullable) { _%>
54
54
  <%= p.name %>: this.<%= p.name %>,
55
55
  <%_ } else { _%>
56
56
  <%= p.name %>: this.<%= p.name %>.orUndefined,
@@ -1,7 +1,7 @@
1
1
  <%_ if (schema.schemaType === 'object') { -%>
2
2
  export interface <%= schema.name %> {
3
3
  <%_ schema.properties.foreach(p => { -%>
4
- readonly <%= p.name %><%= p.nullable ? '?' : '' %>: <%- p.jsType %>;
4
+ readonly <%= p.name %><%= !p.required ? '?' : '' %>: <%- p.jsType %>;
5
5
  <%_ }); -%>
6
6
  }
7
7
  <%_ } if (schema.schemaType === 'enum') { -%>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@penkov/swagger-code-gen",
3
- "version": "1.6.4",
3
+ "version": "1.6.6",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "generate-client": "./dist/cli.mjs"
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "homepage": "https://github.com/papirosko/swagger-code-gen#readme",
24
24
  "scripts": {
25
- "test:petstore": "npm run build && mkdir -p tmp && ts-node-esm ./dist/cli.mjs --enableScats --targetNode --url https://petstore3.swagger.io/api/v3/openapi.json tmp/petstore.ts",
25
+ "test:petstore": "node --loader ts-node/esm ./src/cli.mjs --enableScats --targetNode --url https://petstore3.swagger.io/api/v3/openapi.json tmp/petstore.ts",
26
26
  "clean": "rimraf dist",
27
27
  "lint": "eslint \"{src,test}/**/*.ts\" --fix",
28
28
  "prebuild": "npm run lint && npm run clean",
@@ -43,7 +43,7 @@
43
43
  "@typescript-eslint/parser": "^5.10.2",
44
44
  "eslint": "^7.30.0",
45
45
  "rimraf": "^3.0.2",
46
- "ts-node": "^10.9.1",
46
+ "ts-node": "^10.9.2",
47
47
  "typescript": "^4.9.3"
48
48
  }
49
49
  }