Skip to content

Commit

Permalink
refactor(oas): address complexity issues
Browse files Browse the repository at this point in the history
closes #224
  • Loading branch information
ostridm committed Feb 16, 2024
1 parent 928930e commit f3a4527
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 62 deletions.
71 changes: 9 additions & 62 deletions packages/openapi-sampler/src/traverse/DefaultTraverse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Options, Sample, Schema, Specification, Traverse } from './Traverse';
import { VendorExampleExtractor } from './VendorExampleExtractor';
import { SchemaExampleExtractor } from './SchemaExampleExtractor';
import {
firstArrayElement,
getReplacementForCircular,
Expand Down Expand Up @@ -50,7 +50,7 @@ export class DefaultTraverse implements Traverse {
}

constructor(
private readonly vendorExampleExtractor: VendorExampleExtractor = new VendorExampleExtractor()
private readonly sampleValueExtractor: SchemaExampleExtractor = new SchemaExampleExtractor()
) {}

public clearCache(): void {
Expand Down Expand Up @@ -94,7 +94,7 @@ export class DefaultTraverse implements Traverse {
): Sample | undefined {
const type = (schema as any).type as string;

let value = this.extractSampleValueFromSchema(schema);
let value = this.sampleValueExtractor.extractFromProperties(schema);

value =
value === undefined
Expand All @@ -113,19 +113,6 @@ export class DefaultTraverse implements Traverse {
};
}

private extractSampleValueFromSchema(schema: Schema): unknown {
let value;
if (this.isDefaultExists(schema)) {
value = schema.default;
} else if ((schema as any).const !== undefined) {
value = (schema as any).const;
} else if ((schema as any).enum && (schema as any).enum.length) {
value = firstArrayElement((schema as any).enum);
}

return value;
}

private createSampleValueFromInferredType(
schema: Schema,
options?: Options,
Expand Down Expand Up @@ -218,14 +205,12 @@ export class DefaultTraverse implements Traverse {
schema: Schema,
options: Options
): Sample | undefined {
let example = this.extractSampleValueFromExamples(schema);

example =
example === undefined
? this.extractSampleValueFromVendorExamples(schema, options)
: example;
const value = this.sampleValueExtractor.extractFromExamples(
schema,
options
);

if (example !== undefined) {
if (value !== undefined) {
const { type, readOnly, writeOnly } = schema as OpenAPISchema;

this.popSchemaStack();
Expand All @@ -234,31 +219,11 @@ export class DefaultTraverse implements Traverse {
type,
readOnly,
writeOnly,
value: example
value
};
}
}

private extractSampleValueFromExamples(schema: Schema): unknown {
if (this.isExampleExists(schema)) {
return schema.example;
} else if (
(schema as any).examples !== undefined &&
(schema as any).examples.length > 0
) {
return firstArrayElement((schema as any).examples);
}
}

private extractSampleValueFromVendorExamples(
schema: Schema,
{ includeVendorExamples }: Options
): unknown {
return includeVendorExamples
? this.vendorExampleExtractor.extract(schema)
: undefined;
}

private pushSchemaStack(schema: Schema) {
this.schemasStack.push(schema);
}
Expand Down Expand Up @@ -411,22 +376,4 @@ export class DefaultTraverse implements Traverse {
undefined
);
}

private isExampleExists(
schema: Schema
): schema is OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject {
return (
(schema as OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject).example !==
undefined
);
}

private isDefaultExists(
schema: Schema
): schema is OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject {
return (
(schema as OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject).default !==
undefined
);
}
}
71 changes: 71 additions & 0 deletions packages/openapi-sampler/src/traverse/SchemaExampleExtractor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { VendorExampleExtractor } from './VendorExampleExtractor';
import { Options, Schema } from './Traverse';
import { firstArrayElement } from '../utils';
import { OpenAPIV2, OpenAPIV3 } from '@har-sdk/core';

export class SchemaExampleExtractor {
constructor(
protected readonly vendorExampleExtractor: VendorExampleExtractor = new VendorExampleExtractor()
) {}

public extractFromExamples(schema: Schema, options: Options): unknown {
let value = this.extractFromSchemaExamples(schema);

value =
value === undefined
? this.extractFromVendorExamples(schema, options)
: value;

return value;
}
public extractFromProperties(schema: Schema): unknown {
let value;
if (this.isDefaultExists(schema)) {
value = schema.default;
} else if ((schema as any).const !== undefined) {
value = (schema as any).const;
} else if ((schema as any).enum && (schema as any).enum.length) {
value = firstArrayElement((schema as any).enum);
}

return value;
}

private extractFromSchemaExamples(schema: Schema): unknown {
if (this.isExampleExists(schema)) {
return schema.example;
} else if (
(schema as any).examples !== undefined &&
(schema as any).examples.length > 0
) {
return firstArrayElement((schema as any).examples);
}
}

private extractFromVendorExamples(
schema: Schema,
{ includeVendorExamples }: Options
): unknown {
return includeVendorExamples
? this.vendorExampleExtractor.extract(schema)
: undefined;
}

private isExampleExists(
schema: Schema
): schema is OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject {
return (
(schema as OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject).example !==
undefined
);
}

private isDefaultExists(
schema: Schema
): schema is OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject {
return (
(schema as OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject).default !==
undefined
);
}
}

0 comments on commit f3a4527

Please sign in to comment.