Skip to content

Commit cce208e

Browse files
author
Ferdi Koomen
committed
- Prefer media type order in spec
1 parent 5a7c588 commit cce208e

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/openApi/v3/parser/getContent.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ const BASIC_MEDIA_TYPES = [
2222
];
2323

2424
export function getContent(openApi: OpenApi, content: Dictionary<OpenApiMediaType>): Content | null {
25-
const basicMedia = BASIC_MEDIA_TYPES.find(mediaType => isDefined(content[mediaType]?.schema));
26-
if (basicMedia) {
25+
const basicMediaTypeWithSchema = Object.keys(content)
26+
.filter(mediaType => BASIC_MEDIA_TYPES.includes(mediaType))
27+
.find(mediaType => isDefined(content[mediaType]?.schema));
28+
if (basicMediaTypeWithSchema) {
2729
return {
28-
mediaType: basicMedia,
29-
schema: content[basicMedia].schema as OpenApiSchema,
30+
mediaType: basicMediaTypeWithSchema,
31+
schema: content[basicMediaTypeWithSchema].schema as OpenApiSchema,
3032
};
3133
}
3234

33-
const otherMediaTypes = Object.keys(content);
34-
const otherMediaType = otherMediaTypes.find(mediaType => isDefined(content[mediaType]?.schema));
35-
if (otherMediaType) {
35+
const firstMediaTypeWithSchema = Object.keys(content).find(mediaType => isDefined(content[mediaType]?.schema));
36+
if (firstMediaTypeWithSchema) {
3637
return {
37-
mediaType: otherMediaType,
38-
schema: content[otherMediaType].schema as OpenApiSchema,
38+
mediaType: firstMediaTypeWithSchema,
39+
schema: content[firstMediaTypeWithSchema].schema as OpenApiSchema,
3940
};
4041
}
4142
return null;

src/openApi/v3/parser/getOperationRequestBody.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getContent } from './getContent';
77
import { getModel } from './getModel';
88
import { getType } from './getType';
99

10-
export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequestBody): OperationParameter {
10+
export function getOperationRequestBody(openApi: OpenApi, body: OpenApiRequestBody): OperationParameter {
1111
const requestBody: OperationParameter = {
1212
in: 'body',
1313
export: 'interface',
@@ -17,21 +17,21 @@ export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequ
1717
base: 'any',
1818
template: null,
1919
link: null,
20-
description: getComment(parameter.description),
20+
description: getComment(body.description),
2121
default: undefined,
2222
isDefinition: false,
2323
isReadOnly: false,
24-
isRequired: parameter.required === true,
25-
isNullable: parameter.nullable === true,
24+
isRequired: body.required === true,
25+
isNullable: body.nullable === true,
2626
imports: [],
2727
enum: [],
2828
enums: [],
2929
properties: [],
3030
mediaType: null,
3131
};
3232

33-
if (parameter.content) {
34-
const content = getContent(openApi, parameter.content);
33+
if (body.content) {
34+
const content = getContent(openApi, body.content);
3535
if (content) {
3636
requestBody.mediaType = content.mediaType;
3737
switch (requestBody.mediaType) {

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function generateV2() {
1919

2020
async function generateV3() {
2121
await OpenAPI.generate({
22-
input: './test/spec/v3.json',
22+
input: './test/spec/spec.json',
2323
output: './test/generated/v3/',
2424
httpClient: OpenAPI.HttpClient.FETCH,
2525
useOptions: false,

0 commit comments

Comments
 (0)