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

Query params should not be nullable #1215

Open
sergiimk opened this issue Nov 22, 2024 · 0 comments
Open

Query params should not be nullable #1215

sergiimk opened this issue Nov 22, 2024 · 0 comments

Comments

@sergiimk
Copy link

Consider a query params struct like this:

#[derive(Debug, serde::Deserialize, utoipa::IntoParams)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[into_params(parameter_in = Query)]
pub struct QueryParams {
    pub schema_format: Option<SchemaFormat>,
}

#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    PartialEq,
    Eq,
    serde::Serialize,
    serde::Deserialize,
    utoipa::ToSchema,
)]
pub enum SchemaFormat {
    #[default]
    ArrowJson,
    Parquet,
    ParquetJson,
}

Utoipa generates a spec like this:

{
  "openapi": "3.1.0",
  "paths": {
    "/query": {
      "get": {
        "operationId": "query_handler",
        "parameters": [
          {
            "description": "How to encode the schema of the result",
            "in": "query",
            "name": "schemaFormat",
            "required": false,
            "schema": {
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "$ref": "#/components/schemas/SchemaFormat"
                }
              ]
            }
          }
       ]
      }
    }
  },
  "components": {
    "schemas": {
      "SchemaFormat": {
        "enum": [
          "ArrowJson",
          "Parquet",
          "ParquetJson"
        ],
        "type": "string"
      }
    }
  }
}

When we run IBM openapi-validator on this spec it gives an error:

Errors:

  Message :   Invalid type; valid types are: array, boolean, integer, number, object, string
  Rule    :   ibm-schema-type-format
  Path    :   paths./query.get.parameters.5.schema.oneOf.0

I think the validator is correct here, as in case of a query parameter there cannot be null value, as it would be non-distinguishable from a "null" string.

My expectation is that for an optional query parameter utoipa should only specify "required": false but not make value nullable.

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

No branches or pull requests

1 participant