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

JSON Schema: Ignore fields via comment #14

Merged
merged 3 commits into from
May 8, 2024

Conversation

rodaine
Copy link
Member

@rodaine rodaine commented May 7, 2024

This patch enables having fields ignored in the JSON Schema generation via a comment containing jsonschema:ignore (in either a leading or trailing comment).

A comment here is expedient, but not ideal as a long-term strategy. Would be much better as a custom option. This also has no impact on PubSub generation, though could presumably be extended to support that as well.

Part of this required changes to the golden tests as source location info (read: comments) aren't preserved in the reflection descriptors by the Go protobuf library (or any of the official ones for that matter). As a result, comments have now appeared in the input.json and impacted golden tests.

Holding off documenting until this strategy is approved.

@rodaine rodaine requested review from Alfus and mfridman May 7, 2024 21:16
@Alfus
Copy link
Contributor

Alfus commented May 7, 2024

should the fields really be ignored? Or somehow less prominent in the json schema. I am pretty sure it will show you an error if you try to use a field that is not listed in the json schema.

@mfridman
Copy link
Member

mfridman commented May 7, 2024

@Alfus are you thinking of something more explicit like https://json-schema.org/understanding-json-schema/reference/combining#not where you can exclude specific keys?

but also, this seems like a nice property in general where you want the jsonschema to be a subset of the protos?

@Alfus
Copy link
Contributor

Alfus commented May 7, 2024

I am suggesting the field get listed in the schema, but not have docs, or maybe json schema has a 'hid' option so it doen't suggest the field, but will still validate it, if present.

@rodaine
Copy link
Member Author

rodaine commented May 7, 2024

The goal here is for a config file that supports some internal-only (mostly testing or experimental) properties, but hides them from end users. Some options:

  • Set additionalProperties to allow extra undefined fields. This aligns well with unknown proto fields, fwiw.

  • We could generate a wildcard property that will apply to any unknown fields. I'm not familiar enough with JSON Schema or IDE plugins to say if they're good at handling specialization/specificity in this context.

  • Move these rules out of the comments (or field options) and up to the protoc plugin as options there. That way the caller can choose what to generate by usecase (as opposed to having it fixed in the proto source itself).

  • Leave this as-is and allow the JSON schema to be an explicit subset of the actual proto definition.

@mfridman
Copy link
Member

mfridman commented May 7, 2024

My vote would be option 4 (current implementation), followed closely by a plugin option to get this out of the proto definitions, something like ignore_type. I personally don't mind the extra annotations, but I can see how that's a slippery slope towards littering proto files with unrelated things.

In terms of additionalProperties or simply dropping, not sure which is better. I'd defer to the person who is actually generating and working with this (I think ignoring it makes sense to start). It also sounds like we could support both in due time and they don't have to be mutually exclusive?

My only other suggestion is let's give it a try, get some hands-on experience, report feedback and tweak it as we go.

@rodaine rodaine requested review from Alfus and mfridman May 8, 2024 18:51
@rodaine
Copy link
Member Author

rodaine commented May 8, 2024

Kept it as-is for now. We can revisit with a more robust solution after we see where this all lands.

Copy link
Member

@mfridman mfridman left a comment

Choose a reason for hiding this comment

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

I'd suggest removing the readme changes, and keeping this as an undocumented "internal feature". Otherwise lgtm as we buy us time to iterate on this.

@rodaine rodaine merged commit a876f2b into main May 8, 2024
4 checks passed
@rodaine rodaine deleted the rodaine/jsonschema-ignore-field branch May 8, 2024 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants