Skip to content

Commit f441f36

Browse files
committed
1 parent 203b74e commit f441f36

17 files changed

+336
-194
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.5.3",
3+
"version": "0.5.4",
44
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

src/openApi/v2/parser/getMappedType.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('getMappedType', () => {
1515
expect(getMappedType('object')).toEqual('any');
1616
expect(getMappedType('void')).toEqual('void');
1717
expect(getMappedType('null')).toEqual('null');
18-
expect(getMappedType('unknown')).toEqual('unknown');
19-
expect(getMappedType('')).toEqual('');
18+
expect(getMappedType('unknown')).toEqual(undefined);
19+
expect(getMappedType('')).toEqual(undefined);
2020
});
2121
});

src/openApi/v2/parser/getMappedType.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import { PrimaryType, TYPE_MAPPINGS } from './constants';
33
/**
44
* Get mapped type for given type to any basic Typescript/Javascript type.
55
*/
6-
export function getMappedType(type: string): PrimaryType | string {
7-
const mapped = TYPE_MAPPINGS.get(type.toLowerCase());
8-
if (mapped) {
9-
return mapped;
10-
}
11-
return type;
6+
export function getMappedType(type: string): PrimaryType | undefined {
7+
return TYPE_MAPPINGS.get(type.toLowerCase());
128
}
139

1410
export function hasMappedType(type: string): boolean {

src/openApi/v2/parser/getType.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,28 @@ describe('getType', () => {
5656
expect(type.template).toEqual(null);
5757
expect(type.imports).toEqual([]);
5858
});
59+
60+
it('should support dot', () => {
61+
const type = getType('#/definitions/model.000');
62+
expect(type.type).toEqual('model_000');
63+
expect(type.base).toEqual('model_000');
64+
expect(type.template).toEqual(null);
65+
expect(type.imports).toEqual(['model_000']);
66+
});
67+
68+
it('should support dashes', () => {
69+
const type = getType('#/definitions/some_special-schema');
70+
expect(type.type).toEqual('some_special_schema');
71+
expect(type.base).toEqual('some_special_schema');
72+
expect(type.template).toEqual(null);
73+
expect(type.imports).toEqual(['some_special_schema']);
74+
});
75+
76+
it('should support dollar sign', () => {
77+
const type = getType('#/definitions/$some+special+schema');
78+
expect(type.type).toEqual('$some_special_schema');
79+
expect(type.base).toEqual('$some_special_schema');
80+
expect(type.template).toEqual(null);
81+
expect(type.imports).toEqual(['$some_special_schema']);
82+
});
5983
});

src/openApi/v2/parser/getType.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { PrimaryType } from './constants';
33
import { getMappedType, hasMappedType } from './getMappedType';
44
import { stripNamespace } from './stripNamespace';
55

