Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi: improve validation testing #2058

Merged
merged 21 commits into from
Sep 23, 2022

Conversation

thomasheartman
Copy link
Contributor

@thomasheartman thomasheartman commented Sep 13, 2022

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.

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 (emphasis mine):

the Location header field in a 201 (Created) 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.

@vercel
Copy link

vercel bot commented Sep 13, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

3 Ignored Deployments
Name Status Preview Updated
unleash ⬜️ Ignored (Inspect) Sep 23, 2022 at 9:53AM (UTC)
unleash-docs ⬜️ Ignored (Inspect) Sep 23, 2022 at 9:53AM (UTC)
unleash-monorepo-frontend ⬜️ Ignored (Inspect) Sep 23, 2022 at 9:53AM (UTC)

@thomasheartman thomasheartman marked this pull request as draft September 13, 2022 06:57
@github-actions
Copy link

github-actions bot commented Sep 13, 2022

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements
88.27% (-3.1% 🔻)
7052/7989
🟡 Branches 78.72% 1099/1396
🟢 Functions
81.89% (-4.34% 🔻)
1985/2424
🟢 Lines
88.57% (-2.74% 🔻)
6538/7382

⚠️ Details were not displayed: the report size has exceeded the limit.

Test suite run success

1160 tests passing in 191 suites.

Report generated by 🧪jest coverage report action from f4e55c6

@@ -221,7 +226,7 @@
req.body,
userName,
);
res.status(201).json(tag);
res.status(201).header('location', `${featureName}/tags`).json(tag);

Check warning

Code scanning / CodeQL

Server-side URL redirect

Untrusted URL redirection due to [user-provided value](1).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, this looks interesting. Don't know if we can do anything about this or whether it's an issue here 💁🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sighphyre Do you have any thoughts around this comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't believe this is an issue, you'd need to successfully add a tag for the location header to get sent and for that to work you'd need to send in a feature name as part of the URL, and that feature needs to exist. I think this is okay

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.
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.
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.
@thomasheartman thomasheartman marked this pull request as ready for review September 14, 2022 13:09
This reverts commit 0dd5d0f.

Turns out we need to allow much more than just objects and arrays.
I'll leave this as is for now.
src/lib/db/context-field-store.ts Show resolved Hide resolved
src/lib/db/context-field-store.ts Show resolved Hide resolved
src/lib/openapi/validate.ts Show resolved Hide resolved
src/lib/routes/admin-api/context.ts Outdated Show resolved Hide resolved
src/lib/services/openapi-service.ts Show resolved Hide resolved
Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good, I just want to be certain that this isn't going to cause new errors to bubble in cases where they previously wouldn't?

package.json Outdated Show resolved Hide resolved
src/lib/db/context-field-store.ts Show resolved Hide resolved
src/lib/routes/admin-api/context.ts Outdated Show resolved Hide resolved
src/lib/services/openapi-service.ts Show resolved Hide resolved
src/test/e2e/api/openapi/openapi.e2e.test.ts Outdated Show resolved Hide resolved
There was indeed a bug in the package, and this has been addressed
now, so we can go back to describing the params properly.
@thomasheartman thomasheartman force-pushed the validation/improve-openapi-validation-tests branch from 9f32046 to f4e55c6 Compare September 23, 2022 09:53
Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot here, but it looks good and seems to have some nice tests so... LGTM!

@thomasheartman thomasheartman merged commit 97c2b3c into main Sep 23, 2022
@thomasheartman thomasheartman deleted the validation/improve-openapi-validation-tests branch September 23, 2022 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants