Skip to content

Commit

Permalink
feat: Support parsing media type application/merge-patch+json. (#4833)
Browse files Browse the repository at this point in the history
* add support for top level media types

* lint fix and changelog

* limit to json type

* undo renaming

* support 2 allowed types

* update changelog

* update changelog

* change log message

* Update .changeset/afraid-timers-fail.md

Co-authored-by: KavithaSiva <[email protected]>

* Changes from lint:fix

* refactor

* chore:rename variable

---------

Co-authored-by: KavithaSiva <[email protected]>
Co-authored-by: cloud-sdk-js <[email protected]>
Co-authored-by: Kavitha <[email protected]>
  • Loading branch information
4 people authored Jul 25, 2024
1 parent 586e77e commit e9a243a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-timers-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/openapi-generator': minor
---

[Improvement] Generated OpenAPI clients support `application/merge-patch+json` media type for patch requests.
47 changes: 45 additions & 2 deletions packages/openapi-generator/src/parser/media-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseApplicationJsonMediaType, parseMediaType } from './media-type';

const defaultOptions = { strictNaming: true };
describe('parseApplicationJsonMediaType', () => {
it('returns undefined if there is no application/json media type', async () => {
it('returns undefined if the media type is not supported', async () => {
expect(
parseApplicationJsonMediaType(
{
Expand All @@ -15,7 +15,7 @@ describe('parseApplicationJsonMediaType', () => {
).toBeUndefined();
});

it('returns parsed media type for application/json', async () => {
it('returns parsed media type for supported media type application/json', async () => {
expect(
parseApplicationJsonMediaType(
{
Expand All @@ -26,6 +26,20 @@ describe('parseApplicationJsonMediaType', () => {
)
).toEqual(emptyObjectSchema);
});

it('returns parsed media type for supported media type application/merge-patch+json', async () => {
expect(
parseApplicationJsonMediaType(
{
content: {
'application/merge-patch+json': { schema: { type: 'object' } }
}
},
await createTestRefs(),
defaultOptions
)
).toEqual(emptyObjectSchema);
});
});

describe('parseMediaType', () => {
Expand Down Expand Up @@ -59,6 +73,35 @@ describe('parseMediaType', () => {
).toEqual(emptyObjectSchema);
});

it('returns parsed media type if there is only application/merge-patch+json', async () => {
expect(
parseMediaType(
{
content: {
'application/merge-patch+json': { schema: { type: 'object' } }
}
},
await createTestRefs(),
defaultOptions
)
).toEqual(emptyObjectSchema);
});

it('returns parsed media type if there is both application/json and application/merge-patch+json', async () => {
expect(
parseMediaType(
{
content: {
'application/json': { schema: { type: 'object' } },
'application/merge-patch+json': { schema: { type: 'object' } }
}
},
await createTestRefs(),
defaultOptions
)
).toEqual(emptyObjectSchema);
});

it('returns anyOf schema if there are other schemas and application/json', async () => {
expect(
parseMediaType(
Expand Down
22 changes: 13 additions & 9 deletions packages/openapi-generator/src/parser/media-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { parseSchema } from './schema';
import { ParserOptions } from './options';

const logger = createLogger('openapi-generator');
const allowedJsonMediaTypes = [
'application/json',
'application/merge-patch+json'
];
/**
* Parse the type of a resolved request body or response object.
* @param bodyOrResponseObject - The request body or response object to parse the type from.
Expand All @@ -25,7 +29,7 @@ export function parseApplicationJsonMediaType(
if (bodyOrResponseObject) {
const mediaType = getMediaTypeObject(
bodyOrResponseObject,
'application/json'
allowedJsonMediaTypes
);
const schema = mediaType?.schema;
if (schema) {
Expand Down Expand Up @@ -55,19 +59,19 @@ export function parseMediaType(

if (!jsonMediaType) {
logger.warn(
"Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime."
`Could not parse '${allMediaTypes}', because it is not supported. Generation will continue with 'any'. This might lead to errors at runtime.`
);
return { type: 'any' };
}

// There is only the application/json media type
// There is only one media type
if (allMediaTypes.length === 1) {
return jsonMediaType;
}

return {
anyOf: [jsonMediaType, { type: 'any' }]
};
return allMediaTypes.every(type => allowedJsonMediaTypes.includes(type))
? jsonMediaType
: { anyOf: [jsonMediaType, { type: 'any' }] };
}
}
/**
Expand All @@ -93,11 +97,11 @@ function getMediaTypeObject(
| OpenAPIV3.RequestBodyObject
| OpenAPIV3.ResponseObject
| undefined,
contentType: string
contentType: string[]
): OpenAPIV3.MediaTypeObject | undefined {
if (bodyOrResponseObject?.content) {
return Object.entries(bodyOrResponseObject.content).find(
([key]) => key === contentType
return Object.entries(bodyOrResponseObject.content).find(([key]) =>
contentType.includes(key)
)?.[1];
}
return undefined;
Expand Down

0 comments on commit e9a243a

Please sign in to comment.