From 4029114b227b35594915a4d828fc61ef754e5e35 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Thu, 15 Sep 2022 12:22:21 +0200 Subject: [PATCH] Fix(#1391): Expose API version even when not running via npm/yarn (#2062) * Fix(#1391): Expose API version even when not running via npm/yarn Expose the current Unleash version in the generated OpenAPI docs, even when running via docker or other processes. An OpenAPI spec without a version isn't valid. This causes some of our generation tools (such as the one for documentation) to fail. By changing how we fetch the current version: Previously, we used `process.env.npm_package_version!`. However, when you're not running with yarn or npm, this is `undefined`. That causes the version number to not be included when running tests and when running using the official docker image. Instead, we now use `version` from `lib/util/version`. This is the same version as the one used by the UI config endpoint, so it should be the same one as what the front end displays. To the best of my knowledge, this _is_ the version of the API. It _may_ be that I have misunderstood what the version represents, but from what I can tell, it just exports what's listed as the version in package.json. The source code of the `lib/util/version` file is: ```ts // export module version require('pkginfo')(module, 'version'); const { version } = module.exports; export default version; module.exports = version; ``` * Refactor(#1391): rename imported variable for clarity --- src/lib/openapi/index.ts | 3 ++- .../e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap | 1 + src/test/e2e/api/openapi/openapi.e2e.test.ts | 2 -- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index b13142a4a5..3ca6f94b46 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -113,6 +113,7 @@ import { proxyMetricsSchema } from './spec/proxy-metrics-schema'; import { setUiConfigSchema } from './spec/set-ui-config-schema'; import { edgeTokenSchema } from './spec/edge-token-schema'; import { validateEdgeTokensSchema } from './spec/validate-edge-tokens-schema'; +import apiVersion from '../util/version'; // All schemas in `openapi/spec` should be listed here. export const schemas = { @@ -273,7 +274,7 @@ export const createOpenApiSchema = ({ servers: url ? [{ url }] : [], info: { title: 'Unleash API', - version: process.env.npm_package_version!, + version: apiVersion, }, security: [{ apiKey: [] }], components: { diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index ed1dacfc77..64d8a8b4d4 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -3316,6 +3316,7 @@ exports[`should serve the OpenAPI spec 1`] = ` }, "info": { "title": "Unleash API", + "version": "4.15.1", }, "openapi": "3.0.3", "paths": { diff --git a/src/test/e2e/api/openapi/openapi.e2e.test.ts b/src/test/e2e/api/openapi/openapi.e2e.test.ts index 6378b59fe2..97391f4d65 100644 --- a/src/test/e2e/api/openapi/openapi.e2e.test.ts +++ b/src/test/e2e/api/openapi/openapi.e2e.test.ts @@ -30,8 +30,6 @@ test('should serve the OpenAPI spec', async () => { .expect('Content-Type', /json/) .expect(200) .expect((res) => { - // The version field is not set when running jest without yarn/npm. - delete res.body.info.version; // This test will fail whenever there's a change to the API spec. // If the change is intended, update the snapshot with `jest -u`. expect(res.body).toMatchSnapshot();