Skip to content

Commit

Permalink
Fix all the code generation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlong committed Dec 27, 2019
1 parent 09c51ad commit 516e0c5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion code-generator/definitions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import yaml from 'js-yaml'
import fs from 'fs'
import path from 'path'
import pascalCase from 'pascal-case'
import { pascalCase } from 'change-case'

const outputDir = '../src/main/java/com/ringcentral/definitions'

Expand Down
1 change: 1 addition & 0 deletions code-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"change-case": "^4.1.1",
"js-yaml": "^3.13.1",
"lower-case-first": "^2.0.1",
"ramda": "^0.26.1"
},
"devDependencies": {
Expand Down
17 changes: 9 additions & 8 deletions code-generator/paths.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import yaml from 'js-yaml'
import fs from 'fs'
import * as R from 'ramda'
import changeCase from 'change-case'
import { pascalCase, titleCase } from 'change-case'
import { lowerCaseFirst } from 'lower-case-first'
import path from 'path'

import { normalizePath, deNormalizePath, getResponseType, appendCodeToFile } from './utils'
Expand All @@ -17,7 +18,7 @@ const paths = Object.keys(doc.paths)
const normalizedPaths = paths.map(p => normalizePath(p))

const getRoutes = (prefix, name) => {
return [...prefix.split('/').filter(t => t !== '' && !t.startsWith('{')), name].map(t => changeCase.pascalCase(t))
return [...prefix.split('/').filter(t => t !== '' && !t.startsWith('{')), name].map(t => pascalCase(t))
}
const getFolderPath = (prefix, name) => {
return path.join(outputDir, ...getRoutes(prefix, name))
Expand Down Expand Up @@ -128,7 +129,7 @@ const generate = (prefix = '/') => {
})

operations.forEach(operation => {
const method = changeCase.pascalCase(operation.method)
const method = pascalCase(operation.method)
const smartMethod = (operation.method === 'get' && !operation.endpoint.endsWith('}') &&
R.any(o => o.method === 'get' && o.endpoint === operation.endpoint + `/{${paramName}}`)(operations)) ? 'List' : method
const responses = operation.detail.responses
Expand All @@ -152,18 +153,18 @@ const generate = (prefix = '/') => {
bodyParam = 'body'
} else {
bodyClass = R.last(body.schema.$ref.split('/'))
bodyParam = changeCase.lowerCaseFirst(bodyClass)
bodyParam = lowerCaseFirst(bodyClass)
bodyClass = 'com.ringcentral.definitions.' + bodyClass
}
}
}
if (formUrlEncoded || multipart) {
bodyClass = `com.ringcentral.definitions.${changeCase.pascalCase(operation.detail.operationId)}Request`
bodyClass = `com.ringcentral.definitions.${pascalCase(operation.detail.operationId)}Request`
bodyParam = `${operation.detail.operationId}Request`
body = (operation.detail.parameters || []).filter(p => p.in === 'body' && p.schema && p.schema.$ref)[0]
if (body) {
bodyClass = R.last(body.schema.$ref.split('/'))
bodyParam = changeCase.lowerCaseFirst(bodyClass)
bodyParam = lowerCaseFirst(bodyClass)
bodyClass = 'com.ringcentral.definitions.' + bodyClass
}
}
Expand All @@ -175,12 +176,12 @@ const generate = (prefix = '/') => {
methodParams.push(`${bodyParam}: ${bodyClass}`)
}
if (queryParams.length > 0) {
methodParams.push(`queryParams: com.ringcentral.definitions.${changeCase.pascalCase(operation.detail.operationId)}Parameters? = null`)
methodParams.push(`queryParams: com.ringcentral.definitions.${pascalCase(operation.detail.operationId)}Parameters? = null`)
}
code += `
/**
* Operation: ${operation.detail.summary || changeCase.titleCase(operation.detail.operationId)}
* Operation: ${operation.detail.summary || titleCase(operation.detail.operationId)}
* Http ${method} ${operation.endpoint}
*/
${methodParams.join(', ').includes(' = ') ? '@JvmOverloads ' : ''}fun ${smartMethod.toLowerCase()}(${methodParams.join(', ')}) : ${responseType}?
Expand Down
7 changes: 7 additions & 0 deletions code-generator/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,13 @@ loose-envify@^1.0.0, loose-envify@^1.4.0:
dependencies:
js-tokens "^3.0.0 || ^4.0.0"

lower-case-first@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/lower-case-first/-/lower-case-first-2.0.1.tgz#ecd03e25d9abc32aeab8d52b5fd7072772dd0ccf"
integrity sha512-Z5fqHsaDN4FkWtPjTkhZDBy3uD4ir9L/ooqrNzOyyruDW6nCn/05AbC9HupxEIjlIoeT4bdrtChUllcU+cNgmg==
dependencies:
tslib "^1.10.0"

lower-case@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-2.0.1.tgz#39eeb36e396115cc05e29422eaea9e692c9408c7"
Expand Down

0 comments on commit 516e0c5

Please sign in to comment.