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

Add #[schemars(extend("key" = value))] attribute #297

Merged
merged 2 commits into from
Jun 5, 2024
Merged

Add #[schemars(extend("key" = value))] attribute #297

merged 2 commits into from
Jun 5, 2024

Conversation

GREsau
Copy link
Owner

@GREsau GREsau commented Jun 5, 2024

Implements #50

  • this attribute can be used to add properties (or replace existing properties) in a generated schema
  • can be set on a struct, enum, or enum variant
  • can have multiple key/value pairs and/or be specified multiple times - but if so, every key must be unique (otherwise, a compile error is emitted)
  • key must be quoted but can be any string
  • value can be any expression that produces a value implementing Serialize
  • value can also be a JSON literal which can interpolate other values, e.g. #[schemars(extend("obj" = {"array": [null, ()]}))]
    • this follows the same rules as arguments to the serde_json::json!(...) macro (which this attribute uses under the hood)

@GREsau GREsau merged commit 840315b into v1 Jun 5, 2024
8 checks passed
@GREsau GREsau deleted the extend branch June 5, 2024 20:09
andrew-lee-architect added a commit to architect-xyz/architect-api that referenced this pull request Jan 13, 2025
Initially I was trying to implement this in a way that required no app
code changes, but was unable to accomplish that.

The current diff is the fourth idea. Including the failed attempts
below:

* ❌ Implement a `schema_with_renames`.
* ❌⭐ Implement a `schemars#Visitor` to make it completely transparent 
* ❌ Use `schemars(extend = ...)` attribute
* ✅ Switch from `serde(rename)` to `serde(rename = "...", title =
"<source-name>" )` attribute

I strongly believe that the second option is the best superior option,
though was unable to find a way to get access to the source field name.
I poked around serde and schemars source unhelpfully. Eg. schemars
tracks `is_renamed` at
https://github.com/architect-xyz/schemars/blob/a804f4f0510dbc0ddbf48da53e410baa7e6af8ba/schemars_derive/src/attr/mod.rs#L30
and
https://github.com/architect-xyz/schemars/blob/a804f4f0510dbc0ddbf48da53e410baa7e6af8ba/schemars_derive/src/lib.rs#L88-L94,
though this did not appear to be visible or useful in the visitor.


*Tasks*
- [x] Settle on an approach to encode this in rust side
- [x] Write decoder for this format in architect-ts
architect-xyz/architect-ts#16
- [x] Update everything accordingly
- [x] Codegen in ts sdk
architect-xyz/architect-ts#21

**Things intentionally not remapped back**
* `PutOrCall`
https://github.com/architect-xyz/architect/blob/2e5c0253f4bc92ed973412ae0a2c37d7f7669391/api/src/symbology_v2/options_series.rs#L180-L185
* any `rename_all` or other style-based renaming option

<details>
<summary> `schema_with_renames` idea </summary>

Definition
```rs
// schemars_utils.rs
use schemars::gen::SchemaGenerator;
use schemars::schema::{Schema, SchemaObject};
use schemars::JsonSchema;
use serde_json::json;

pub fn schema_with_renames(generator: &mut SchemaGenerator) -> Schema {
    let schema = String::json_schema(generator);
    let mut schema = schema.into_object();
    // TODO: get a handle on the source attribute from generator or schema.
    // Was unable to find access to this
    schema.extensions.insert("renamed".into(), json!("market_id"));
    schema.into()
}
```

Usage
```diff
     #[serde(rename = "a")]
+    #[schemars(schema_with = "crate::schemars_utils::schema_with_renames")]
     pub best_ask: Option<(Decimal, Decimal)>, 
```

</details>

<details>
<summary> `schema#Visitor` idea </summary>

This is an improvement on the first idea, however was blocked on the
same issue: could not find a reference to the source field name

```rs
// schemars_visitor.rs
use schemars::gen::SchemaGenerator;
use schemars::schema::{Schema, SchemaObject};
use schemars::visit::{visit_schema, visit_schema_object, Visitor};
use schemars::JsonSchema;
// use serde::Serialize;
use serde_json::json;

#[derive(Clone, Debug)]
pub struct RenameVisitor;

impl Visitor for RenameVisitor {
    fn visit_schema_object(&mut self, schema: &mut SchemaObject) {
        // First, make our change to this schema

        match schema.metadata.as_ref() {
            Some(n) => {
                println!(
                    "cargo:warning={} {:?}",
                    "visit_schema_object",
                    n // schema
                );
                // TODO: get a handle on the source attribute from generator or schema.
                // Was unable to find access to this
                schema.extensions.insert("renamed".into(), json!("market_id"));
            }
            None => {
                // do nothing
            }
        }

        // Then delegate to default implementation to visit any subschemas
        visit_schema_object(self, schema);
    }
}

pub fn schema_with_renames(generator: &mut SchemaGenerator) -> Schema {
    let schema = String::json_schema(generator);
    let mut schema = schema.into_object();
    schema.extensions.insert("renamed".into(), json!("market_id"));
    schema.into()
}
```

</details>


<details>
<summary> `schema(rename = ...)` attribute </summary>

This was requested at GREsau/schemars#50 and
implemented at GREsau/schemars#297. While this
is present in the schemars v0.8 docs, we forked schemars the month
before this was added.

I started to backport this into our fork, but the diff was not trivial
and I figured (ab)using the `title` attribute was not much different so
decided to go that direction for now.

</details>

---------

Co-authored-by: andrew-lee-architect <126016409+andrew-lee-architect@users.noreply.github.com>
andrew-lee-architect added a commit to architect-xyz/architect-api that referenced this pull request Jan 13, 2025
Initially I was trying to implement this in a way that required no app
code changes, but was unable to accomplish that.

The current diff is the fourth idea. Including the failed attempts
below:

* ❌ Implement a `schema_with_renames`.
* ❌⭐ Implement a `schemars#Visitor` to make it completely transparent 
* ❌ Use `schemars(extend = ...)` attribute
* ✅ Switch from `serde(rename)` to `serde(rename = "...", title =
"<source-name>" )` attribute

I strongly believe that the second option is the best superior option,
though was unable to find a way to get access to the source field name.
I poked around serde and schemars source unhelpfully. Eg. schemars
tracks `is_renamed` at
https://github.com/architect-xyz/schemars/blob/a804f4f0510dbc0ddbf48da53e410baa7e6af8ba/schemars_derive/src/attr/mod.rs#L30
and
https://github.com/architect-xyz/schemars/blob/a804f4f0510dbc0ddbf48da53e410baa7e6af8ba/schemars_derive/src/lib.rs#L88-L94,
though this did not appear to be visible or useful in the visitor.


*Tasks*
- [x] Settle on an approach to encode this in rust side
- [x] Write decoder for this format in architect-ts
architect-xyz/architect-ts#16
- [x] Update everything accordingly
- [x] Codegen in ts sdk
architect-xyz/architect-ts#21

**Things intentionally not remapped back**
* `PutOrCall`
https://github.com/architect-xyz/architect/blob/2e5c0253f4bc92ed973412ae0a2c37d7f7669391/api/src/symbology_v2/options_series.rs#L180-L185
* any `rename_all` or other style-based renaming option

<details>
<summary> `schema_with_renames` idea </summary>

Definition
```rs
// schemars_utils.rs
use schemars::gen::SchemaGenerator;
use schemars::schema::{Schema, SchemaObject};
use schemars::JsonSchema;
use serde_json::json;

pub fn schema_with_renames(generator: &mut SchemaGenerator) -> Schema {
    let schema = String::json_schema(generator);
    let mut schema = schema.into_object();
    // TODO: get a handle on the source attribute from generator or schema.
    // Was unable to find access to this
    schema.extensions.insert("renamed".into(), json!("market_id"));
    schema.into()
}
```

Usage
```diff
     #[serde(rename = "a")]
+    #[schemars(schema_with = "crate::schemars_utils::schema_with_renames")]
     pub best_ask: Option<(Decimal, Decimal)>, 
```

</details>

<details>
<summary> `schema#Visitor` idea </summary>

This is an improvement on the first idea, however was blocked on the
same issue: could not find a reference to the source field name

```rs
// schemars_visitor.rs
use schemars::gen::SchemaGenerator;
use schemars::schema::{Schema, SchemaObject};
use schemars::visit::{visit_schema, visit_schema_object, Visitor};
use schemars::JsonSchema;
// use serde::Serialize;
use serde_json::json;

#[derive(Clone, Debug)]
pub struct RenameVisitor;

impl Visitor for RenameVisitor {
    fn visit_schema_object(&mut self, schema: &mut SchemaObject) {
        // First, make our change to this schema

        match schema.metadata.as_ref() {
            Some(n) => {
                println!(
                    "cargo:warning={} {:?}",
                    "visit_schema_object",
                    n // schema
                );
                // TODO: get a handle on the source attribute from generator or schema.
                // Was unable to find access to this
                schema.extensions.insert("renamed".into(), json!("market_id"));
            }
            None => {
                // do nothing
            }
        }

        // Then delegate to default implementation to visit any subschemas
        visit_schema_object(self, schema);
    }
}

pub fn schema_with_renames(generator: &mut SchemaGenerator) -> Schema {
    let schema = String::json_schema(generator);
    let mut schema = schema.into_object();
    schema.extensions.insert("renamed".into(), json!("market_id"));
    schema.into()
}
```

</details>


<details>
<summary> `schema(rename = ...)` attribute </summary>

This was requested at GREsau/schemars#50 and
implemented at GREsau/schemars#297. While this
is present in the schemars v0.8 docs, we forked schemars the month
before this was added.

I started to backport this into our fork, but the diff was not trivial
and I figured (ab)using the `title` attribute was not much different so
decided to go that direction for now.

</details>

---------

Co-authored-by: andrew-lee-architect <126016409+andrew-lee-architect@users.noreply.github.com>
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.

1 participant