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 schemas for VSCode YAML Support #497

Merged
merged 7 commits into from
May 22, 2024
Merged

Json schemas for VSCode YAML Support #497

merged 7 commits into from
May 22, 2024

Conversation

haixuanTao
Copy link
Collaborator

@haixuanTao haixuanTao commented May 15, 2024

In an effort to address, developers difficulty in discovering dora-rs functionality, I'm adding a way to:

  • Generate JSON Schema: https://json-schema.org/ using schemars
  • JSON Schema can be interpreted by many extension to give us YAML validation, documentation and suggestions. In my case, I'm using VSCode most popular extension for YAML which is redhat.vscode-yaml

This enables:

  • Documentation that is directly generated from the docstring within the rust code
  • Suggestions from the field defined in the Rust Code
  • Validation of type

Edge cases

Failure to auto-generate schema

Some auto-generation of some of our rust struct definition did not work. They are the one that are using serde(from= such as:

}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(
deny_unknown_fields,
from = "PythonSourceDef",
into = "PythonSourceDef"
)]
pub struct PythonSource {
pub source: String,
pub conda_env: Option<String>,
}

In those case I had to remove the validation so that our current dataflow passes.

Failure to manage #[serde(flatten)] with additional properties

In our Nodes definition, we are using #[serde(flatten)] of an enum with some additional properties.

In order to validate those additional properties, I had to set additionalproperties to true.

Demo

2024-05-15.15-43-22.mp4

@haixuanTao haixuanTao changed the title Json schemas for VSCode YAML Suppport Json schemas for VSCode YAML Support May 15, 2024
"nodes"
],
"properties": {
"_unstable_deploy": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to have this (and other) _unstable_* fields in the schema? If not, we should be able to ignore them using a #[schemars(skip)] attribute.

I'm also fine with keeping them in, as the field names makes it clear that they are unstable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, in the end I have removed unstable features as to not overcrowd: autocomplete and push people on features not yet complete.

Comment on lines +10 to +13
let raw_schema = raw_schema.replace(
"\"additionalProperties\": false",
"\"additionalProperties\": true",
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this replace needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

// Add additional properties to True, as #[derive(transparent)] of enums are not well handled.
//
// 'OneOf' such as Custom Nodes, Operators and Single Operators overwrite property values of the initial struct `Nodes`.`
// which make the original properties such as `id` and `name` not validated by IDE extensions.

I have added documentation but basically its because we use #[derive(transparent)] on an enum, and the parent properties get forgotten in the JSON Schema.

libraries/core/src/config.rs Outdated Show resolved Hide resolved
libraries/core/src/config.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@phil-opp phil-opp left a comment

Choose a reason for hiding this comment

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

Thanks!

@phil-opp phil-opp merged commit ef214d1 into main May 22, 2024
16 of 17 checks passed
@phil-opp phil-opp deleted the json-schemas branch May 22, 2024 10:25
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.

2 participants