Skip to content
Open
26 changes: 16 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const defaultOptions = {
optionalParams: true,
useExampleValues: true,
useDefaultValues: true,
inlineSchema: true
//inlineSchema: true
},
check: {
status: true,
Expand All @@ -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({
Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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: '"'
}))
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,4 +22,4 @@
"js-yaml": "^4.1.0",
"json-schema-faker": "^0.5.0-rcv.44"
}
}
}
14 changes: 14 additions & 0 deletions tests/index-jsonpath.js
Original file line number Diff line number Diff line change
@@ -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
}
})