Skip to content

Commit

Permalink
Merge pull request #24 from sinasab/sina/adding-schema-validity-test
Browse files Browse the repository at this point in the history
Adding schema validity test
  • Loading branch information
SaltyAom committed Oct 27, 2023
2 parents f666664 + ef78657 commit 815a1aa
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 37 deletions.
40 changes: 18 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { swagger } from '@elysiajs/swagger'

const app = new Elysia()
.use(swagger())
.get('/', () => 'hi')
.get('/unpath/:id', ({ params: { id } }) => id)
.get('/unpath/:id/:name', ({ params: { id, name } }) => `${id} ${name}`)
.get('/', () => 'hi', { response: t.String({ description: 'sample description' }) })
.post(
'/json/:id',
({ body, params: { id }, query: { name } }) => ({
Expand All @@ -24,27 +22,25 @@ const app = new Elysia()
name
}),
{
schema: {
params: t.Object({
id: t.String()
}),
query: t.Object({
name: t.String()
}),
body: t.Object({
username: t.String(),
password: t.String()
}),
response: t.Object({
username: t.String(),
password: t.String(),
id: t.String(),
name: t.String()
})
}
params: t.Object({
id: t.String()
}),
query: t.Object({
name: t.String()
}),
body: t.Object({
username: t.String(),
password: t.String()
}),
response: t.Object({
username: t.String(),
password: t.String(),
id: t.String(),
name: t.String()
}, { description: 'sample description' })
}
)
.listen(8080)
.listen(8080);
```

Then go to `http://localhost:8080/swagger`.
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"elysia": ">= 0.7.0"
},
"devDependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@types/node": "^20.1.4",
"bun-types": "^0.7.0",
"elysia": "0.7.10",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const swagger =
method: route.method,
path: route.path,
// @ts-ignore
models: app.definitions.type,
models: app.definitions?.type,
contentType: route.hooks.type
})
})
Expand All @@ -161,7 +161,7 @@ export const swagger =
...documentation.components,
schemas: {
// @ts-ignore
...app.definitions.type,
...app.definitions?.type,
...documentation.components?.schemas
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ export const mapProperties = (
if (schema in models) schema = models[schema]
else throw new Error(`Can't find model ${schema}`)

return Object.entries(schema?.properties ?? []).map(([key, value]) => ({
// @ts-ignore
schema: {
return Object.entries(schema?.properties ?? []).map(([key, value]) => {
const { type: valueType = undefined, ...rest } = value as any;
return {
// @ts-ignore
...value,
...rest,
schema: { type: valueType },
in: name,
name: key,
// @ts-ignore
type: value?.type,
},
in: name,
name: key,
// @ts-ignore
required: schema!.required?.includes(key) ?? false
}))
required: schema!.required?.includes(key) ?? false,
};
});
}

const mapTypesResponse = (
Expand Down Expand Up @@ -121,7 +120,7 @@ export const registerSchemaPath = ({

if (typeof responseSchema === 'object') {
if (Kind in responseSchema) {
const { type, properties, required, additionalProperties: _, ...rest } =
const { type, properties, required, additionalProperties, ...rest } =
responseSchema as typeof responseSchema & {
type: string
properties: Object
Expand Down Expand Up @@ -165,7 +164,7 @@ export const registerSchemaPath = ({
content: mapTypesResponse(contentTypes, value)
}
} else {
const { type, properties, required, additionalProperties: _, ...rest } =
const { type, properties, required, additionalProperties, ...rest } =
value as typeof value & {
type: string
properties: Object
Expand Down Expand Up @@ -290,6 +289,7 @@ export const filterPaths = (
schema: { type: 'string' },
in: 'path',
name: x.slice(1, x.length - 1),
schema: { type: "string" },
required: true
})),
...schema.parameters
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Elysia, t } from 'elysia'
import SwaggerParser from '@apidevtools/swagger-parser';
import { swagger } from '../src'

import { describe, expect, it } from 'bun:test'
import { fail } from 'assert';

const req = (path: string) => new Request(`http://localhost${path}`)

Expand All @@ -13,6 +15,13 @@ describe('Swagger', () => {
expect(res.status).toBe(200)
})

it('returns a valid Swagger/OpenAPI json config', async () => {
const app = new Elysia().use(swagger())
const res = await app.handle(req('/swagger/json')).then((x) => x.json());
expect(res.openapi).toBe("3.0.3");
await SwaggerParser.validate(res).catch((err) => fail(err));
});

it('use custom Swagger version', async () => {
const app = new Elysia().use(
swagger({
Expand Down
45 changes: 45 additions & 0 deletions test/validateSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Elysia, t } from 'elysia'
import SwaggerParser from '@apidevtools/swagger-parser';
import { swagger } from '../src'

import { describe, expect, it } from 'bun:test'
import { fail } from 'assert';

const req = (path: string) => new Request(`http://localhost${path}`)

it('returns a valid Swagger/OpenAPI json config for many routes', async () => {
const app = new Elysia()
.use(swagger())
.get('/', () => 'hi', { response: t.String({ description: 'sample description' }) })
.get('/unpath/:id', ({ params: { id } }) => id, { response: t.String({ description: 'sample description' }) })
.get('/unpath/:id/:name/:age', ({ params: { id, name } }) => `${id} ${name}`,
{ type: "json", response: t.String({ description: 'sample description' }), params: t.Object({ id: t.String(), name: t.String() }) })
.post(
'/json/:id',
({ body, params: { id }, query: { name } }) => ({
...body,
id,
name
}),
{
params: t.Object({
id: t.String()
}),
query: t.Object({
name: t.String()
}),
body: t.Object({
username: t.String(),
password: t.String()
}),
response: t.Object({
username: t.String(),
password: t.String(),
id: t.String(),
name: t.String()
}, { description: 'sample description 3' })
}
);
const res = await app.handle(req('/swagger/json')).then((x) => x.json());
await SwaggerParser.validate(res).catch((err) => fail(err));
});

0 comments on commit 815a1aa

Please sign in to comment.