Skip to content

Commit

Permalink
fix #104 duplicated inline response type schemas (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
davepacheco authored Apr 16, 2021
1 parent e04241c commit 8d2fd34
Show file tree
Hide file tree
Showing 7 changed files with 989 additions and 144 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

https://github.com/oxidecomputer/dropshot/compare/v0.5.1\...HEAD[Full list of commits]

* https://github.com/oxidecomputer/dropshot/pull/105[#105] When generating an OpenAPI spec, Dropshot now uses references rather than inline schemas to represent request and response bodies.
* https://github.com/oxidecomputer/dropshot/pull/103[#103] When the Dropshot server is dropped before having been shut down, Dropshot now attempts to gracefully shut down rather than panic.

=== Breaking Changes
Expand Down
11 changes: 9 additions & 2 deletions dropshot/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ where
true,
ApiSchemaGenerator::Gen {
name: BodyType::schema_name,
schema: BodyType::json_schema,
schema: make_subschema_for::<BodyType>,
},
vec![],
);
Expand Down Expand Up @@ -1141,13 +1141,20 @@ where
ApiEndpointResponse {
schema: Some(ApiSchemaGenerator::Gen {
name: T::Body::schema_name,
schema: T::Body::json_schema,
schema: make_subschema_for::<T::Body>,
}),
success: Some(T::STATUS_CODE),
description: Some(T::DESCRIPTION.to_string()),
}
}
}

fn make_subschema_for<T: JsonSchema>(
gen: &mut schemars::gen::SchemaGenerator,
) -> schemars::schema::Schema {
gen.subschema_for::<T>()
}

/**
* `HttpResponseCreated<T: Serialize>` wraps an object of any serializable type.
* It denotes an HTTP 201 "Created" response whose body is generated by
Expand Down
309 changes: 268 additions & 41 deletions dropshot/tests/test_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,159 @@
}
}
},
"/dup1": {
"get": {
"operationId": "handler8",
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedResponseTopLevel"
}
}
}
}
}
}
},
"/dup2": {
"get": {
"operationId": "handler9",
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedResponseTopLevel"
}
}
}
}
}
}
},
"/dup3": {
"put": {
"operationId": "handler10",
"parameters": [
{
"in": "query",
"name": "_b",
"required": true,
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedParamNextLevel"
},
"style": "form"
}
],
"responses": {
"200": {
"description": "successful operation"
}
}
}
},
"/dup4": {
"put": {
"operationId": "handler11",
"parameters": [
{
"in": "query",
"name": "_b",
"required": true,
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedParamNextLevel"
},
"style": "form"
}
],
"responses": {
"200": {
"description": "successful operation"
}
}
}
},
"/dup5": {
"put": {
"operationId": "handler12",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedBodyTopLevel"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "successful operation"
}
}
}
},
"/dup6": {
"put": {
"operationId": "handler13",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedBodyTopLevel"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "successful operation"
}
}
}
},
"/dup7": {
"put": {
"operationId": "handler14",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedTop"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "successful operation"
}
}
}
},
"/dup8": {
"get": {
"operationId": "handler15",
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NeverDuplicatedTop"
}
}
}
}
}
}
},
"/impairment": {
"get": {
"operationId": "handler6",
Expand Down Expand Up @@ -67,25 +220,7 @@
"content": {
"application/json": {
"schema": {
"title": "ResponseItemResultsPage",
"description": "A single page of results",
"type": "object",
"properties": {
"items": {
"description": "list of items on this page of results",
"type": "array",
"items": {
"$ref": "#/components/schemas/ResponseItem"
}
},
"next_page": {
"description": "token used to fetch the next page of results (if any)",
"type": "string"
}
},
"required": [
"items"
]
"$ref": "#/components/schemas/ResponseItemResultsPage"
}
}
}
Expand All @@ -101,16 +236,7 @@
"content": {
"application/json": {
"schema": {
"title": "BodyParam",
"type": "object",
"properties": {
"_x": {
"type": "string"
}
},
"required": [
"_x"
]
"$ref": "#/components/schemas/BodyParam"
}
}
},
Expand All @@ -122,8 +248,7 @@
"content": {
"application/json": {
"schema": {
"title": "Response",
"type": "object"
"$ref": "#/components/schemas/Response"
}
}
}
Expand Down Expand Up @@ -219,16 +344,7 @@
"content": {
"application/json": {
"schema": {
"title": "BodyParam",
"type": "object",
"properties": {
"_x": {
"type": "string"
}
},
"required": [
"_x"
]
"$ref": "#/components/schemas/BodyParam"
}
}
},
Expand Down Expand Up @@ -288,6 +404,86 @@
},
"components": {
"schemas": {
"BodyParam": {
"type": "object",
"properties": {
"_x": {
"type": "string"
}
},
"required": [
"_x"
]
},
"NeverDuplicatedBodyNextLevel": {
"type": "object",
"properties": {
"_v": {
"type": "boolean"
}
},
"required": [
"_v"
]
},
"NeverDuplicatedBodyTopLevel": {
"type": "object",
"properties": {
"_b": {
"$ref": "#/components/schemas/NeverDuplicatedBodyNextLevel"
}
},
"required": [
"_b"
]
},
"NeverDuplicatedNext": {
"type": "object",
"properties": {
"_v": {
"type": "boolean"
}
},
"required": [
"_v"
]
},
"NeverDuplicatedResponseNextLevel": {
"type": "object",
"properties": {
"v": {
"type": "boolean"
}
},
"required": [
"v"
]
},
"NeverDuplicatedResponseTopLevel": {
"type": "object",
"properties": {
"b": {
"$ref": "#/components/schemas/NeverDuplicatedResponseNextLevel"
}
},
"required": [
"b"
]
},
"NeverDuplicatedTop": {
"type": "object",
"properties": {
"_b": {
"$ref": "#/components/schemas/NeverDuplicatedNext"
}
},
"required": [
"_b"
]
},
"Response": {
"type": "object"
},
"ResponseItem": {
"type": "object",
"properties": {
Expand All @@ -298,6 +494,37 @@
"required": [
"word"
]
},
"ResponseItemResultsPage": {
"description": "A single page of results",
"type": "object",
"properties": {
"items": {
"description": "list of items on this page of results",
"type": "array",
"items": {
"$ref": "#/components/schemas/ResponseItem"
}
},
"next_page": {
"description": "token used to fetch the next page of results (if any)",
"type": "string"
}
},
"required": [
"items"
]
},
"NeverDuplicatedParamNextLevel": {
"type": "object",
"properties": {
"_v": {
"type": "boolean"
}
},
"required": [
"_v"
]
}
}
}
Expand Down
Loading

0 comments on commit 8d2fd34

Please sign in to comment.