Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Issue #5724: parse all content types ending with +json as json in rest data source #5737

Merged
merged 2 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The version headers in this history reflect the versions of Apollo Server itself
- `apollo-server-koa`: The peer dependency on `koa` (added in v3.0.0) should be a `^` range dependency rather than depending on exactly one version, and it should not be automatically increased when new versions of `koa` are released. [PR #5759](https://github.com/apollographql/apollo-server/pull/5759)
- `apollo-server-fastify`: Export `ApolloServerFastifyConfig` and `FastifyContext` TypeScript types. [PR #5743](https://github.com/apollographql/apollo-server/pull/5743)
- `apollo-server-core`: Only generate the schema hash once on startup rather than twice. [PR #5757](https://github.com/apollographql/apollo-server/pull/5757)
- `apollo-datasource-rest@3.2.1`: When choosing whether or not to parse a response as JSON, treat any `content-type` ending in `+json` as JSON rather than just `application/hal+json` (in addition to `application/json`). [PR #5737](https://github.com/apollographql/apollo-server/pull/5737)

## v3.3.0

Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
contentLength !== '0' &&
contentType &&
(contentType.startsWith('application/json') ||
contentType.startsWith('application/hal+json'))
contentType.endsWith('+json'))
) {
return response.json();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,27 @@ describe('RESTDataSource', () => {
expect(data).toEqual({ foo: 'bar' });
});

it('returns data as parsed JSON when Content-Type ends in +json', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';

getFoo() {
return this.get('foo');
}
})();

dataSource.httpCache = httpCache;

fetch.mockJSONResponseOnce(
{ foo: 'bar' },
{ 'Content-Type': 'application/vnd.api+json' },
);

const data = await dataSource.getFoo();

expect(data).toEqual({ foo: 'bar' });
});

it('returns data as a string when Content-Type is text/plain', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';
Expand Down