schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Bug `json_schema` returns `$ref`

Open ralpha opened this issue 4 years ago • 0 comments
trafficstars

I'm using the function json_schema and it returns a $ref while explicitly stating it should not do this.

This happens when Option<T> is used. I think this happens here. I'm using the openapi3 settings

There is the code example that triggers this case:

#[derive(JsonSchema)]
struct SortByKey {
    sort_by: String,
}

fn test() {
    let settings = rocket_okapi::settings::OpenApiSettings::default();
    let mut gen = rocket_okapi::gen::OpenApiGenerator::new(&settings);
    let schema = gen.json_schema_no_ref::<Option<SortByKey>>();
    // function above calls: `<Option<SortByKey>>::json_schema(&mut gen.schema_generator).into()`
    dbg!(schema);
}

This will output:

schema = SchemaObject {
    metadata: None,
    instance_type: None,
    format: None,
    enum_values: None,
    const_value: None,
    subschemas: None,
    number: None,
    string: None,
    array: None,
    object: None,
    reference: Some(
        "#/components/schemas/SortByKey",
    ),
    extensions: {
        "nullable": Bool(
            true,
        ),
    },
}

This is thus not what I expected. I hope I can work around this for now, but it might break other parts of the code too.

ralpha avatar Sep 12 '21 20:09 ralpha