Skip to content

Commit

Permalink
Resolve Issue #5724: parse all content types ending with +json as jso…
Browse files Browse the repository at this point in the history
…n in rest data source (#5737)

Co-authored-by: David Glasser <glasser@davidglasser.net>
  • Loading branch information
nholik and glasser committed Sep 29, 2021
1 parent 5f8e586 commit e199acc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
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

0 comments on commit e199acc

Please sign in to comment.