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

Unable to deserialize and then serialize SecurityScheme into JSON #75

Open
Flowneee opened this issue Mar 3, 2022 · 0 comments
Open
Labels
bug Something isn't working okapi This affects the okapi crate

Comments

@Flowneee
Copy link

Flowneee commented Mar 3, 2022

Hi. There is problem with deserializing and then serializing security scheme into JSON. I have a code which parse spec from YAML, then attempt to serialize it into both YAML and JSON. YAML is valid, but JSON produces duplicated fields type and scheme:

use okapi::openapi3::OpenApi;

static SPEC_ROOT: &str = r##"openapi: 3.0.3
info:
  title: CARDI API
  version: 0.14.0
paths: {}
security:
  - BasicAuth: []
components:
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
"##;

let mut spec: OpenApi = serde_yaml::from_str(SPEC_ROOT).unwrap();
println!(
    "DEBUG: {:#?}\nJSON: {}\nYAML: {}", 
    spec, 
    serde_json::to_string_pretty(&spec).unwrap(), 
    serde_yaml::to_string(&spec).unwrap()
);

Problem is that fields type and scheme ends up in both data and extensions fields. Since it looks like some serde limitation/bug, proper solution could be custom deserialization. If this approach is OK, I could try to implement it.

I believe this might be related to #74.

Debug output

DEBUG: OpenApi {
    openapi: "3.0.3",
    info: Info {
        title: "CARDI API",
        description: None,
        terms_of_service: None,
        contact: None,
        license: None,
        version: "0.14.0",
        extensions: {},
    },
    servers: [],
    paths: {},
    components: Some(
        Components {
            schemas: {},
            responses: {},
            parameters: {},
            examples: {},
            request_bodies: {},
            headers: {},
            security_schemes: {
                "BasicAuth": Object(
                    SecurityScheme {
                        description: None,
                        data: Http {
                            scheme: "basic",
                            bearer_format: None,
                        },
                        extensions: {
                            "scheme": String(
                                "basic",
                            ),
                            "type": String(
                                "http",
                            ),
                        },
                    },
                ),
            },
            links: {},
            callbacks: {},
            extensions: {},
        },
    ),
    security: [
        {
            "BasicAuth": [],
        },
    ],
    tags: [],
    external_docs: None,
    extensions: {},
}

JSON output (invalid)

{
  "openapi": "3.0.3",
  "info": {
    "title": "CARDI API",
    "version": "0.14.0"
  },
  "paths": {},
  "components": {
    "securitySchemes": {
      "BasicAuth": {
        "type": "http",
        "scheme": "basic",
        "scheme": "basic",
        "type": "http"
      }
    }
  },
  "security": [
    {
      "BasicAuth": []
    }
  ]
}

YAML output (ok)

---
openapi: 3.0.3
info:
  title: CARDI API
  version: 0.14.0
paths: {}
components:
  securitySchemes:
    BasicAuth:
      scheme: basic
      type: http
security:
  - BasicAuth: []
@ralpha ralpha added bug Something isn't working okapi This affects the okapi crate labels Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working okapi This affects the okapi crate
Projects
None yet
Development

No branches or pull requests

2 participants