-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(oas): address complexity issues
closes #224
- Loading branch information
Showing
2 changed files
with
80 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/openapi-sampler/src/traverse/SchemaExampleExtractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |