From 6d72770c65cfa7f14139db250d3cd0c402caf9a4 Mon Sep 17 00:00:00 2001 From: dphuang2 Date: Sat, 24 Aug 2024 22:37:07 -0700 Subject: [PATCH] specUsesPatternString --- .../src/util/execute-test-command.ts | 13 +++++-- .../src/util/spec-uses-pattern-string.ts | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 generator/konfig-dash/packages/konfig-cli/src/util/spec-uses-pattern-string.ts diff --git a/generator/konfig-dash/packages/konfig-cli/src/util/execute-test-command.ts b/generator/konfig-dash/packages/konfig-cli/src/util/execute-test-command.ts index 23dfd05a61..93f2c1f336 100644 --- a/generator/konfig-dash/packages/konfig-cli/src/util/execute-test-command.ts +++ b/generator/konfig-dash/packages/konfig-cli/src/util/execute-test-command.ts @@ -6,6 +6,7 @@ import { RequiredEnvironmentVariablesConfig, TestConfig, KonfigYamlAdditionalGeneratorConfig, + parseSpec, } from 'konfig-lib' import waiton from 'wait-on' import kill from 'konfig-kill-port' @@ -13,6 +14,8 @@ import path from 'path' import { parseKonfigYaml } from './parse-konfig-yaml' import { parseFilterFlag } from './parseFilterFlag' import { findNodeModulesBinPath } from './find-node-modules-bin-path' +import * as fs from 'fs' +import { specUsesPatternString } from './spec-uses-pattern-string' const validateRequiredEnvironmentVariables = ( config: RequiredEnvironmentVariablesConfig @@ -47,6 +50,10 @@ export async function executeTestCommand({ configDir, }) + const specPath = common.specPath + const specRawString = fs.readFileSync(specPath, 'utf-8') + const usesPatternString = specUsesPatternString(specRawString) + // kill any existing process on mock server port if (!noMockServer) { CliUx.ux.log(`Killing any process using port ${mockServerPort}`) @@ -66,9 +73,9 @@ export async function executeTestCommand({ cwd: cliRoot, }) execa.command( - `${pathToPrism} mock ${useDynamicResponseConfiguration ? '-d' : ''} ${ - common.specPath - } --port ${mockServerPort}`, + `${pathToPrism} mock ${ + useDynamicResponseConfiguration || usesPatternString ? '-d' : '' + } ${common.specPath} --port ${mockServerPort}`, { cwd: configDir, stdio: 'inherit', diff --git a/generator/konfig-dash/packages/konfig-cli/src/util/spec-uses-pattern-string.ts b/generator/konfig-dash/packages/konfig-cli/src/util/spec-uses-pattern-string.ts new file mode 100644 index 0000000000..0b782e1f22 --- /dev/null +++ b/generator/konfig-dash/packages/konfig-cli/src/util/spec-uses-pattern-string.ts @@ -0,0 +1,36 @@ +import { OpenAPIV3_XDocument, recurseObject } from 'konfig-lib' +import * as yaml from 'js-yaml' + +export function specUsesPatternString(specString: string): boolean { + let spec: OpenAPIV3_XDocument + + try { + // Try parsing as JSON first + spec = JSON.parse(specString) + } catch { + // If JSON parsing fails, try parsing as YAML + try { + spec = yaml.load(specString) as OpenAPIV3_XDocument + } catch { + throw new Error('Invalid spec format. Must be valid JSON or YAML.') + } + } + + let hasPatternString = false + + recurseObject(spec, ({ value }) => { + if ( + typeof value === 'object' && + value !== null && + 'type' in value && + value.type === 'string' && + 'pattern' in value && + typeof value.pattern === 'string' && + value.pattern.length > 0 + ) { + hasPatternString = true + } + }) + + return hasPatternString +}