- Prefer media type order in spec

This commit is contained in:
Ferdi Koomen 2021-10-28 21:30:52 +02:00
parent 5a7c58873f
commit cce208e514
3 changed files with 17 additions and 16 deletions

View File

@ -22,20 +22,21 @@ const BASIC_MEDIA_TYPES = [
];
export function getContent(openApi: OpenApi, content: Dictionary<OpenApiMediaType>): Content | null {
const basicMedia = BASIC_MEDIA_TYPES.find(mediaType => isDefined(content[mediaType]?.schema));
if (basicMedia) {
const basicMediaTypeWithSchema = Object.keys(content)
.filter(mediaType => BASIC_MEDIA_TYPES.includes(mediaType))
.find(mediaType => isDefined(content[mediaType]?.schema));
if (basicMediaTypeWithSchema) {
return {
mediaType: basicMedia,
schema: content[basicMedia].schema as OpenApiSchema,
mediaType: basicMediaTypeWithSchema,
schema: content[basicMediaTypeWithSchema].schema as OpenApiSchema,
};
}
const otherMediaTypes = Object.keys(content);
const otherMediaType = otherMediaTypes.find(mediaType => isDefined(content[mediaType]?.schema));
if (otherMediaType) {
const firstMediaTypeWithSchema = Object.keys(content).find(mediaType => isDefined(content[mediaType]?.schema));
if (firstMediaTypeWithSchema) {
return {
mediaType: otherMediaType,
schema: content[otherMediaType].schema as OpenApiSchema,
mediaType: firstMediaTypeWithSchema,
schema: content[firstMediaTypeWithSchema].schema as OpenApiSchema,
};
}
return null;

View File

@ -7,7 +7,7 @@ import { getContent } from './getContent';
import { getModel } from './getModel';
import { getType } from './getType';
export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequestBody): OperationParameter {
export function getOperationRequestBody(openApi: OpenApi, body: OpenApiRequestBody): OperationParameter {
const requestBody: OperationParameter = {
in: 'body',
export: 'interface',
@ -17,12 +17,12 @@ export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequ
base: 'any',
template: null,
link: null,
description: getComment(parameter.description),
description: getComment(body.description),
default: undefined,
isDefinition: false,
isReadOnly: false,
isRequired: parameter.required === true,
isNullable: parameter.nullable === true,
isRequired: body.required === true,
isNullable: body.nullable === true,
imports: [],
enum: [],
enums: [],
@ -30,8 +30,8 @@ export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequ
mediaType: null,
};
if (parameter.content) {
const content = getContent(openApi, parameter.content);
if (body.content) {
const content = getContent(openApi, body.content);
if (content) {
requestBody.mediaType = content.mediaType;
switch (requestBody.mediaType) {

View File

@ -19,7 +19,7 @@ async function generateV2() {
async function generateV3() {
await OpenAPI.generate({
input: './test/spec/v3.json',
input: './test/spec/spec.json',
output: './test/generated/v3/',
httpClient: OpenAPI.HttpClient.FETCH,
useOptions: false,