Skip to content

Commit

Permalink
fix(oas): use application/json when consumes media-type configura…
Browse files Browse the repository at this point in the history
…tion missing

closes #210
  • Loading branch information
ostridm committed Sep 18, 2023
1 parent 749b6d8 commit 6bd7082
Show file tree
Hide file tree
Showing 15 changed files with 4,008 additions and 1,825 deletions.
39 changes: 39 additions & 0 deletions packages/oas/src/converter/parts/Oas2MediaTypesResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { isOASV2 } from '../../utils';

import { OpenAPIV2 } from '@har-sdk/core';

export class Oas2MediaTypesResolver {
private readonly DEFAULT_MEDIA_TYPE = 'application/json';

constructor(private readonly spec: OpenAPIV2.Document) {}

public resolveToConsume(operation: OpenAPIV2.OperationObject) {
let mediaTypes = this.resolve(operation, 'consumes');
if (!mediaTypes?.length) {
mediaTypes = [this.DEFAULT_MEDIA_TYPE];
}

return mediaTypes;
}

public resolveToProduce(operation: OpenAPIV2.OperationObject) {
const mediaTypes = this.resolve(operation, 'produces');

return mediaTypes?.length ? mediaTypes : undefined;
}

private resolve(
operation: OpenAPIV2.OperationObject,
node: 'consumes' | 'produces'
): OpenAPIV2.MimeTypes {
let mediaTypes: OpenAPIV2.MimeTypes;

if (operation[node]?.length) {
mediaTypes = operation[node];
} else if (isOASV2(this.spec) && this.spec[node]?.length) {
mediaTypes = this.spec[node];
}

return mediaTypes?.map((mediaType) => mediaType?.trim()).filter(Boolean);
}
}
19 changes: 15 additions & 4 deletions packages/oas/src/converter/parts/headers/Oas2HeadersConveter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@ import { LocationParam } from '../LocationParam';
import { Oas2ValueSerializer } from '../Oas2ValueSerializer';
import { Sampler } from '../../Sampler';
import { HeadersConverter } from './HeadersConverter';
import { Oas2MediaTypesResolver } from '../Oas2MediaTypesResolver';
import { Header, OpenAPIV2 } from '@har-sdk/core';

export class Oas2HeadersConverter extends HeadersConverter<OpenAPIV2.Document> {
private readonly oas2ValueSerializer = new Oas2ValueSerializer();
private readonly oas2MediaTypeResolver!: Oas2MediaTypesResolver;

constructor(spec: OpenAPIV2.Document, sampler: Sampler) {
super(spec, sampler);
this.oas2MediaTypeResolver = new Oas2MediaTypesResolver(spec);
}

protected createContentTypeHeaders(
pathObj: OpenAPIV2.OperationObject
operation: OpenAPIV2.OperationObject
): Header[] {
return this.createHeaders('content-type', pathObj.consumes);
return this.createHeaders(
'content-type',
this.oas2MediaTypeResolver.resolveToConsume(operation)
);
}

protected createAcceptHeaders(pathObj: OpenAPIV2.OperationObject): Header[] {
return this.createHeaders('accept', pathObj.produces);
protected createAcceptHeaders(
operation: OpenAPIV2.OperationObject
): Header[] {
return this.createHeaders(
'accept',
this.oas2MediaTypeResolver.resolveToProduce(operation)
);
}

protected convertHeaderParam(
Expand Down
16 changes: 6 additions & 10 deletions packages/oas/src/converter/parts/postdata/Oas2BodyConverter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { BodyConverter } from './BodyConverter';
import type { Sampler } from '../../Sampler';
import { filterLocationParams, getParameters, isOASV2 } from '../../../utils';
import { filterLocationParams, getParameters } from '../../../utils';
import { Oas2MediaTypesResolver } from '../Oas2MediaTypesResolver';
import type { OpenAPIV2, PostData } from '@har-sdk/core';

export class Oas2BodyConverter extends BodyConverter<OpenAPIV2.Document> {
private readonly oas2MediaTypeResolver!: Oas2MediaTypesResolver;

constructor(spec: OpenAPIV2.Document, sampler: Sampler) {
super(spec, sampler);
this.oas2MediaTypeResolver = new Oas2MediaTypesResolver(spec);
}

public convert(path: string, method: string): PostData | null {
Expand Down Expand Up @@ -35,17 +39,9 @@ export class Oas2BodyConverter extends BodyConverter<OpenAPIV2.Document> {
protected getContentType(path: string, method: string): string | undefined {
const operation = this.spec.paths[path][method];

let consumes: OpenAPIV2.MimeTypes;

if (operation.consumes?.length) {
consumes = operation.consumes;
} else if (isOASV2(this.spec) && this.spec.consumes?.length) {
consumes = this.spec.consumes;
}

return this.sampler.sample({
type: 'array',
examples: consumes
examples: this.oas2MediaTypeResolver.resolveToConsume(operation)
});
}

Expand Down
10 changes: 10 additions & 0 deletions packages/oas/tests/DefaultConverter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ describe('DefaultConverter', () => {
expected: 'empty-schema.swagger.result.json',
message: 'should correctly handle empty schemas (swagger)'
},
{
input: 'missing-consumes.swagger.yaml',
expected: 'missing-consumes.swagger.result.json',
message: 'should correctly handle missing consumes (swagger)'
},
{
input: 'override-consumes.swagger.yaml',
expected: 'override-consumes.swagger.result.json',
message: 'should correctly handle override consumes to empty (swagger)'
},
{
input: 'empty-schema.oas.yaml',
expected: 'empty-schema.oas.result.json',
Expand Down
Loading

0 comments on commit 6bd7082

Please sign in to comment.