6+
function encode(value: string): string {
7+
return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^\w$]+/g, '_');
8+
}
9+
610
/**
711
* Parse any string value into a type object.
812
* @param value String value like "integer" or "Link[Model]".
@@ -21,8 +25,8 @@ export function getType(value?: string, template?: string): Type {
2125
if (/\[.*\]$/g.test(valueClean)) {
2226
const matches = valueClean.match(/(.*?)\[(.*)\]$/);
2327
if (matches && matches.length) {
24-
const match1 = getType(matches[1]);
25-
const match2 = getType(matches[2]);
28+
const match1 = getType(encode(matches[1]));
29+
const match2 = getType(encode(matches[2]));
2630

2731
if (match1.type === PrimaryType.ARRAY) {
2832
result.type = `${match2.type}[]`;
@@ -43,12 +47,15 @@ export function getType(value?: string, template?: string): Type {
4347
}
4448
} else if (hasMappedType(valueClean)) {
4549
const mapped = getMappedType(valueClean);
46-
result.type = mapped;
47-
result.base = mapped;
50+
if (mapped) {
51+
result.type = mapped;
52+
result.base = mapped;
53+
}
4854
} else if (valueClean) {
49-
result.type = valueClean;
50-
result.base = valueClean;
51-
result.imports.push(valueClean);
55+
const type = encode(valueClean);
56+
result.type = type;
57+
result.base = type;
58+
result.imports.push(type);
5259
}
5360

5461
// If the property that we found matched the parent template class

src/openApi/v2/parser/stripNamespace.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@ describe('stripNamespace', () => {
66
expect(stripNamespace('#/parameters/Item')).toEqual('Item');
77
expect(stripNamespace('#/responses/Item')).toEqual('Item');
88
expect(stripNamespace('#/securityDefinitions/Item')).toEqual('Item');
9-
expect(stripNamespace('Template[Model]')).toEqual('Template[Model]');
10-
expect(stripNamespace('namespace.Template[Model]')).toEqual('Template[Model]');
11-
expect(stripNamespace('namespace.Template[namespace.Model]')).toEqual('Template[Model]');
12-
expect(stripNamespace('Item')).toEqual('Item');
139
});
1410
});

src/openApi/v2/parser/stripNamespace.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,10 @@
33
* @param value
44
*/
55
export function stripNamespace(value: string): string {
6-
return (
7-
value
8-
.trim()
9-
.replace(/^#\/definitions\//, '')
10-
.replace(/^#\/parameters\//, '')
11-
.replace(/^#\/responses\//, '')
12-
.replace(/^#\/securityDefinitions\//, '')
13-
14-
// First we remove the namespace from template notation:
15-
// Example: namespace.Template[namespace.Model] -> namespace.Template[Model]
16-
.replace(/(\[.*\]$)/, (s: string): string => {
17-
const v = s.replace('[', '').replace(']', '').split('.').pop()!;
18-
return `[${v}]`;
19-
})
20-
21-
// Then we remove the namespace from the complete result:
22-
// Example: namespace.Template[Model] -> Template[Model]
23-
.replace(/.*/, (s: string): string => {
24-
return s.split('.').pop()!;
25-
})
26-
);
6+
return value
7+
.trim()
8+
.replace(/^#\/definitions\//, '')
9+
.replace(/^#\/parameters\//, '')
10+
.replace(/^#\/responses\//, '')
11+
.replace(/^#\/securityDefinitions\//, '');
2712
}

src/openApi/v3/parser/getMappedType.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('getMappedType', () => {
1515
expect(getMappedType('object')).toEqual('any');
1616
expect(getMappedType('void')).toEqual('void');
1717
expect(getMappedType('null')).toEqual('null');
18-
expect(getMappedType('unknown')).toEqual('unknown');
19-
expect(getMappedType('')).toEqual('');
18+
expect(getMappedType('unknown')).toEqual(undefined);
19+
expect(getMappedType('')).toEqual(undefined);
2020
});
2121
});

src/openApi/v3/parser/getMappedType.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import { PrimaryType, TYPE_MAPPINGS } from './constants';
33
/**
44
* Get mapped type for given type to any basic Typescript/Javascript type.
55
*/
6-
export function getMappedType(type: string): PrimaryType | string {
7-
const mapped = TYPE_MAPPINGS.get(type.toLowerCase());
8-
if (mapped) {
9-
return mapped;
10-
}
11-
return type;
6+
export function getMappedType(type: string): PrimaryType | undefined {
7+
return TYPE_MAPPINGS.get(type.toLowerCase());
128
}
139

1410
export function hasMappedType(type: string): boolean {

src/openApi/v3/parser/getType.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,28 @@ describe('getType', () => {
5656
expect(type.template).toEqual(null);
5757
expect(type.imports).toEqual([]);
5858
});
59+
60+
it('should support dot', () => {
61+
const type = getType('#/components/schemas/model.000');
62+
expect(type.type).toEqual('model_000');
63+
expect(type.base).toEqual('model_000');
64+
expect(type.template).toEqual(null);
65+
expect(type.imports).toEqual(['model_000']);
66+
});
67+
68+
it('should support dashes', () => {
69+
const type = getType('#/components/schemas/some_special-schema');
70+
expect(type.type).toEqual('some_special_schema');
71+
expect(type.base).toEqual('some_special_schema');
72+
expect(type.template).toEqual(null);
73+
expect(type.imports).toEqual(['some_special_schema']);
74+
});
75+
76+
it('should support dollar sign', () => {
77+
const type = getType('#/components/schemas/$some+special+schema');
78+
expect(type.type).toEqual('$some_special_schema');
79+
expect(type.base).toEqual('$some_special_schema');
80+
expect(type.template).toEqual(null);
81+
expect(type.imports).toEqual(['$some_special_schema']);
82+
});
5983
});

0 commit comments

Comments
 (0)