Skip to content

Commit

Permalink
refactor(oas): address duplication issues
Browse files Browse the repository at this point in the history
closes #224
  • Loading branch information
ostridm committed Feb 16, 2024
1 parent 1d5e320 commit 8da9097
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions packages/openapi-sampler/src/samplers/ArraySampler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Options, Sample, Specification, Traverse } from '../traverse';
import { Sampler, OpenAPISchema } from './Sampler';
import { isItemsExists } from '../utils';
import { isArraySchema } from '../utils';

export class ArraySampler implements Sampler {
constructor(private readonly traverse: Traverse) {}
Expand All @@ -12,21 +12,21 @@ export class ArraySampler implements Sampler {
): any[] {
let arrayLength = schema.minItems || 1;

if (isItemsExists(schema) && Array.isArray(schema.items)) {
if (isArraySchema(schema) && Array.isArray(schema.items)) {
arrayLength = Math.max(arrayLength, schema.items.length);
}

const itemSchemaGetter = (itemNumber: number) => {
if (isItemsExists(schema) && Array.isArray(schema.items)) {
if (isArraySchema(schema) && Array.isArray(schema.items)) {
return schema.items[itemNumber] || {};
}

return isItemsExists(schema) ? schema.items : {};
return isArraySchema(schema) ? schema.items : {};
};

const res: Sample[] = [];

if (!isItemsExists(schema)) {
if (!isArraySchema(schema)) {
return res;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-sampler/src/traverse/DefaultTraverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SchemaExampleExtractor } from './SchemaExampleExtractor';
import {
firstArrayElement,
getReplacementForCircular,
isRefExists,
isReference,
mergeDeep
} from '../utils';
import { OpenAPISchema, Sampler } from '../samplers';
Expand Down Expand Up @@ -77,7 +77,7 @@ export class DefaultTraverse implements Traverse {

this.pushSchemaStack(schema);

if (isRefExists(schema)) {
if (isReference(schema)) {
return this.inferRef(spec, schema, options);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VendorExampleExtractor } from './VendorExampleExtractor';
import { Options, Schema } from './Traverse';
import { firstArrayElement, isDefaultExists, isExampleExists } from '../utils';
import { firstArrayElement, hasDefault, hasExample } from '../utils';

export class SchemaExampleExtractor {
constructor(
Expand All @@ -19,7 +19,7 @@ export class SchemaExampleExtractor {
}
public extractFromProperties(schema: Schema): unknown {
let value;
if (isDefaultExists(schema)) {
if (hasDefault(schema)) {
value = schema.default;
} else if ((schema as any).const !== undefined) {
value = (schema as any).const;
Expand All @@ -31,7 +31,7 @@ export class SchemaExampleExtractor {
}

private extractFromSchemaExamples(schema: Schema): unknown {
if (isExampleExists(schema)) {
if (hasExample(schema)) {
return schema.example;
} else if (
(schema as any).examples !== undefined &&
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-sampler/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ export const getReplacementForCircular = (type: string) => ({
value: type === 'object' ? {} : type === 'array' ? [] : undefined
});

export const isExampleExists = (
export const hasExample = (
schema: Schema
): schema is OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject =>
(schema as any).example !== undefined;

export const isDefaultExists = (
export const hasDefault = (
schema: Schema
): schema is OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject =>
(schema as any).default !== undefined;

export const isItemsExists = (
export const isArraySchema = (
schema: Schema
): schema is OpenAPIV3.ArraySchemaObject | OpenAPIV2.SchemaObject =>
(schema as any).items !== undefined;

export const isRefExists = (
export const isReference = (
schema: Schema
): schema is OpenAPIV3.ReferenceObject | OpenAPIV2.ReferenceObject =>
(schema as any).$ref !== undefined;

0 comments on commit 8da9097

Please sign in to comment.