Skip to content

Commit

Permalink
fix: fix the type parameter issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKdhim87 committed Nov 15, 2024
1 parent bbeadfb commit 45a8a7d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 29 deletions.
4 changes: 3 additions & 1 deletion apps/overige-objecten-api/src/controllers/objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export const getAllObjectsController: RequestHandler = async (req, res, next) =>
if (type.endsWith('kennisartikel')) return res.status(200).json({ ...pagination, results: kennisartikel });

if (type.endsWith('vac')) return res.status(200).json({ results: vac });

// If no specific type, return empty array
if (!!type && !type.endsWith('kennisartikel') && !type.endsWith('vac'))
return res.status(200).json({ ...pagination, results: [] });
// If no specific type, return both kennisartikel and vac objects
if (vac.length > 0 && kennisartikel.length > 0)
return res.status(200).json({ ...pagination, results: [...kennisartikel, ...vac] });
Expand Down
17 changes: 0 additions & 17 deletions apps/overige-objecten-api/src/controllers/openapi/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,4 @@ describe('openAPIController', () => {
expect(response.text).toEqual(JSON.stringify({ message: 'An unexpected error occurred.' }));
spy.mockRestore();
});
it('GET api/v2/openapi.json return 200 and json with updated type parameter schema enum contains the correct URLs', async () => {
const spy = jest
.spyOn(require('../../utils/getTheServerURL'), 'getTheServerURL')
.mockImplementation(() => 'https://example.com/');
const response = await request(app).get('/api/v2/openapi.json');
const body = response.body as OpenAPI;
expect(response.status).toBe(200);
expect(body).toBeDefined();
expect(body.paths['/objects'].get.parameters).toBeDefined();
const typeParameter = body.paths['/objects'].get.parameters.find((parameter) => parameter.name === 'type');
expect(typeParameter).toBeDefined();
expect(typeParameter?.schema.enum).toEqual([
'https://example.com/api/v2/objecttypes/kennisartikel',
'https://example.com/api/v2/objecttypes/vac',
]);
spy.mockRestore();
});
});
8 changes: 0 additions & 8 deletions apps/overige-objecten-api/src/controllers/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { getTheServerURL, readFile } from '../../utils';
export const openAPIController: RequestHandler = async (req, res, next) => {
try {
const url = new URL('api/v2', getTheServerURL(req)).href;
const OBJECT_TYPE_ENUM = [`${url}/objecttypes/kennisartikel`, `${url}/objecttypes/vac`];
const OPEN_API_YAML = readFile(path.join(__dirname, '../../docs/openapi.yaml'));

if (!OPEN_API_YAML) throw new Error('openapi.yaml file not found');
Expand All @@ -19,13 +18,6 @@ export const openAPIController: RequestHandler = async (req, res, next) => {
url,
description: server.description,
}));
// Update enum for specific endpoint parameter
const typeParameter = openAPIDocument.paths['/objects']?.get?.parameters?.find(
(parameter) => parameter.name === 'type',
);
if (typeParameter?.schema) {
typeParameter.schema.enum = OBJECT_TYPE_ENUM;
}

res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
Expand Down
4 changes: 1 addition & 3 deletions apps/overige-objecten-api/src/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ paths:
schema:
type: string
format: uri
enum:
- "http://localhost:4001/api/v2/objecttypes/kennisartikel"
- "http://localhost:4001/api/v2/objecttypes/vac"
description: Optional parameter to filter by object type. If not specified, both types will be returned.
example: "http://localhost:4001/api/v2/objecttypes/kennisartikel"
- in: query
name: page
required: false
Expand Down

0 comments on commit 45a8a7d

Please sign in to comment.