Updated code generation (#1441) (#1442)

* Updated code generation

* Nit

Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
This commit is contained in:
github-actions[bot]
2021-04-07 15:25:30 +02:00
committed by GitHub
parent 93231e621e
commit 1db476f716
3 changed files with 19 additions and 40 deletions

View File

@ -59,17 +59,6 @@ const noPathValidation = [
'update'
]
// apis that uses bulkBody property
const ndjsonApi = [
'bulk',
'msearch',
'msearch_template',
'ml.find_file_structure',
'monitoring.bulk',
'xpack.ml.find_file_structure',
'xpack.monitoring.bulk'
]
function generateNamespace (namespace, nested, specFolder, version) {
const common = require(join(specFolder, '_common.json'))
let code = dedent`
@ -251,7 +240,7 @@ function generateSingleApi (version, spec, common) {
const request = {
method,
path,
${genBody(api, methods, spec[api].body)}
${genBody(api, methods, spec[api].body, spec)}
querystring
}
@ -378,7 +367,7 @@ function generateSingleApi (version, spec, common) {
}
let hasStaticPath = false
const sortedPaths = paths
let sortedPaths = paths
// some legacy API have mutliple statis paths
// this filter removes them
.filter(p => {
@ -392,6 +381,9 @@ function generateSingleApi (version, spec, common) {
// sort by number of parameters (desc)
.sort((a, b) => Object.keys(b.parts || {}).length - Object.keys(a.parts || {}).length)
const allDeprecated = paths.filter(path => path.deprecated != null)
if (allDeprecated.length === paths.length) sortedPaths = [paths[0]]
let code = ''
for (let i = 0; i < sortedPaths.length; i++) {
const { path, methods } = sortedPaths[i]
@ -439,9 +431,10 @@ function generatePickMethod (methods) {
}
}
function genBody (api, methods, body) {
function genBody (api, methods, body, spec) {
const bodyMethod = getBodyMethod(methods)
if (ndjsonApi.indexOf(api) > -1) {
const { content_type } = spec[api].headers
if (content_type && content_type.includes('application/x-ndjson')) {
return 'bulkBody: body,'
}
if (body === null && bodyMethod) {
@ -560,4 +553,3 @@ function Uppercase (str) {
}
module.exports = generateNamespace
module.exports.ndjsonApi = ndjsonApi