1- import fs from "fs" ;
2- import path from "path" ;
3- import { fileURLToPath } from "url" ;
4- import type { Field } from "ringcentral-open-api-parser/lib/types.js" ;
5-
6- import { escapeJavaDoc } from "./utils.js" ;
1+ import fs from "node:fs" ;
2+ import path from "node:path" ;
3+ import { fileURLToPath } from "node:url" ;
4+ import type { Field } from "ringcentral-open-api-parser" ;
75import { parsed } from "./parser.js" ;
6+ import { escapeJavaDoc } from "./utils.js" ;
87
98const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
109
@@ -21,9 +20,11 @@ const normalizeField = (f: Field): Field => {
2120 } else if ( f . type === "integer" ) {
2221 f . type = "Long" ;
2322 } else if ( f . type === "array" ) {
24- f . type = `${ normalizeField ( f . items ! ) . type } []` ;
23+ const itemType = f . items ? normalizeField ( f . items ) . type : "Object" ;
24+ f . type = `${ itemType } []` ;
2525 } else if ( f . type === "dict" ) {
26- f . type = `java.util.Map<String, ${ normalizeField ( f . items ! ) . type } >` ;
26+ const itemType = f . items ? normalizeField ( f . items ) . type : "Object" ;
27+ f . type = `java.util.Map<String, ${ itemType } >` ;
2728 } else if ( f . type === "boolean" ) {
2829 f . type = "Boolean" ;
2930 } else if ( f . type === "string" ) {
@@ -45,7 +46,7 @@ const generateField = (f: Field, modelName: string) => {
4546 p += ` @SerializedName("${ f . name } ")\n` ;
4647 f . name = f . name . replace (
4748 / - ( [ a - z ] ) / g,
48- ( match , p1 : string ) => p1 . toUpperCase ( ) ,
49+ ( _match , p1 : string ) => p1 . toUpperCase ( ) ,
4950 ) ;
5051 }
5152 if ( f . name . includes ( ":" ) ) {
@@ -60,8 +61,8 @@ const generateField = (f: Field, modelName: string) => {
6061 }` ;
6162
6263 p = ` */\n${ p } ` ;
63- if ( f . enum || ( f . items || { } ) . enum ) {
64- p = ` * Enum: ${ ( f . enum || ( f . items || { } ) . enum ) ! . join ( ", " ) } \n ${ p } ` ;
64+ if ( f . enum || f . items ? .enum ) {
65+ p = ` * Enum: ${ ( f . enum || f . items ? .enum ) ? .join ( ", " ) } \n ${ p } ` ;
6566 }
6667 if ( f . default ) {
6768 p = ` * Default: ${ f . default } \n ${ p } ` ;
@@ -83,8 +84,8 @@ const generateField = (f: Field, modelName: string) => {
8384 }
8485 if ( f . description ) {
8586 p = ` * ${
86- escapeJavaDoc ( f . description ) !
87- . trim ( )
87+ escapeJavaDoc ( f . description )
88+ ? .trim ( )
8889 . split ( "\n" )
8990 . join ( "\n * " )
9091 } \n ${ p } `;
@@ -99,7 +100,7 @@ parsed.models.forEach((model) => {
99100 ? "\n /**\n" +
100101 model . description
101102 . split ( "\n" )
102- . map ( ( line ) => "* " + line )
103+ . map ( ( line ) => `* ${ line } ` )
103104 . join ( "\n" ) +
104105 "\n*/"
105106 : ""
@@ -109,8 +110,8 @@ public class ${model.name}
109110 ${ model . fields . map ( ( f ) => generateField ( f , model . name ) ) . join ( "\n\n " ) }
110111}` ;
111112 if ( code . includes ( "@SerializedName(" ) ) {
112- code = " import com.google.gson.annotations.SerializedName;\n\n" + code ;
113+ code = ` import com.google.gson.annotations.SerializedName;\n\n${ code } ` ;
113114 }
114- code = " package com.ringcentral.definitions;\n\n" + code ;
115+ code = ` package com.ringcentral.definitions;\n\n${ code } ` ;
115116 fs . writeFileSync ( path . join ( outputDir , `${ model . name } .java` ) , code ) ;
116117} ) ;
0 commit comments