Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions dropshot/src/schema_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ mod test {

use super::j2oas_schema;
use super::j2oas_schema_object;
use super::make_subschema_for;
use schemars::JsonSchema;

#[test]
Expand Down Expand Up @@ -1030,6 +1031,36 @@ mod test {
);
}

#[test]
fn test_nullable_subschema() {
#[expect(dead_code)]
#[derive(JsonSchema)]
struct Foo {
bar: String,
}
let settings = schemars::r#gen::SchemaSettings::openapi3();
let mut generator = schemars::r#gen::SchemaGenerator::new(settings);

// BUG: Currently, the fact that the type is nullable is dropped from
// the output, leading to the generated schema for Option<Foo> being
// the same as the one for Foo below.
let schema = make_subschema_for::<Option<Foo>>(&mut generator);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, with SchemaSettings::openapiv3() one would expect the generated schema to be something like { allOf: [ { $ref: Foo }], nullable: true }, instead it seems to be:

{
  "$ref": "Foo",
  "nullable": true
}

... which isn't a valid OppenAPI 3.0.x schema

assert_eq!(
j2oas_schema(None, &schema),
openapiv3::ReferenceOr::Reference {
reference: "#/components/schemas/Foo".to_string()
}
);

let schema = make_subschema_for::<Foo>(&mut generator);
assert_eq!(
j2oas_schema(None, &schema),
openapiv3::ReferenceOr::Reference {
reference: "#/components/schemas/Foo".to_string()
}
);
}

/// These cases are errors, but we want to provide good error messages for
/// them.
#[test]
Expand Down
13 changes: 13 additions & 0 deletions dropshot/tests/integration-tests/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,18 @@ async fn handler32(
todo!();
}

#[endpoint {
method = PUT,
path = "/testing33",
tags = ["it"]
}]
async fn handler33(
_: RequestContext<()>,
_: TypedBody<Option<CoolStruct>>,
) -> Result<HttpResponseOk<Option<CoolStruct>>, HttpError> {
todo!();
}

fn make_api(
maybe_tag_config: Option<TagConfig>,
) -> Result<ApiDescription<()>, ApiDescriptionRegisterError> {
Expand Down Expand Up @@ -729,6 +741,7 @@ fn make_api(
api.register(handler30)?;
api.register(handler31)?;
api.register(handler32)?;
api.register(handler33)?;
Ok(api)
}

Expand Down
36 changes: 36 additions & 0 deletions dropshot/tests/test_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,42 @@
}
}
},
"/testing33": {
"put": {
"tags": [
"it"
],
"operationId": "handler33",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CoolStruct"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CoolStruct"
}
}
}
},
"4XX": {
"$ref": "#/components/responses/Error"
},
"5XX": {
"$ref": "#/components/responses/Error"
}
}
}
},
"/thing_with_headers": {
"get": {
"tags": [
Expand Down
36 changes: 36 additions & 0 deletions dropshot/tests/test_openapi_fuller.json
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,42 @@
}
}
},
"/testing33": {
"put": {
"tags": [
"it"
],
"operationId": "handler33",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CoolStruct"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CoolStruct"
}
}
}
},
"4XX": {
"$ref": "#/components/responses/Error"
},
"5XX": {
"$ref": "#/components/responses/Error"
}
}
}
},
"/thing_with_headers": {
"get": {
"tags": [
Expand Down
Loading