Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

feat: add a migration layer for @apidevtools/swagger-parser #140

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add dereference to the migration layer
hanspagel committed Aug 27, 2024

Verified

This commit was signed with the committer’s verified signature.
hanspagel Hans Pagel
commit b8aadaabe7eafdd88868256eb67fcc3c3e1097a2
47 changes: 39 additions & 8 deletions packages/openapi-parser/tests/migration-layer.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import OriginalSwaggerParser from '@apidevtools/swagger-parser'
import { describe, expect, it } from 'vitest'

import { dereference } from '../src/utils/dereference'
import { validate } from '../src/utils/validate'

const myAPI = `{
"openapi": "3.1.0",
"info": {
"title": "Hello World",
"version": "1.0.0"
const myAPI = JSON.stringify({
openapi: '3.1.0',
info: {
title: 'Hello World',
version: '1.0.0',
},
paths: {
'/foobar': {
post: {
requestBody: {
$ref: '#/components/requestBodies/Foobar',
},
},
},
"paths": {}
}`
},
components: {
requestBodies: {
Foobar: {
content: {},
},
},
},
})

class SwaggerParser {
static async validate(api: string, callback: (err: any, api: any) => void) {
@@ -24,10 +40,14 @@ class SwaggerParser {
callback(error, null)
})
}

static async dereference(api: string) {
return dereference(api).then((result) => result.schema)
}
}

// https://github.com/APIDevTools/swagger-parser?tab=readme-ov-file#example
describe('migration-layer', async () => {
describe('validate', async () => {
it('validates', async () => {
return new Promise((resolve, reject) => {
SwaggerParser.validate(myAPI, (err, api) => {
@@ -55,3 +75,14 @@ describe('migration-layer', async () => {
})
})
})

// https://apitools.dev/swagger-parser/docs/swagger-parser.html#dereferenceapi-options-callback
describe('dereference', () => {
it('dereferences', async () => {
let api = await SwaggerParser.dereference(myAPI)

// The `api` object is a normal JavaScript object,
// so you can easily access any part of the API using simple dot notation
expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({})
})
})