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

Feature request: support #[schema(skip)] #795

Closed
magurotuna opened this issue Nov 4, 2023 · 6 comments · Fixed by #1090
Closed

Feature request: support #[schema(skip)] #795

magurotuna opened this issue Nov 4, 2023 · 6 comments · Fixed by #1090
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@magurotuna
Copy link

Requested feature

Add support for #[schema(skip)]

What this does

When this attribute is present, the target field or variant will not show up in the generated OpenAPI spec. This is like #[serde(skip)], but the difference is that the proposed attribute doesn't affect how it is serialized.

Background

We're using utoipa to generate OpenAPI spec based on API implementation written in Rust.
One unique point with our API is that it's used both externally and internally; for external users, the API must not make breaking changes while for internal purposes we may make some changes that can be breaking.
In order to achieve it, we use structs like below, which has __internal field that appears in the response body only if the request was made by an internal client.

#[derive(utoipa::ToSchema, Serailize)]
struct MyResponse {
    /// This is public to all users
    public_value: i32,
    /// This is set to `Some` only when responding to internal clients
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    ___secret: Option<SecretStuff>,
}

#[derive(Serialize)]
struct SecretStuff {
    secret_value: String,
}

So essentially we would like to use SecretStuff as a schema, but we don't want it to show up in the OpenAPI spec because our OpenAPI spec serves as a public documentation of stable interfaces.

With #[schema(skip)], this would be accomplished by just adding this attribute to __secret, like:

#[derive(utoipa::ToSchema, Serailize)]
struct MyResponse {
    /// This is public to all users
    public_value: i32,
    /// This is set to `Some` only when responding to internal clients
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    #[schema(skip)]
    ___secret: Option<SecretStuff>,
}

#[derive(Serialize)]
struct SecretStuff {
    secret_value: String,
}
@juhaku
Copy link
Owner

juhaku commented Nov 7, 2023

@magurotuna Sure, sounds a good idea, adding this kind of support should be quite easily done. Though I would make it as #[schema(ignore)] so it will not be confused with the #[serde(skip)]. The reasoning for this is that there currently are some schema attributes that are synomuous to serde attributes and if this is added but it behaves differently to the serde counterparts it will be confusing to the users. Thus #[schema(ignore)] to ignore attribute from the schema and #[serde(skip)] to skip the de/serialization. Then this would even allow defining support for #[schema(skip)] that would behave like #[serde(skip)] if needed.

@GodTamIt
Copy link

I ran into a similar use case today. #[param(ignore)] would also be quite useful

@Narayanbhat166
Copy link
Contributor

Have a similar use case. @juhaku can I pick this up if this has not been implemented yet?

@GeeWee
Copy link

GeeWee commented Jul 26, 2024

I also have a similar use-case, where we e.g. use #[serde(flatten) on a recursive data-type, which I'd prefer to just skip adding to the openAPI spec entirely. I can't skip it with serde as I still want it to be serialized, just not documented.

@juhaku
Copy link
Owner

juhaku commented Sep 3, 2024

@Narayanbhat166 This has not been yet started working on, So please if there is time an energy, all contributions are welcome 🙂

@juhaku juhaku added good first issue Good for newcomers enhancement New feature or request labels Sep 10, 2024
@juhaku juhaku moved this to In Progress in utoipa kanban Oct 4, 2024
juhaku added a commit that referenced this issue Oct 4, 2024
Add support for `#[schema(ignore)]` for named field struct types
implementing `ToSchema` trait. Add support for `param(ignore)` for named
field structs implementing `IntoParams` trait. The ignored field will
not get serialized to the OpenAPI schema.

```rust
 #[derive(ToSchema)]
 struct SchemaIgnoredField {
     value: String,
     #[schema(ignore)]
     __this_is_private: String,
 }

 #[derive(IntoParams)]
 #[into_params(parameter_in = Query)]
 struct Params {
     name: String,
     #[param(ignore)]
     __this_is_private: String,
 }
```

Closes #795
juhaku added a commit that referenced this issue Oct 4, 2024
Add support for `#[schema(ignore)]` for named field struct types
implementing `ToSchema` trait. Add support for `param(ignore)` for named
field structs implementing `IntoParams` trait. The ignored field will
not get serialized to the OpenAPI schema.

```rust
 #[derive(ToSchema)]
 struct SchemaIgnoredField {
     value: String,
     #[schema(ignore)]
     __this_is_private: String,
 }

 #[derive(IntoParams)]
 #[into_params(parameter_in = Query)]
 struct Params {
     name: String,
     #[param(ignore)]
     __this_is_private: String,
 }
```

Closes #795
juhaku added a commit that referenced this issue Oct 4, 2024
Add support for `#[schema(ignore)]` for named field struct types
implementing `ToSchema` trait. Add support for `param(ignore)` for named
field structs implementing `IntoParams` trait. The ignored field will
not get serialized to the OpenAPI schema.

```rust
 #[derive(ToSchema)]
 struct SchemaIgnoredField {
     value: String,
     #[schema(ignore)]
     __this_is_private: String,
 }

 #[derive(IntoParams)]
 #[into_params(parameter_in = Query)]
 struct Params {
     name: String,
     #[param(ignore)]
     __this_is_private: String,
 }
```

Closes #795
juhaku added a commit that referenced this issue Oct 4, 2024
Add support for `#[schema(ignore)]` for named field struct types
implementing `ToSchema` trait. Add support for `param(ignore)` for named
field structs implementing `IntoParams` trait. The ignored field will
not get serialized to the OpenAPI schema.

```rust
 #[derive(ToSchema)]
 struct SchemaIgnoredField {
     value: String,
     #[schema(ignore)]
     __this_is_private: String,
 }

 #[derive(IntoParams)]
 #[into_params(parameter_in = Query)]
 struct Params {
     name: String,
     #[param(ignore)]
     __this_is_private: String,
 }
```

Closes #795
juhaku added a commit that referenced this issue Oct 4, 2024
Add support for `#[schema(ignore)]` for named field struct types
implementing `ToSchema` trait. Add support for `param(ignore)` for named
field structs implementing `IntoParams` trait. The ignored field will
not get serialized to the OpenAPI schema.

```rust
 #[derive(ToSchema)]
 struct SchemaIgnoredField {
     value: String,
     #[schema(ignore)]
     __this_is_private: String,
 }

 #[derive(IntoParams)]
 #[into_params(parameter_in = Query)]
 struct Params {
     name: String,
     #[param(ignore)]
     __this_is_private: String,
 }
```

Closes #795
@juhaku
Copy link
Owner

juhaku commented Oct 4, 2024

Now there is implementation coming up for this #1090

juhaku added a commit that referenced this issue Oct 4, 2024
Add support for `#[schema(ignore)]` for named field struct types
implementing `ToSchema` trait. Add support for `param(ignore)` for named
field structs implementing `IntoParams` trait. The ignored field will
not get serialized to the OpenAPI schema.

```rust
 #[derive(ToSchema)]
 struct SchemaIgnoredField {
     value: String,
     #[schema(ignore)]
     __this_is_private: String,
 }

 #[derive(IntoParams)]
 #[into_params(parameter_in = Query)]
 struct Params {
     name: String,
     #[param(ignore)]
     __this_is_private: String,
 }
```

Closes #795
@github-project-automation github-project-automation bot moved this from In Progress to Done in utoipa kanban Oct 4, 2024
@juhaku juhaku moved this from Done to Released in utoipa kanban Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

5 participants