Skip to content

Commit da4e144

Browse files
committed
fix: additionalProperties problem
1 parent 6543f0b commit da4e144

File tree

10 files changed

+153
-6
lines changed

10 files changed

+153
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# next release
22

3-
fix: problems with missing type imports in `.d.ts` files with using option `--js`
3+
fix: problems with missing type imports in `.d.ts` files with using option `--js`
4+
internal: add extra spec tests
5+
fix: additionalProperties management problem in Swagger 2 (#343)
46

57
# 11.1.1
68
fix: `--api-class-name` option has no default value (#433)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"test:deprecated": "node tests/spec/deprecated/test.js",
5656
"test:nullableRefTest3.0": "node tests/spec/nullable-3.0/test.js",
5757
"test:nullableRefTest2.0": "node tests/spec/nullable-2.0/test.js",
58+
"test:additionalProperties2.0": "node tests/spec/additional-properties-2.0/test.js",
5859
"test:enums2.0": "node tests/spec/enums-2.0/test.js"
5960
},
6061
"author": "acacode",

src/schema-parser/schema-parser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class SchemaParser {
149149
});
150150
},
151151
[SCHEMA_TYPES.OBJECT]: (schema, typeName) => {
152-
const content = this.getObjectSchemaContent(schema);
152+
const contentProperties = this.getObjectSchemaContent(schema);
153153

154154
return this.attachParsedRef(schema, {
155155
...(_.isObject(schema) ? schema : {}),
@@ -159,8 +159,8 @@ class SchemaParser {
159159
typeIdentifier: this.config.Ts.Keyword.Interface,
160160
name: typeName,
161161
description: this.schemaFormatters.formatDescription(schema.description),
162-
allFieldsAreOptional: !_.some(_.values(content), (part) => part.isRequired),
163-
content: content,
162+
allFieldsAreOptional: !_.some(_.values(contentProperties), (part) => part.isRequired),
163+
content: contentProperties,
164164
});
165165
},
166166
[SCHEMA_TYPES.COMPLEX]: (schema, typeName) => {
@@ -424,7 +424,7 @@ class SchemaParser {
424424
};
425425
});
426426

427-
if (additionalProperties === true) {
427+
if (additionalProperties) {
428428
propertiesContent.push({
429429
$$raw: { additionalProperties },
430430
description: "",

tests/generated/v3.0/full-swagger-scheme.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,7 @@ export interface Integration {
31643164
metadata?: string;
31653165
contents?: string;
31663166
deployments?: string;
3167+
[key: string]: any;
31673168
};
31683169
/**
31693170
* The slug name of the GitHub app
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable */
2+
/* tslint:disable */
3+
/*
4+
* ---------------------------------------------------------------
5+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
6+
* ## ##
7+
* ## AUTHOR: acacode ##
8+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
9+
* ---------------------------------------------------------------
10+
*/
11+
12+
export interface MyObject1 {
13+
id?: string;
14+
[key: string]: any;
15+
}
16+
17+
export interface MyObject2 {
18+
id?: string;
19+
[key: string]: any;
20+
}
21+
22+
export interface MyObject3 {
23+
id?: string;
24+
[key: string]: any;
25+
}
26+
27+
export type MyObject4 = Record<
28+
string,
29+
{
30+
content?: string;
31+
filename?: string | null;
32+
}
33+
>;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"swagger": "2.0",
3+
"schemes": ["https"],
4+
"host": "ffff.com",
5+
"basePath": "/",
6+
"info": {},
7+
"definitions": {
8+
"MyObject1": {
9+
"type": "object",
10+
"properties": {
11+
"id": {
12+
"type": "string"
13+
}
14+
},
15+
"additionalProperties": {
16+
"type": "object"
17+
}
18+
},
19+
"MyObject2": {
20+
"type": "object",
21+
"properties": {
22+
"id": {
23+
"type": "string"
24+
}
25+
},
26+
"additionalProperties": {}
27+
},
28+
"MyObject3": {
29+
"type": "object",
30+
"properties": {
31+
"id": {
32+
"type": "string"
33+
}
34+
},
35+
"additionalProperties": true
36+
},
37+
"MyObject4": {
38+
"type": "object",
39+
"additionalProperties": {
40+
"type": "object",
41+
"nullable": true,
42+
"properties": {
43+
"content": {
44+
"type": "string"
45+
},
46+
"filename": {
47+
"type": "string",
48+
"nullable": true
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable */
2+
/* tslint:disable */
3+
/*
4+
* ---------------------------------------------------------------
5+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
6+
* ## ##
7+
* ## AUTHOR: acacode ##
8+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
9+
* ---------------------------------------------------------------
10+
*/
11+
12+
export interface MyObject1 {
13+
id?: string;
14+
[key: string]: any;
15+
}
16+
17+
export interface MyObject2 {
18+
id?: string;
19+
[key: string]: any;
20+
}
21+
22+
export interface MyObject3 {
23+
id?: string;
24+
[key: string]: any;
25+
}
26+
27+
export type MyObject4 = Record<
28+
string,
29+
{
30+
content?: string;
31+
filename?: string | null;
32+
}
33+
>;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { generateApiForTest } = require("../../helpers/generateApiForTest");
2+
const { resolve } = require("path");
3+
const validateGeneratedModule = require("../../helpers/validateGeneratedModule");
4+
const assertGeneratedModule = require("../../helpers/assertGeneratedModule");
5+
const createSchemaInfos = require("../../helpers/createSchemaInfos");
6+
7+
const schemas = createSchemaInfos({ absolutePathToSchemas: resolve(__dirname, "./") });
8+
9+
schemas.forEach(({ absolutePath, apiFileName }) => {
10+
generateApiForTest({
11+
testName: "additional properties 2.0 test",
12+
silent: true,
13+
name: apiFileName,
14+
input: absolutePath,
15+
output: resolve(__dirname, "./"),
16+
addReadonly: true,
17+
generateClient: false,
18+
}).then(() => {
19+
validateGeneratedModule(resolve(__dirname, `./${apiFileName}`));
20+
assertGeneratedModule(resolve(__dirname, `./${apiFileName}`), resolve(__dirname, `./expected.ts`));
21+
});
22+
});

tests/spec/jsSingleHttpClientModular/test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ schemas.forEach(({ absolutePath, Exception }) => {
1414
output: resolve(__dirname, "./generated"),
1515
modular: true,
1616
toJS: true,
17-
cleanOutput: true,
1817
singleHttpClient: true,
1918
generateClient: true,
2019
generateRouteTypes: true,

tests/spec/object-types/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ export type Test1 = {
4141

4242
export interface AdditionalObjectProperties {
4343
id?: string;
44+
[key: string]: any;
4445
}
4546

4647
export interface AdditionalIntProperties {
4748
id?: string;
49+
[key: string]: any;
4850
}
4951

5052
export interface ABCOptional {

0 commit comments

Comments
 (0)