diff --git a/index.js b/index.js index 9d87f78..0c7f46c 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ const defaultOptions = { optionalParams: true, useExampleValues: true, useDefaultValues: true, - inlineSchema: true + //inlineSchema: true }, check: { status: true, @@ -23,7 +23,7 @@ const defaultOptions = { JSONSchemaFaker.format('binary', () => 'file.txt') -async function generateWorkflow (file, options) { +async function generateWorkflow(file, options) { options = merge(defaultOptions, options) JSONSchemaFaker.option({ @@ -46,7 +46,7 @@ async function generateWorkflow (file, options) { const taggedSchemas = [] if (swagger.components?.schemas) { - if (options.generator.inlineSchema) { + if (options.check.schema) { workflow.components = { schemas: swagger.components?.schemas } @@ -73,8 +73,8 @@ async function generateWorkflow (file, options) { const value = param.schema?.default || param.example - || (param.examples && Object.keys(param.examples > 0) ? Object.values(param.examples)[0].value : false) - || param.schema ? JSONSchemaFaker.generate(param.schema, taggedSchemas) : false + || (param.examples && Object.keys(param.examples).length > 0 ? Object.values(param.examples)[0] : false) + || (param.schema ? JSONSchemaFaker.generate(param.schema, taggedSchemas) : false) if (param.in === 'path' && options.generator.pathParams) { step.http.url = step.http.url.replace(`{${param.name}}`, value) @@ -103,8 +103,8 @@ async function generateWorkflow (file, options) { for (const contentType in requestBody) { const body = requestBody[contentType].example - || (requestBody[contentType].examples && Object.keys(requestBody[contentType].examples) > 0) ? Object.values(requestBody[contentType].examples)[0].value : false - || JSONSchemaFaker.generate(requestBody[contentType].schema, taggedSchemas) + || ((requestBody[contentType].examples && Object.keys(requestBody[contentType].examples).length > 0) ? Object.values(requestBody[contentType].examples)[0].value : false) + || (requestBody[contentType].schema ? JSONSchemaFaker.generate(requestBody[contentType].schema, taggedSchemas) : false) if (!step.http.headers) step.http.headers = {} const bodyExists = step.http.json || step.http.xml || step.http.body || step.http.form || step.http.formData @@ -153,7 +153,7 @@ async function generateWorkflow (file, options) { if (swagger.paths[path][method].responses) { const response = Object.values(swagger.paths[path][method].responses)[0] - const responseContent = response.content?.[options.contentType] + const responseContent = response.content?.[options.contentType] if (response) { if (Object.keys(options.check).length !== 0) step.http.check = {} @@ -171,8 +171,14 @@ async function generateWorkflow (file, options) { step.http.check.schema = responseContent.schema } + const jsonPathIndicator = "$" + if (options.check.examples) { - step.http.check.json = responseContent.example || (responseContent.examples ? Object.values(responseContent.examples)[0].value : undefined) + if (responseContent.example && JSON.stringify(responseContent.example).includes(jsonPathIndicator) || (responseContent.examples && JSON.stringify(Object.values(responseContent.examples)[0].value).includes(jsonPathIndicator))) { + step.http.check.jsonpath = responseContent.example || (responseContent.examples ? Object.values(responseContent.examples)[0].value : undefined) + } else { + step.http.check.json = responseContent.example || (responseContent.examples ? Object.values(responseContent.examples)[0].value : undefined) + } } } } @@ -204,7 +210,7 @@ async function generateWorkflow (file, options) { return workflow } -async function generateWorkflowFile (file, output, options) { +async function generateWorkflowFile(file, output, options) { return fs.promises.writeFile(output, dump(await generateWorkflow(file, options), { quotingType: '"' })) diff --git a/package.json b/package.json index 7234d13..a8ae747 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stepci/plugin-openapi", - "version": "0.4.2", + "version": "0.4.5", "description": "Step CI OpenAPI Plugin", "main": "index.js", "repository": { @@ -22,4 +22,4 @@ "js-yaml": "^4.1.0", "json-schema-faker": "^0.5.0-rcv.44" } -} +} \ No newline at end of file diff --git a/tests/index-jsonpath.js b/tests/index-jsonpath.js new file mode 100644 index 0000000..d3c669e --- /dev/null +++ b/tests/index-jsonpath.js @@ -0,0 +1,14 @@ +const { generateWorkflowFile } = require('../index.js') +generateWorkflowFile('./tests/swagger.json', 'workflow.yml', { + check: { + schema: false + }, + generator: { + pathParams: true, + requestBody: true, + optionalParams: true, + useExampleValues: true, + useDefaultValues: false, + inlineSchema: false + } +})