openapi-to-graphql icon indicating copy to clipboard operation
openapi-to-graphql copied to clipboard

should have a content-type 'application/json' but has 'application/vnd.api+json'

Open pontusvision opened this issue 3 years ago • 0 comments

Describe the bug We're trying to expose a UK gov API with the following spec: https://api.publish-teacher-training-courses.service.gov.uk/api-docs/public_v1/api_spec.json

Unfortunately, they decided to implement it using the content type application/vnd.api+json.

Currently, the resolver code in this project is only ever treating application/json as a first-class citizen; after changing the spec to have a content type of application/vnd.api+json, the number of entries that appear in the query section reduce signficantly. Please start treating application/vnd.api+json and application/json as synonyms.

To Reproduce Steps to reproduce the behavior:

  1. Download this file
  2. Globally Replace 'float' with number, and save it as file spec.json
  3. create the following index.js app:
const express = require('express')
const {graphqlHTTP} = require('express-graphql')
const {createGraphQLSchema} = require('openapi-to-graphql')
const oas = require('./spec.json');

async function main(oas) {
  // generate schema:
  const {schema, report} = await createGraphQLSchema(oas, {
    strict: false
  })

  // server schema:
  const app = express()
  app.use(
    '/graphql',
    graphqlHTTP({
      schema,
      graphiql: true,


    })
  )
  app.listen(3001)
}

main(oas)


  1. create the following package.json file:
{
  "name": "gql",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "express-graphql": "^0.12.0",
    "graphql": "^15.8.0",
    "openapi-to-graphql": "^2.6.3"
  }
}

  1. Run the following:
npm install 
node index.js
  1. Point a browser to http://localhost:3001/graphql
  2. Run the following Query:
query{
  courseLocationListResponse (courseCode:"2N22",providerCode:"B20", year:"2020"){
    jsonapi{
      version
    }
  }
}

The response we get is the following:

{
  "errors": [
    {
      "message": "Operation GET /recruitment_cycles/{year}/providers/{provider_code}/courses/{course_code}/locations should have a content-type 'application/json' but has 'application/vnd.api+json' instead",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "courseLocationListResponse"
      ]
    }
  ],
  "data": {
    "courseLocationListResponse": null
  }
}

Expected behavior I'd expect the app to understand that application/json and application/vnd.api+json are compatible, and return the correct results.

Screenshots N/A.

Additional context Changing the spec.json file to use ''application/vnd.api+json" instead of "application/json" causes erratic behaviour, as the API does not understand this mime type.

The issue is likely to be resolved by loosening the if statement here to also treat ''application/vnd.api+json" in the same way: https://github.com/IBM/openapi-to-graphql/blob/master/packages/openapi-to-graphql/src/resolver_builder.ts#L809

pontusvision avatar Oct 26 '22 14:10 pontusvision