Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(openapi-sampler): able to sample pattern with nested infinite quantifiers and max-length #247

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions packages/openapi-sampler/src/samplers/StringSampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,60 @@ export class StringSampler implements Sampler {
const randExp = new RandExp(pattern);

if (min) {
ostridm marked this conversation as resolved.
Show resolved Hide resolved
// ADHOC: make a probe for regex using min quantifier value
// e.g. ^[a]+[b]+$ expect 'ab', ^[a-z]*$ expect ''
return this.sampleMinLength(randExp, min, max);
}

randExp.max = max ?? randExp.max;
randExp.randInt = (a, b) => Math.floor((a + b) / 2);

const result = randExp.gen();

return !!max && result.length > max && this.hasInfiniteQuantifier(pattern)
ostridm marked this conversation as resolved.
Show resolved Hide resolved
? this.sampleInfiniteQuantifier(randExp, max)
: result;
}

private hasInfiniteQuantifier(pattern: string | RegExp) {
const pat = pattern.toString();
ostridm marked this conversation as resolved.
Show resolved Hide resolved

return ['+', '*', ',}'].some((q) => pat.includes(q));
ostridm marked this conversation as resolved.
Show resolved Hide resolved
}

private sampleInfiniteQuantifier(randExp: RandExp, max: number): string {
randExp.randInt = (a, b) => Math.floor((a + b) / 2);

randExp.max = 0;
randExp.randInt = (a, _) => a;
for (let i = 1, lmax = max; lmax > 0; lmax = Math.floor(max / ++i)) {
randExp.max = lmax;
ostridm marked this conversation as resolved.
Show resolved Hide resolved

const result = randExp.gen();

if (result.length >= min) {
if (result.length <= max) {
ostridm marked this conversation as resolved.
Show resolved Hide resolved
return result;
}
}

// ADHOC: fallback for failed min quantifier probe with doubled min length
return '';
}

randExp.max = 2 * min;
randExp.randInt = (a, b) => Math.floor((a + b) / 2);
private sampleMinLength(randExp: RandExp, min: number, max: number) {
ostridm marked this conversation as resolved.
Show resolved Hide resolved
derevnjuk marked this conversation as resolved.
Show resolved Hide resolved
// ADHOC: make a probe for regex using min quantifier value
// e.g. ^[a]+[b]+$ expect 'ab', ^[a-z]*$ expect ''

return this.adjustMaxLength(randExp.gen(), max);
randExp.max = 0;
ostridm marked this conversation as resolved.
Show resolved Hide resolved
randExp.randInt = (a, _) => a;

const result = randExp.gen();

if (result.length >= min) {
return result;
}
ostridm marked this conversation as resolved.
Show resolved Hide resolved

randExp.max = max ?? randExp.max;
// ADHOC: fallback for failed min quantifier probe with doubled min length

randExp.max = 2 * min;
randExp.randInt = (a, b) => Math.floor((a + b) / 2);
ostridm marked this conversation as resolved.
Show resolved Hide resolved

return randExp.gen();
return this.adjustMaxLength(randExp.gen(), max);
}

private checkLength(
Expand Down
10 changes: 10 additions & 0 deletions packages/openapi-sampler/tests/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { sample } from '../src';

describe('StringSampler', () => {
[
{
input: {
maxLength: 55,
minLength: 0,
format: 'pattern',
pattern: '^[A-Za-z0-9._%-]+@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{1,4}$',
type: 'string'
},
expected: '[email protected]'
},
{
input: {
type: 'string',
Expand Down
Loading