Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@wagoodman
Copy link
Contributor

Today we are generating our (messy) json schema from a set of examples derived from integration test cases. This process results in a JSON schema where type semantics are lost and merged into a homogenous blob of individual field definitions (instead of a collection of definitions with a set of parameters for each type).

This PR replaces the JSON schema (and the mechanism for generating it) with one that keeps the semantics intact. Specifically, by using github.com/alecthomas/jsonschema we can now generate the json schema directly from our struct definitions.

There is one caveat: this json schema does not include definitions for anything underneath weakly typed interface{} fields, such as pkg.Package.Metadata. This is pretty notable, but something that can be improved on in the future (either by tweaking this automated approach, or by replacing it with a manual approach).

Partially addresses #38

@wagoodman wagoodman added the enhancement New feature or request label Nov 20, 2020
@wagoodman wagoodman requested a review from a team November 20, 2020 12:23
@wagoodman wagoodman self-assigned this Nov 20, 2020

for _, d := range deep.Equal(a, e) {
// ignore errors for empty collections vs nil for select fields
// TODO: this is brittle, but not dangerously so. We should still find a better way to do this.
Copy link
Contributor

Choose a reason for hiding this comment

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

It's unclear to me why this is here, it'd be good to talk this through and/or add more explanation in the comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we have cases where the licenses are still nil slices on the package object, something that the presenter now normalizes to an empty slice instead during presentation, but technically they are not equivalent when doing comparison testing for the serialization loop (make package -> get json -> import json -> get packages -> compare packages against original packages). This ignores a select case of the described behavior for a single field.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, interesting. So this test is effectively testing several processes — normal cataloging, JSON presenters, and the new document important logic. Just so I can better understand, with regard to the document important logic, what are we verifying that wouldn't be caught with tests whose only notable function call was CatalogFromJSON? It seems like we could have a JSON document test fixture, and then assert that the returned values (e.g. distro, source metadata, catalog packages) are what we expect.

Btw, these could easily be notes for enhancement after this PR. I'm just trying to better understand what's going on in this code. And I think I'm coming to the realization that the empty slice vs. nil slice issue is more of a symptom than a root issue.

For now, I think it's fine to explain in the comment that this "exception" to the equality assertion is due to everything that's happening in the "serialization loop" (like you describe above), specifically that this test scope introduces the need to account for data changes in the JSON presenter logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like we could have a JSON document test fixture...

Yes. However, this would be brittle in another way.

As the shape of the JSON document changes, we'll need to update the JSON test fixture continually. We do this for the JSON presenter since the unit tests should be ensuring values are correct (which we do with a diff of the json values and depend on folks updating the snapshot with a scrutinizing eye). We already have a set of curated test cases which are guaranteed to cover all package types, which provides a greater variety of data testing coverage than what we do in the unit tests for the JSON presenters... using these cases along with the real JSON presenter output as test input casts a much wider net for being alerted to potential import problems that may arise.

the empty slice vs. nil slice issue is more of a symptom than a root issue...

Ack. I came to the same conclusion, and didn't want to go to the trouble of fixing that behavior since it technically isn't "wrong" and would take more time.

@wagoodman wagoodman force-pushed the generate-json-schema branch from 1ff45e6 to 8a17bfb Compare November 20, 2020 20:47

for _, d := range deep.Equal(a, e) {
// ignore errors for empty collections vs nil for select fields
// TODO: this is brittle, but not dangerously so. We should still find a better way to do this.
Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, interesting. So this test is effectively testing several processes — normal cataloging, JSON presenters, and the new document important logic. Just so I can better understand, with regard to the document important logic, what are we verifying that wouldn't be caught with tests whose only notable function call was CatalogFromJSON? It seems like we could have a JSON document test fixture, and then assert that the returned values (e.g. distro, source metadata, catalog packages) are what we expect.

Btw, these could easily be notes for enhancement after this PR. I'm just trying to better understand what's going on in this code. And I think I'm coming to the realization that the empty slice vs. nil slice issue is more of a symptom than a root issue.

For now, I think it's fine to explain in the comment that this "exception" to the equality assertion is due to everything that's happening in the "serialization loop" (like you describe above), specifically that this test scope introduces the need to account for data changes in the JSON presenter logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants