Skip to content

Commit

Permalink
specUsesPatternString
Browse files Browse the repository at this point in the history
  • Loading branch information
dphuang2 committed Aug 25, 2024
1 parent 3e9fa56 commit 6d72770
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
RequiredEnvironmentVariablesConfig,
TestConfig,
KonfigYamlAdditionalGeneratorConfig,
parseSpec,
} from 'konfig-lib'
import waiton from 'wait-on'
import kill from 'konfig-kill-port'
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
Expand Down Expand Up @@ -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}`)
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6d72770

Please sign in to comment.