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

Stack overflow for recursive types in an openapi schema #3

Open
weiznich opened this issue Oct 17, 2023 · 0 comments
Open

Stack overflow for recursive types in an openapi schema #3

weiznich opened this issue Oct 17, 2023 · 0 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@weiznich
Copy link
Contributor

I run into a stack overflow due to a unrestricted recursion in some of my defined schemas while using ValidationTree::from_schema.

Test case:

const SCHEMA: &str = r##"{
  "openapi": "3.0.3",
  "info": {
    "title": "test",
    "version": "0.1.0"
  },
  "paths": {
    "/": {
      "get": {
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Collection"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Collection": {
        "type": "object",
        "properties": {
          "children": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Collection"
            }
          }
        }
      }
    }
  }
}"##;

fn main() {
    let spec: oas3::Spec = serde_json::from_str(SCHEMA).unwrap();

    let c = &spec.paths["/"].get.as_ref().unwrap().responses["200"];
    let c = c.resolve(&spec).unwrap();
    let schema = c.content["application/json"]
        .schema
        .as_ref()
        .unwrap()
        .resolve(&spec)
        .unwrap();

    println!("Fine until here");
    let validation_tree = oas3::validation::ValidationTree::from_schema(&schema, &spec).unwrap();
    dbg!(validation_tree); // required so that the compiler does use
    println!("Does not reach that");
}

Relevant dependencies:

[dependencies]
oas3 = "0.2.1"
serde_json = "1.0.107"

Log output for oas3=trace:

[2023-10-17T14:14:10Z TRACE oas3::spec::r#ref] creating Ref: schemas/Collection
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Object
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding object validators: props children
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Array
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding array validators
[2023-10-17T14:14:10Z TRACE oas3::spec::r#ref] creating Ref: schemas/Collection
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Object
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding object validators: props children
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Array
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding array validators
[2023-10-17T14:14:10Z TRACE oas3::spec::r#ref] creating Ref: schemas/Collection
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Object
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding object validators: props children
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Array
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding array validators
[2023-10-17T14:14:10Z TRACE oas3::spec::r#ref] creating Ref: schemas/Collection
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Object
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding object validators: props children
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] creating validation tree from schema: _unnamed_
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] restricting data type: Array
[2023-10-17T14:14:10Z TRACE oas3::validation::validator] adding array validators
[2023-10-17T14:14:10Z TRACE oas3::spec::r#ref] creating Ref: schemas/Collection

(Repeating that many times more)

It seems like there is just no guarding against this case in the relevant code. It somehow would need to detect that there is some kind of loop and stop trying to resolve it.

@robjtede robjtede added the bug Something isn't working label Oct 20, 2023
@robjtede robjtede added the help wanted Extra attention is needed label Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants