Skip to content

Commit 53ea276

Browse files
committed
Added kotlin support
1 parent c8cf2ec commit 53ea276

25 files changed

+284
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ test/e2e/generated
1515
samples/generated
1616
samples/swagger-codegen-cli-v2.jar
1717
samples/swagger-codegen-cli-v3.jar
18+
output
19+
bin/generator.js

bin/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const params = program
1919
.option('--exportCore <value>', 'Write core files to disk', true)
2020
.option('--exportServices <value>', 'Write services to disk', true)
2121
.option('--exportModels <value>', 'Write models to disk', true)
22+
.option(
23+
'--exportKotlinModels <value>',
24+
'Write kotlin models to disk. If this option is set. Only Kotlin models will be generated.',
25+
false
26+
)
27+
.option('--kotlinPackageName <value>', 'Name of the package for the generated Kotlin models.', 'com.models')
2228
.option('--exportSchemas <value>', 'Write schemas to disk', false)
2329
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
2430
.option('--postfix <value>', 'Deprecated: Use --postfixServices instead. Service name postfix', 'Service')
@@ -28,7 +34,7 @@ const params = program
2834
.parse(process.argv)
2935
.opts();
3036

31-
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
37+
const OpenAPI = require(path.resolve(__dirname, './generator.js'));
3238

3339
if (OpenAPI) {
3440
OpenAPI.generate({
@@ -41,6 +47,8 @@ if (OpenAPI) {
4147
exportCore: JSON.parse(params.exportCore) === true,
4248
exportServices: JSON.parse(params.exportServices) === true,
4349
exportModels: JSON.parse(params.exportModels) === true,
50+
exportKotlinModels: JSON.parse(params.exportModels) === true,
51+
kotlinPackageName: params.kotlinPackageName,
4452
exportSchemas: JSON.parse(params.exportSchemas) === true,
4553
indent: params.indent,
4654
postfixServices: params.postfixServices ?? params.postfix,

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
"email": "info@madebyferdi.com"
3232
}
3333
],
34-
"main": "dist/index.js",
34+
"main": "bin/generator.js",
3535
"types": "types/index.d.ts",
3636
"bin": {
3737
"openapi": "bin/index.js"
3838
},
3939
"files": [
4040
"bin/index.js",
41-
"dist/index.js",
41+
"bin/generator.js",
4242
"types/index.d.ts"
4343
],
4444
"scripts": {
@@ -120,7 +120,7 @@
120120
"typescript": "4.8.4",
121121
"zone.js": "0.11.8"
122122
},
123-
"overrides" : {
123+
"overrides": {
124124
"rollup": "3.2.3"
125125
}
126126
}

rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default {
6868
input: './src/index.ts',
6969
output: {
7070
exports: 'named',
71-
file: './dist/index.js',
71+
file: './bin/generator.js',
7272
format: 'cjs',
7373
},
7474
external: ['camelcase', 'commander', 'fs-extra', 'handlebars', 'json-schema-ref-parser'],

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export type Options = {
2222
exportCore?: boolean;
2323
exportServices?: boolean;
2424
exportModels?: boolean;
25+
exportKotlinModels?: boolean;
26+
kotlinPackageName?: string;
2527
exportSchemas?: boolean;
2628
indent?: Indent;
2729
postfixServices?: string;
@@ -43,6 +45,8 @@ export type Options = {
4345
* @param exportCore Generate core client classes
4446
* @param exportServices Generate services
4547
* @param exportModels Generate models
48+
* @param exportKotlinModels Generate models for Kotlin
49+
* @param kotlinPackageName Kotlin package name
4650
* @param exportSchemas Generate schemas
4751
* @param indent Indentation options (4, 2 or tab)
4852
* @param postfixServices Service name postfix
@@ -60,11 +64,13 @@ export const generate = async ({
6064
exportCore = true,
6165
exportServices = true,
6266
exportModels = true,
67+
exportKotlinModels = false,
6368
exportSchemas = false,
6469
indent = Indent.SPACE_4,
6570
postfixServices = 'Service',
6671
postfixModels = '',
6772
request,
73+
kotlinPackageName = '',
6874
write = true,
6975
}: Options): Promise<void> => {
7076
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
@@ -90,6 +96,8 @@ export const generate = async ({
9096
exportCore,
9197
exportServices,
9298
exportModels,
99+
exportKotlinModels,
100+
kotlinPackageName,
93101
exportSchemas,
94102
indent,
95103
postfixServices,
@@ -114,6 +122,8 @@ export const generate = async ({
114122
exportCore,
115123
exportServices,
116124
exportModels,
125+
exportKotlinModels,
126+
kotlinPackageName,
117127
exportSchemas,
118128
indent,
119129
postfixServices,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package {{kotlinPackageName}}
2+
3+
import kotlin.collections.*
4+
5+
{{#equals export 'enum'}}
6+
{{>exportKotlinEnum}}
7+
{{else}}
8+
{{>exportKotlinClass}}
9+
{{/equals}}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{~#equals base 'string'}}
2+
{{~#equals format 'uuid'}}java.util.UUID
3+
{{~else}}String{{/equals~}}
4+
{{~else equals base 'number'}}
5+
{{~#equals format 'integer'}}Int
6+
{{~else equals format 'float'}}Float
7+
{{~else equals format 'double'}}Double
8+
{{~else}}Long
9+
{{/equals~}}
10+
{{~else equals base 'boolean'}}Boolean
11+
{{~else}}{{base}}
12+
{{/equals~}}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{{#ifdef description deprecated}}
2+
/**
3+
{{#if description}}
4+
* {{{escapeComment description}}}
5+
{{/if}}
6+
{{#if deprecated}}
7+
* @deprecated
8+
{{/if}}
9+
*/
10+
{{/ifdef}}
11+
data class {{{name}}} (
12+
{{#each properties}}
13+
{{#ifdef description deprecated}}
14+
/**
15+
{{#if description}}
16+
* {{{escapeComment description}}}
17+
{{/if}}
18+
{{#if deprecated}}
19+
* @deprecated
20+
{{/if}}
21+
*/
22+
{{/ifdef}}
23+
var {{{name}}}: {{>typeKotlin parent=../name}}{{>isRequired}}{{#unless isRequired}} = null{{/unless}},
24+
{{/each}}
25+
)
26+
{{#if enums}}
27+
{{#unless @root.useUnionTypes}}
28+
29+
{{#each enums}}
30+
{{#if description}}
31+
/**
32+
* {{{escapeComment description}}}
33+
*/
34+
{{/if}}
35+
enum class {{../name}}_{{{name}}} {
36+
{{#each enum}}
37+
{{{name}}},
38+
{{/each}}
39+
}
40+
{{~/each~}}
41+
{{~/unless~}}
42+
{{~/if~}}
43+
44+
{{#if properties}}
45+
{{#each properties}}
46+
{{#if properties}}
47+
48+
{{#ifdef description deprecated}}
49+
/**
50+
{{#if description}}
51+
* {{{escapeComment description}}}
52+
{{/if}}
53+
{{#if deprecated}}
54+
* @deprecated
55+
{{/if}}
56+
*/
57+
{{/ifdef}}
58+
data class {{../name}}_{{{name}}} (
59+
{{#each properties}}
60+
{{#ifdef description deprecated}}
61+
/**
62+
{{#if description}}
63+
* {{{escapeComment description}}}
64+
{{/if}}
65+
{{#if deprecated}}
66+
* @deprecated
67+
{{/if}}
68+
*/
69+
{{/ifdef}}
70+
var {{{name}}}: {{>typeKotlin parent=../name}}{{>isRequired}}{{#unless isRequired}} = null{{/unless}},
71+
{{/each}}
72+
)
73+
74+
{{~/if~}}
75+
{{~/each~}}
76+
{{~/if~}}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{#ifdef description deprecated}}
2+
/**
3+
{{#if description}}
4+
* {{{escapeComment description}}}
5+
{{/if}}
6+
{{#if deprecated}}
7+
* @deprecated
8+
{{/if}}
9+
*/
10+
{{/ifdef}}
11+
enum class {{{name}}} {
12+
{{#each enum}}
13+
{{#if description}}
14+
/**
15+
* {{{escapeComment description}}}
16+
*/
17+
{{/if}}
18+
{{{name}}},
19+
{{/each}}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{#equals export 'interface'}}
2+
{{>typeKotlinEnum}}
3+
{{else equals export 'reference'}}
4+
{{>typeKotlinReference}}
5+
{{else equals export 'enum'}}
6+
{{>typeKotlinEnum}}
7+
{{else equals export 'array'}}
8+
{{>typeKotlinArray}}
9+
{{else equals export 'dictionary'}}
10+
{{>typeKotlinDictionary}}
11+
{{else equals export 'one-of'}}
12+
typeUnion {{>typeUnion}}
13+
{{else equals export 'any-of'}}
14+
typeUnion {{>typeUnion}}
15+
{{else equals export 'all-of'}}
16+
typeIntersection {{>typeIntersection}}
17+
{{else}}
18+
{{>typeKotlinReference}}
19+
{{/equals}}

0 commit comments

Comments
 (0)