Skip to content

Commit

Permalink
test(open_api3.1): ensure that methods with non-explicit semantics al…
Browse files Browse the repository at this point in the history
…low request body
  • Loading branch information
SF97 committed Mar 31, 2024
1 parent 15d9049 commit 264f83f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/openapi_3.1/get_with_request_body.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as request from 'supertest';
import * as express from 'express';
import { createApp } from "../common/app";
import { join } from "path";

describe('type null support - OpenAPI 3.1', () => {
let app;

before(async () => {
const apiSpec = join('test', 'openapi_3.1', 'resources', 'get_with_request_body.yaml');
app = await createApp(
{ apiSpec, validateRequests: true, validateResponses: true },
3005,
(app) => app.use(
express
.Router()
.get(`/v1/entity`, (req, res) =>
res.status(200).json({
property: null
}),
),
)
);
});

after(() => {
app.server.close();
});

// In OpenAPI 3.0, methods that RFC7231 does not have explicitly defined semantics for request body (GET, HEAD, DELETE) do not allow request body
// In OpenAPI 3.1, request body is allowed for these methods. This test ensures that it is correctly handled
it('should support an API with types set to null', async () => {
return request(app)
.get(`${app.basePath}/entity`)
.set('Content-Type', 'application/json')
.send({request: 123})
.expect(400);
});

})
34 changes: 34 additions & 0 deletions test/openapi_3.1/resources/get_with_request_body.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.1.0
info:
title: API
version: 1.0.0
servers:
- url: /v1
paths:
/entity:
get:
summary: test
description: GETS my entity
requestBody:
description: Request body for entity
required: true
content:
application/json:
schema:
title: EntityRequest
type: object
properties:
request:
type: ['string']
responses:
'200':
description: OK
content:
application/json:
schema:
title: Entity
type: object
properties:
property:
type: ['string', 'null']

0 comments on commit 264f83f

Please sign in to comment.