oas3-rs icon indicating copy to clipboard operation
oas3-rs copied to clipboard

Stack overflow for recursive types in an openapi schema

Open weiznich opened this issue 2 years ago • 0 comments

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.

weiznich avatar Oct 17 '23 14:10 weiznich