Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/openapi-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@sap-cloud-sdk/generator-common": "workspace:^",
"@sap-cloud-sdk/openapi": "workspace:^",
"@sap-cloud-sdk/util": "workspace:^",
"content-type": "^1.0.5",
"content-type": "^2.0.0",
"js-yaml": "^4.1.1",
"openapi-types": "^12.1.3",
"swagger2openapi": "^7.0.4"
Expand All @@ -53,7 +53,7 @@
"@apidevtools/json-schema-ref-parser": "^14.1.1",
"@jest/globals": "^30.3.0",
"@sap-cloud-sdk/test-util-internal": "workspace:^",
"@types/content-type": "^1.1.9",
"@types/content-type": "^2.0.0",
"@types/js-yaml": "^4.0.9",
"depcheck": "^1.4.7",
"memfs": "^4.57.2",
Expand Down
44 changes: 24 additions & 20 deletions packages/openapi-generator/src/parser/media-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,21 +475,23 @@ describe('parseTopLevelMediaType', () => {
});
});

it('throws error with malformed content type', async () => {
it('parses malformed content type leniently (strips bad params)', async () => {
const schema = {
type: 'object',
properties: { file: { type: 'string', format: 'binary' } }
};
const encoding = { file: { contentType: 'image/png;;invalid' } };
const refs = await createTestRefs();
const result = parseTopLevelMediaType(
createMultipartFormContent(schema, encoding),
await createTestRefs(),
defaultOptions
);

expect(() =>
parseTopLevelMediaType(
createMultipartFormContent(schema, encoding),
refs,
defaultOptions
)
).toThrow(/invalid content type.*image\/png;;invalid.*file/i);
expect(result?.encoding?.file).toEqual({
contentType: 'image/png;;invalid',
isImplicit: false,
parsedContentTypes: [{ type: 'image/png', parameters: {} }]
});
});

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

it('throws error with completely invalid content type format', async () => {
it('parses completely invalid content type leniently', async () => {
const schema = {
type: 'object',
properties: { attachment: { type: 'string', format: 'binary' } }
};
const encoding = {
attachment: { contentType: 'not-a-valid-content-type-at-all' }
};
const refs = await createTestRefs();

expect(() =>
parseTopLevelMediaType(
createMultipartFormContent(schema, encoding),
refs,
defaultOptions
)
).toThrow(
/invalid content type.*not-a-valid-content-type-at-all.*attachment/i
const result = parseTopLevelMediaType(
createMultipartFormContent(schema, encoding),
await createTestRefs(),
defaultOptions
);

expect(result?.encoding?.attachment).toEqual({
contentType: 'not-a-valid-content-type-at-all',
isImplicit: false,
parsedContentTypes: [
{ type: 'not-a-valid-content-type-at-all', parameters: {} }
]
});
});
});

Expand Down
37 changes: 7 additions & 30 deletions packages/openapi-generator/src/parser/media-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createLogger, ErrorWithCause } from '@sap-cloud-sdk/util';
import { parse as parseContentType, type ParsedMediaType } from 'content-type';
import { createLogger } from '@sap-cloud-sdk/util';
import { parse as parseContentType, type ContentType } from 'content-type';
import { parseSchema } from './schema';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenApiMediaTypeObject, OpenApiSchema } from '../openapi-types';
Expand All @@ -19,36 +19,16 @@ const allowedMediaTypes = [
interface EncodingInfo {
contentType: string;
isImplicit: boolean;
parsedContentTypes: ParsedMediaType[];
parsedContentTypes: ContentType[];
}

type EncodingMap = Record<string, EncodingInfo>;

/**
* Parse content types from a comma-separated content type string.
* @param contentType - Comma-separated content types from encoding object.
* @param propName - Property name for error messages.
* @returns Array of parsed content types.
*/
function parseContentTypes(
contentType: string,
propName: string
): ParsedMediaType[] {
function parseContentTypes(contentType: string): ContentType[] {
return contentType
.split(',')
.map(ct => ct.trim())
.map(ct => {
try {
return parseContentType(ct);
} catch (error: any) {
throw new ErrorWithCause(
`Invalid content type '${ct}' for property '${propName}' in OpenAPI specification. ` +
"Content types must follow the format 'type/subtype' (e.g., 'image/png', 'text/plain'). " +
'Please fix your OpenAPI document.',
error
);
}
});
.map(ct => parseContentType(ct));
}

/**
Expand Down Expand Up @@ -128,7 +108,7 @@ function inferMultipartFormEncodings(
{
contentType,
isImplicit: true,
parsedContentTypes: parseContentTypes(contentType, propName)
parsedContentTypes: parseContentTypes(contentType)
}
];
})
Expand Down Expand Up @@ -158,10 +138,7 @@ function parseMultipartFormEncodings(
{
contentType: encodingObj.contentType!,
isImplicit: false,
parsedContentTypes: parseContentTypes(
encodingObj.contentType!,
propName
)
parsedContentTypes: parseContentTypes(encodingObj.contentType!)
}
])
)
Expand Down
23 changes: 16 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export const TestCaseApi = {
stringProperty: {
contentType: 'text/plain',
isImplicit: true,
parsedContentTypes: [{ parameters: {}, type: 'text/plain' }]
parsedContentTypes: [{ type: 'text/plain', parameters: {} }]
}
},
headerParameters: { 'content-type': 'multipart/form-data' }
Expand All @@ -306,7 +306,7 @@ export const TestCaseApi = {
stringProperty: {
contentType: 'text/plain',
isImplicit: true,
parsedContentTypes: [{ parameters: {}, type: 'text/plain' }]
parsedContentTypes: [{ type: 'text/plain', parameters: {} }]
}
},
headerParameters: {
Expand Down
Loading