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

Any plans to support OpenSpec 3.0? #29

Open
bengro opened this issue Aug 17, 2020 · 4 comments
Open

Any plans to support OpenSpec 3.0? #29

bengro opened this issue Aug 17, 2020 · 4 comments

Comments

@bengro
Copy link

bengro commented Aug 17, 2020

We're migrating from 2.0 to 3.0 and we really like this plugin. Are there any plans to support OpenApi Specification 3.0?

@devdigital
Copy link
Owner

Hi, thanks, that's been a plan for a while now, but currently struggling for time, so I would imagine it would be a couple of months away at least unfortunately.

@jhodges10
Copy link

+1 on support for OAS 3.0!

@jhodges10
Copy link

jhodges10 commented Oct 16, 2020

Figured out a solution for the meantime @bengro!

npm install -g api-spec-converter

Then in your package.json, add this task:

"transform-docs": "api-spec-converter --from=openapi_3 --to=swagger_2 --syntax=json --order=alpha [Your OAS spec URL] > ./src/data/swagger.json"

Then also add this before your dev / build phases:

    "dev": "yarn transform-docs && gatsby develop",
    "build": "yarn transform-docs && gatsby build",

@bengro
Copy link
Author

bengro commented Oct 20, 2020

Nice one, @jhodges10 👍 !

I wrote my own gatsby plugin because we rely on OpenApi Spec 3.0 features (hence transforming to OpenApi Spec 2.0 wasn't an option). Unfortunately I cannot share the code as it's closed source, but here the gist for peeps in a similar situation.

The hardest part was building the GraphQL model correctly, which is shown here:

import { parseOpenApi3 } from "./api-parser/openapi3parser"
import { ApiOperation } from "./api-parser/types/ApiOperation"

export interface Options {
  specs: string[]
}

exports.sourceNodes = async ({ actions }: any, options: Options) => {
  const allOperations: ApiOperation[] = []

  for (let spec of options.specs) {
    const operations = await parseOpenApi3(spec)

    for (let operation of operations) {
      allOperations.push(operation)
    }
  }

  allOperations.forEach((operation: ApiOperation) => {
    actions.createNode({
      id: operation.path, // assigning a unique id through helper function
      children: [], // no children at this point
      parent: undefined, // no parent
      internal: {
        type: "ApiOperation", // A globally unique name for this node type
        contentDigest: toHashCode(JSON.stringify(operation)),
      },
      ...operation,
    })
  })
}

const toHashCode = (input: string): string => {
  let h: number = 0

  for (let i = 0; i < input.length; i++) {
    h = (Math.imul(31, h) + input.charCodeAt(i)) | 0
  }

  return h.toString()
}

The parsing of the spec is delegating to @apidevtools/swagger-parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants