mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Prefer media type order in spec
This commit is contained in:
parent
5a7c58873f
commit
cce208e514
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user