1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/openapi/spec/create-api-token-schema.ts

57 lines
1.6 KiB
TypeScript
Raw Normal View History

import { FromSchema } from 'json-schema-to-ts';
import { ApiTokenType } from '../../types/models/api-token';
openapi: improve validation testing (#2058) ## What This PR adds an extra layer of OpenAPI validation testing to what we already have. It also fixes any issues that make the new tests fail. ## Why While the current OpenAPI validation takes care of _some_ things, there's also things it misses, as shown by #2055. By adding the OpenAPI Enforcer package, we should hopefully be able to catch more of these errors in the future. The enforcer _does_ flag the issue in #2055 as an error. ## How By adding the OpenAPI Enforcer package and making whatever changes it picks up on. By adding location headers to all our 201 endpoints. I also had to change some signatures on `create` store methods so that they actually return something (a lot of them just returned `void`). ## Discussion points ### Code changes Some of the code changes may not be necessary or we may want to change more code to align with what changes have been done. It may be worth standardizing on a pattern for `*store.create` methods, so that they always return an identifier or the stored object, for instance. ### On relative URIs The 201 location headers use relative URIs to point to the created resources. This seemed to be the easiest way to me, as we don't need to worry about figuring out what the absolute URL of the instance is (though we could probably just concat it to the request URI?). The algorithm for determining relative URIs is described in [RFC 3986 section 5](https://www.rfc-editor.org/rfc/rfc3986#section-5). There's also some places where I'm not sure we _can_ provide accurate location url. I think they're supposed to point _directly at_ whatever the resource is, but for some resources (such as api tokens), you can only get it as part of a collection. From [RFC 9110 on the location field](https://httpwg.org/specs/rfc9110.html#field.location) (emphasis mine): > the Location header field in a [201 (Created)](https://httpwg.org/specs/rfc9110.html#status.201) response is supposed to provide a URI that is **specific** to the created resource. A link to a collection is not specific. I'm not sure what best to do about this. ### Inline comments I've added a number of inline PR comments that I'd love to get some feedback on too. Have a look and let me know what you think! ### Unfinished business I've added some juicy comments to some of the files here. They contain non-blocking issues that I'm tracking (via github issues). We should resolve them in the future, but right now they can stay as they are. ## Commits * Feat: add openapi-enforcer + tests; fix _some_ issues * Test: allow non-standard string formats * validation: fix _some_ 201 created location header endpoints * #1391: fix remaining 201 location headers missing * Refactor: use the ajv options object instead of add* methods * #1391: flag validation errors and warnings as test failures * #1391: modify patch schema to specify either object or array We don't provide many patch endpoints, so we _could_ create separate patch operation objects for each one. I think that makes sense to do as part of the larger cleanup. For now, I think it's worth to simply turn it into one of these. While it's not entirely accurate, it's better than what we had before. * Refactor: make tests easier to read * #1391: use enum for valid token types This was previously only a description. This may seem like a breaking change because OpenAPI would previously accept any string. However, Joi also performs validation on this, so invalid values wouldn't work previously either. * #1391: Comment out default parameter values for now This isn't the _right_ way, but it's the pragmatic solution. It's not a big deal and this works as a stopgap solution. * #1391: add todo note for api token schema fixes * #1391: update snapshot * Revert "#1391: modify patch schema to specify either object or array" This reverts commit 0dd5d0faa1554a394178c0f632915e096204cac7. Turns out we need to allow much more than just objects and arrays. I'll leave this as is for now. * Chore(#1391): update comment explaining api token schema TODO * #1391: modify some test code and add comment * #1391: update tests and spec to allow 'any' type in schema * Chore: remove comment * #1391: add tests for context field stores * #1391: add location header for public signup links * #1391: fix query parameter description. There was indeed a bug in the package, and this has been addressed now, so we can go back to describing the params properly.
2022-09-23 15:02:09 +02:00
// TODO: (openapi) this schema isn't entirely correct: `project` and `projects`
// are mutually exclusive.
//
// That is, when creating a token, you can provide either `project` _or_
// `projects`, but *not* both.
//
// We should be able to annotate this using `oneOf` and `allOf`, but making
// `oneOf` only valid for _either_ `project` _or_ `projects` is tricky.
//
// I've opened an issue to get some help (thought it was a bug initially).
// There's more info available at:
//
// https://github.com/ajv-validator/ajv/issues/2096
//
// This also applies to apiTokenSchema and potentially other related schemas.
export const createApiTokenSchema = {
$id: '#/components/schemas/createApiTokenSchema',
type: 'object',
required: ['username', 'type'],
properties: {
secret: {
type: 'string',
},
username: {
type: 'string',
},
type: {
type: 'string',
openapi: improve validation testing (#2058) ## What This PR adds an extra layer of OpenAPI validation testing to what we already have. It also fixes any issues that make the new tests fail. ## Why While the current OpenAPI validation takes care of _some_ things, there's also things it misses, as shown by #2055. By adding the OpenAPI Enforcer package, we should hopefully be able to catch more of these errors in the future. The enforcer _does_ flag the issue in #2055 as an error. ## How By adding the OpenAPI Enforcer package and making whatever changes it picks up on. By adding location headers to all our 201 endpoints. I also had to change some signatures on `create` store methods so that they actually return something (a lot of them just returned `void`). ## Discussion points ### Code changes Some of the code changes may not be necessary or we may want to change more code to align with what changes have been done. It may be worth standardizing on a pattern for `*store.create` methods, so that they always return an identifier or the stored object, for instance. ### On relative URIs The 201 location headers use relative URIs to point to the created resources. This seemed to be the easiest way to me, as we don't need to worry about figuring out what the absolute URL of the instance is (though we could probably just concat it to the request URI?). The algorithm for determining relative URIs is described in [RFC 3986 section 5](https://www.rfc-editor.org/rfc/rfc3986#section-5). There's also some places where I'm not sure we _can_ provide accurate location url. I think they're supposed to point _directly at_ whatever the resource is, but for some resources (such as api tokens), you can only get it as part of a collection. From [RFC 9110 on the location field](https://httpwg.org/specs/rfc9110.html#field.location) (emphasis mine): > the Location header field in a [201 (Created)](https://httpwg.org/specs/rfc9110.html#status.201) response is supposed to provide a URI that is **specific** to the created resource. A link to a collection is not specific. I'm not sure what best to do about this. ### Inline comments I've added a number of inline PR comments that I'd love to get some feedback on too. Have a look and let me know what you think! ### Unfinished business I've added some juicy comments to some of the files here. They contain non-blocking issues that I'm tracking (via github issues). We should resolve them in the future, but right now they can stay as they are. ## Commits * Feat: add openapi-enforcer + tests; fix _some_ issues * Test: allow non-standard string formats * validation: fix _some_ 201 created location header endpoints * #1391: fix remaining 201 location headers missing * Refactor: use the ajv options object instead of add* methods * #1391: flag validation errors and warnings as test failures * #1391: modify patch schema to specify either object or array We don't provide many patch endpoints, so we _could_ create separate patch operation objects for each one. I think that makes sense to do as part of the larger cleanup. For now, I think it's worth to simply turn it into one of these. While it's not entirely accurate, it's better than what we had before. * Refactor: make tests easier to read * #1391: use enum for valid token types This was previously only a description. This may seem like a breaking change because OpenAPI would previously accept any string. However, Joi also performs validation on this, so invalid values wouldn't work previously either. * #1391: Comment out default parameter values for now This isn't the _right_ way, but it's the pragmatic solution. It's not a big deal and this works as a stopgap solution. * #1391: add todo note for api token schema fixes * #1391: update snapshot * Revert "#1391: modify patch schema to specify either object or array" This reverts commit 0dd5d0faa1554a394178c0f632915e096204cac7. Turns out we need to allow much more than just objects and arrays. I'll leave this as is for now. * Chore(#1391): update comment explaining api token schema TODO * #1391: modify some test code and add comment * #1391: update tests and spec to allow 'any' type in schema * Chore: remove comment * #1391: add tests for context field stores * #1391: add location header for public signup links * #1391: fix query parameter description. There was indeed a bug in the package, and this has been addressed now, so we can go back to describing the params properly.
2022-09-23 15:02:09 +02:00
enum: Object.values(ApiTokenType),
},
environment: {
type: 'string',
},
project: {
type: 'string',
},
projects: {
type: 'array',
items: {
type: 'string',
},
},
expiresAt: {
type: 'string',
format: 'date-time',
nullable: true,
},
},
components: {},
} as const;
export type CreateApiTokenSchema = FromSchema<typeof createApiTokenSchema>;