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

deserializing and serializing results in faulty json #67

Open
andrieshiemstra opened this issue Dec 1, 2021 · 2 comments
Open

deserializing and serializing results in faulty json #67

andrieshiemstra opened this issue Dec 1, 2021 · 2 comments
Labels
okapi This affects the okapi crate

Comments

@andrieshiemstra
Copy link

andrieshiemstra commented Dec 1, 2021

When deserializing from json the schemas from a param also get added to the extensions object of a param, resulting in faulty json being outputted when serializing again (schema attribute is added twice)

use okapi::openapi3::{Operation, RefOr, Parameter};

fn main() {

    let json = "{\"responses\": {}, \"name\": \"someOp\", \"parameters\": [{\"in\": \"path\", \"schema\": {\"$ref\": \"#/defid\"}, \"name\": \"param1\"}]}";

    let operation: Operation = serde_json::from_str(json).unwrap();

    for param in &operation.parameters {
        match param {
            RefOr::Ref(_) => {
            }
            RefOr::Object(p) => {
                println!("p.json: {}", serde_json::to_string(p).unwrap());
                println!("p.value.json: {}", serde_json::to_string(&p.value).unwrap());
                println!("p.extensions.json: {}", serde_json::to_string(&p.extensions).unwrap());
            }
        }
    }

    println!("json: {}", serde_json::to_string(&operation).unwrap());

}

results in

p.json: {"name":"param1","in":"path","schema":{"$ref":"#/defid"},"schema":{"$ref":"#/defid"}}
p.value.json: {"schema":{"$ref":"#/defid"}}
p.extensions.json: {"schema":{"$ref":"#/defid"}}
json: {"parameters":[{"name":"param1","in":"path","schema":{"$ref":"#/defid"},"schema":{"$ref":"#/defid"}}],"responses":{},"name":"someOp"}

The same happens when deserializing an operation with responses, deserializing and serializing this

   "responses": {
      "200": {
        "description": "info to be returned",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/getInfoResultSchema"
            }
          }
        }
      },
      "default": {
        "description": "Unexpected error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/DefaultError"
            }
          }
        }
      }
    }

results in this

"responses": {
          "default": {
            "description": "Unexpected error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DefaultError"
                }
              }
            }
          },
          "200": {
            "description": "info to be returned",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/getInfoResultSchema"
                }
              }
            }
          },
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/getInfoResultSchema"
                }
              }
            },
            "description": "info to be returned"
          }
        }
@ralpha
Copy link
Collaborator

ralpha commented Dec 1, 2021

Hmm, looks indeed like it is doing something wrong in the default Serde parser. I think this is because of the #[serde(flatten)].

Looks like we might need to add #[serde(skip_deserializing)] to https://github.com/GREsau/okapi/blob/master/okapi/src/openapi3.rs#L272:L273

But would have to look at it when I have some more time to be sure this solves it and does not create other problems. The pub extensions: Object, is part of more items, so a fix for 1 can then also be applied to all the other once that have a double #[serde(flatten)].
(PR is always welcome)

@andrieshiemstra
Copy link
Author

For the moment i'm fixing this post-deser.. which for now is fine for me

Simply ignoring extensions when deserializing seems a bit drastic, so my first thought was to create a simple custom deserializer which ignores the fields already added to flattened ParameterValue enum..

But that got me thinking if this isn't actualy a bug in serde as i don't see how passing a deserialized field to two different flattened members will ever result in a valid structure when serializing again....

snpschaaf added a commit to snpschaaf/okapi that referenced this issue Jul 12, 2022
The 'extensions' fields of various structs
within okapi/src/openapi3.rs was not
deserialized correctly.

This commit fixes the behavior if the Object is
an indexmap. Using rocket_okapi enables the feature
'preserve_order' by default and hence an indexmap is used.

If the feature 'preserve_order' is disables, this commit should
not change the behavior.

Belongs to GREsau#67

Signed-off-by: Philippe Schaaf <[email protected]>
snpschaaf added a commit to snpschaaf/okapi that referenced this issue Jul 12, 2022
The 'extensions' fields of various structs
within okapi/src/openapi3.rs was not
deserialized correctly.

This commit fixes the behavior if the Object is
an indexmap. Using rocket_okapi enables the feature
'preserve_order' by default and hence an indexmap is used.

If the feature 'preserve_order' is disables, this commit should
not change the behavior.

Belongs to GREsau#67

Signed-off-by: Philippe Schaaf <[email protected]>
@ralpha ralpha added the okapi This affects the okapi crate label Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
okapi This affects the okapi crate
Projects
None yet
Development

No branches or pull requests

2 participants