@penkov/swagger-code-gen 1.6.5 → 1.6.7

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 {
@@ -55,9 +57,6 @@ export class Method {
55
57
  }
56
58
  });
57
59
  });
58
- this.body.foreach(b => {
59
- console.log(path + ' => body: ' + b.schemaType + ' ' + b.required);
60
- });
61
60
  this.bodyDescription = option(def.requestBody).flatMap(body => option(body.description));
62
61
  const statusCodes = Collection.from(Object.keys(def.responses))
63
62
  .map(x => parseInt(x));
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))
package/dist/schemas.js CHANGED
@@ -20,19 +20,39 @@ export class SchemaFactory {
20
20
  return SchemaEnum.fromDefinition(name, def);
21
21
  }
22
22
  else if (def.type === 'string') {
23
- return Property.fromDefinition(name, def, schemasTypes, options);
23
+ return Property.fromDefinition(name, {
24
+ ...def,
25
+ required: option(def.required).filter(x => typeof x === 'boolean')
26
+ .map(x => x).orUndefined
27
+ }, schemasTypes, options);
24
28
  }
25
29
  else if (def.type === 'boolean') {
26
- return Property.fromDefinition(name, def, schemasTypes, options);
30
+ return Property.fromDefinition(name, {
31
+ ...def,
32
+ required: option(def.required).filter(x => typeof x === 'boolean')
33
+ .map(x => x).orUndefined
34
+ }, schemasTypes, options);
27
35
  }
28
36
  else if (def.type === 'integer') {
29
- return Property.fromDefinition(name, def, schemasTypes, options);
37
+ return Property.fromDefinition(name, {
38
+ ...def,
39
+ required: option(def.required).filter(x => typeof x === 'boolean')
40
+ .map(x => x).orUndefined
41
+ }, schemasTypes, options);
30
42
  }
31
43
  else if (def.type === 'array') {
32
- return Property.fromDefinition(name, def, schemasTypes, options);
44
+ return Property.fromDefinition(name, {
45
+ ...def,
46
+ required: option(def.required).filter(x => typeof x === 'boolean')
47
+ .map(x => x).orUndefined
48
+ }, schemasTypes, options);
33
49
  }
34
50
  else {
35
- return Property.fromDefinition(name, def, schemasTypes, options);
51
+ return Property.fromDefinition(name, {
52
+ ...def,
53
+ required: option(def.required).filter(x => typeof x === 'boolean')
54
+ .map(x => x).orUndefined
55
+ }, schemasTypes, options);
36
56
  }
37
57
  }
38
58
  }
@@ -58,7 +78,14 @@ export class SchemaObject {
58
78
  this.schemaType = 'object';
59
79
  }
60
80
  static fromDefinition(name, def, schemasTypes, options) {
61
- const properties = Collection.from(Object.keys(def.properties)).map(p => Property.fromDefinition(p, def.properties[p], schemasTypes, options));
81
+ const explicitlyRequired = option(def.required)
82
+ .map(arr => typeof arr === 'boolean' ? Nil : Collection.from(arr));
83
+ const properties = Collection.from(Object.keys(def.properties)).map(propName => {
84
+ const property = Property.fromDefinition(propName, def.properties[propName], schemasTypes, options);
85
+ return property.copy({
86
+ required: explicitlyRequired.exists(c => c.contains(propName)) ? true : property.required
87
+ });
88
+ });
62
89
  return new SchemaObject(name, def.title, def.type, properties);
63
90
  }
64
91
  }
@@ -39,6 +39,8 @@ 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,
44
46
  <%_ } else if (!body.required) { -%>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@penkov/swagger-code-gen",
3
- "version": "1.6.5",
3
+ "version": "1.6.7",
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 http://localhost:3000/-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
  }