Skip to content

Commit 477185e

Browse files
committed
chore: Handle update
1 parent c01c852 commit 477185e

2 files changed

Lines changed: 31 additions & 50 deletions

File tree

packages/openapi-generator/src/parser/media-type.spec.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -475,21 +475,23 @@ describe('parseTopLevelMediaType', () => {
475475
});
476476
});
477477

478-
it('throws error with malformed content type', async () => {
478+
it('parses malformed content type leniently (strips bad params)', async () => {
479479
const schema = {
480480
type: 'object',
481481
properties: { file: { type: 'string', format: 'binary' } }
482482
};
483483
const encoding = { file: { contentType: 'image/png;;invalid' } };
484-
const refs = await createTestRefs();
484+
const result = parseTopLevelMediaType(
485+
createMultipartFormContent(schema, encoding),
486+
await createTestRefs(),
487+
defaultOptions
488+
);
485489

486-
expect(() =>
487-
parseTopLevelMediaType(
488-
createMultipartFormContent(schema, encoding),
489-
refs,
490-
defaultOptions
491-
)
492-
).toThrow(/invalid content type.*image\/png;;invalid.*file/i);
490+
expect(result?.encoding?.file).toEqual({
491+
contentType: 'image/png;;invalid',
492+
isImplicit: false,
493+
parsedContentTypes: [{ type: 'image/png', parameters: {} }]
494+
});
493495
});
494496

495497
it('handles wildcard content types correctly', async () => {
@@ -509,25 +511,27 @@ describe('parseTopLevelMediaType', () => {
509511
});
510512
});
511513

512-
it('throws error with completely invalid content type format', async () => {
514+
it('parses completely invalid content type leniently', async () => {
513515
const schema = {
514516
type: 'object',
515517
properties: { attachment: { type: 'string', format: 'binary' } }
516518
};
517519
const encoding = {
518520
attachment: { contentType: 'not-a-valid-content-type-at-all' }
519521
};
520-
const refs = await createTestRefs();
521-
522-
expect(() =>
523-
parseTopLevelMediaType(
524-
createMultipartFormContent(schema, encoding),
525-
refs,
526-
defaultOptions
527-
)
528-
).toThrow(
529-
/invalid content type.*not-a-valid-content-type-at-all.*attachment/i
522+
const result = parseTopLevelMediaType(
523+
createMultipartFormContent(schema, encoding),
524+
await createTestRefs(),
525+
defaultOptions
530526
);
527+
528+
expect(result?.encoding?.attachment).toEqual({
529+
contentType: 'not-a-valid-content-type-at-all',
530+
isImplicit: false,
531+
parsedContentTypes: [
532+
{ type: 'not-a-valid-content-type-at-all', parameters: {} }
533+
]
534+
});
531535
});
532536
});
533537

packages/openapi-generator/src/parser/media-type.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createLogger, ErrorWithCause } from '@sap-cloud-sdk/util';
2-
import { parse as parseContentType, type ParsedMediaType } from 'content-type';
1+
import { createLogger } from '@sap-cloud-sdk/util';
2+
import { parse as parseContentType, type ContentType } from 'content-type';
33
import { parseSchema } from './schema';
44
import type { OpenAPIV3 } from 'openapi-types';
55
import type { OpenApiMediaTypeObject, OpenApiSchema } from '../openapi-types';
@@ -19,36 +19,16 @@ const allowedMediaTypes = [
1919
interface EncodingInfo {
2020
contentType: string;
2121
isImplicit: boolean;
22-
parsedContentTypes: ParsedMediaType[];
22+
parsedContentTypes: ContentType[];
2323
}
2424

2525
type EncodingMap = Record<string, EncodingInfo>;
2626

27-
/**
28-
* Parse content types from a comma-separated content type string.
29-
* @param contentType - Comma-separated content types from encoding object.
30-
* @param propName - Property name for error messages.
31-
* @returns Array of parsed content types.
32-
*/
33-
function parseContentTypes(
34-
contentType: string,
35-
propName: string
36-
): ParsedMediaType[] {
27+
function parseContentTypes(contentType: string): ContentType[] {
3728
return contentType
3829
.split(',')
3930
.map(ct => ct.trim())
40-
.map(ct => {
41-
try {
42-
return parseContentType(ct);
43-
} catch (error: any) {
44-
throw new ErrorWithCause(
45-
`Invalid content type '${ct}' for property '${propName}' in OpenAPI specification. ` +
46-
"Content types must follow the format 'type/subtype' (e.g., 'image/png', 'text/plain'). " +
47-
'Please fix your OpenAPI document.',
48-
error
49-
);
50-
}
51-
});
31+
.map(ct => parseContentType(ct));
5232
}
5333

5434
/**
@@ -128,7 +108,7 @@ function inferMultipartFormEncodings(
128108
{
129109
contentType,
130110
isImplicit: true,
131-
parsedContentTypes: parseContentTypes(contentType, propName)
111+
parsedContentTypes: parseContentTypes(contentType)
132112
}
133113
];
134114
})
@@ -158,10 +138,7 @@ function parseMultipartFormEncodings(
158138
{
159139
contentType: encodingObj.contentType!,
160140
isImplicit: false,
161-
parsedContentTypes: parseContentTypes(
162-
encodingObj.contentType!,
163-
propName
164-
)
141+
parsedContentTypes: parseContentTypes(encodingObj.contentType!)
165142
}
166143
])
167144
)

0 commit comments

Comments
 (0)