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

encapsulate use of #[derive(JsonSchema)] #70

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions dropshot/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use dropshot::ApiDescription;
use dropshot::ConfigDropshot;
use dropshot::ConfigLogging;
use dropshot::ConfigLoggingLevel;
use dropshot::ExtractedParameter;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpResponseUpdatedNoContent;
use dropshot::HttpServer;
use dropshot::RequestContext;
use dropshot::TypedBody;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use std::any::Any;
Expand Down Expand Up @@ -107,7 +107,7 @@ impl ExampleContext {
* response to a GET request to fetch the counter or as the body of a PUT
* request to update the counter.
*/
#[derive(Deserialize, Serialize, JsonSchema)]
#[derive(Deserialize, Serialize, ExtractedParameter)]
struct CounterValue {
counter: u64,
}
Expand Down
8 changes: 4 additions & 4 deletions dropshot/examples/pagination-basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use dropshot::ConfigDropshot;
use dropshot::ConfigLogging;
use dropshot::ConfigLoggingLevel;
use dropshot::EmptyScanParams;
use dropshot::ExtractedParameter;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpServer;
Expand All @@ -30,7 +31,6 @@ use dropshot::Query;
use dropshot::RequestContext;
use dropshot::ResultsPage;
use dropshot::WhichPage;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
Expand All @@ -42,10 +42,10 @@ use std::sync::Arc;
/**
* Object returned by our paginated endpoint
*
* Like anything returned by Dropshot, we must implement `JsonSchema` and
* Like anything returned by Dropshot, we must implement `ExtractedParameter` and
* `Serialize`. We also implement `Clone` to simplify the example.
*/
#[derive(Clone, JsonSchema, Serialize)]
#[derive(Clone, ExtractedParameter, Serialize)]
struct Project {
name: String,
// lots more fields
Expand All @@ -61,7 +61,7 @@ struct Project {
* include with each page of results, and it must be `Deserialize` to get it
* back in a querystring.
*/
#[derive(Deserialize, JsonSchema, Serialize)]
#[derive(Deserialize, ExtractedParameter, Serialize)]
struct ProjectPage {
name: String,
}
Expand Down
16 changes: 8 additions & 8 deletions dropshot/examples/pagination-multiple-resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use dropshot::ApiDescription;
use dropshot::ConfigDropshot;
use dropshot::ConfigLogging;
use dropshot::ConfigLoggingLevel;
use dropshot::ExtractedParameter;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpServer;
Expand All @@ -21,7 +22,6 @@ use dropshot::Query;
use dropshot::RequestContext;
use dropshot::ResultsPage;
use dropshot::WhichPage;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
Expand All @@ -36,21 +36,21 @@ use uuid::Uuid;
* "name". We'll have one endpoint for each resource to list it.
*/

#[derive(Clone, JsonSchema, Serialize)]
#[derive(Clone, ExtractedParameter, Serialize)]
struct Project {
id: Uuid,
name: String,
// lots more project-like fields
}

#[derive(Clone, JsonSchema, Serialize)]
#[derive(Clone, ExtractedParameter, Serialize)]
struct Disk {
id: Uuid,
name: String,
// lots more disk-like fields
}

#[derive(Clone, JsonSchema, Serialize)]
#[derive(Clone, ExtractedParameter, Serialize)]
struct Instance {
id: Uuid,
name: String,
Expand Down Expand Up @@ -88,7 +88,7 @@ impl_HasIdentity!(Instance);
/*
* Pagination-related types
*/
#[derive(Deserialize, Clone, JsonSchema, Serialize)]
#[derive(Deserialize, Clone, ExtractedParameter, Serialize)]
struct ExScanParams {
#[serde(default = "default_sort_mode")]
sort: ExSortMode,
Expand All @@ -98,7 +98,7 @@ fn default_sort_mode() -> ExSortMode {
ExSortMode::ByNameAscending
}

#[derive(Deserialize, Clone, JsonSchema, Serialize)]
#[derive(Deserialize, Clone, ExtractedParameter, Serialize)]
#[serde(rename_all = "kebab-case")]
enum ExSortMode {
ByIdAscending,
Expand All @@ -107,7 +107,7 @@ enum ExSortMode {
ByNameDescending,
}

#[derive(Debug, Deserialize, JsonSchema, Serialize)]
#[derive(Debug, Deserialize, ExtractedParameter, Serialize)]
#[serde(rename_all = "kebab-case")]
enum ExPageSelector {
Id(PaginationOrder, Uuid),
Expand Down Expand Up @@ -251,7 +251,7 @@ fn do_list<'a, T>(
by_id: &'a BTreeMap<Uuid, Arc<T>>,
) -> ItemIter<'a, T>
where
T: Clone + JsonSchema + Serialize + Send + Sync + 'static,
T: Clone + ExtractedParameter + Serialize + Send + Sync + 'static,
{
match p {
WhichPage::First(_) => match scan_params.sort {
Expand Down
12 changes: 6 additions & 6 deletions dropshot/examples/pagination-multiple-sorts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ use dropshot::ApiDescription;
use dropshot::ConfigDropshot;
use dropshot::ConfigLogging;
use dropshot::ConfigLoggingLevel;
use dropshot::ExtractedParameter;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpServer;
Expand All @@ -110,7 +111,6 @@ use dropshot::RequestContext;
use dropshot::ResultsPage;
use dropshot::WhichPage;
use hyper::Uri;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
Expand All @@ -125,10 +125,10 @@ extern crate slog;
/**
* Item returned by our paginated endpoint
*
* Like anything returned by Dropshot, we must implement `JsonSchema` and
* Like anything returned by Dropshot, we must implement `ExtractedParameter` and
* `Serialize`. We also implement `Clone` to simplify the example.
*/
#[derive(Clone, JsonSchema, Serialize)]
#[derive(Clone, ExtractedParameter, Serialize)]
struct Project {
name: String,
mtime: DateTime<Utc>,
Expand All @@ -148,7 +148,7 @@ struct Project {
* serialize it using `serde_querystring`. That code could fail at runtime for
* certain types of values (e.g., enum variants that contain data).
*/
#[derive(Clone, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Deserialize, ExtractedParameter, Serialize)]
struct ProjectScanParams {
#[serde(default = "default_project_sort")]
sort: ProjectSort,
Expand All @@ -158,7 +158,7 @@ fn default_project_sort() -> ProjectSort {
ProjectSort::ByNameAscending
}

#[derive(Deserialize, Clone, JsonSchema, Serialize)]
#[derive(Deserialize, Clone, ExtractedParameter, Serialize)]
#[serde(rename_all = "kebab-case")]
enum ProjectSort {
/** by name ascending */
Expand Down Expand Up @@ -186,7 +186,7 @@ enum ProjectSort {
* selector back, you find the object having the next value after the one stored
* in the token and start returning results from there.
*/
#[derive(Deserialize, JsonSchema, Serialize)]
#[derive(Deserialize, ExtractedParameter, Serialize)]
#[serde(rename_all = "kebab-case")]
enum ProjectScanPageSelector {
Name(PaginationOrder, String),
Expand Down
19 changes: 9 additions & 10 deletions dropshot/examples/petstore.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use dropshot::{
endpoint, ApiDescription, HttpError, HttpResponseOk, PaginationParams,
Path, Query, RequestContext, ResultsPage, TypedBody,
endpoint, ApiDescription, ExtractedParameter, HttpError, HttpResponseOk,
PaginationParams, Path, Query, RequestContext, ResultsPage, TypedBody,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

Expand Down Expand Up @@ -33,7 +32,7 @@ fn main() -> Result<(), String> {
}

#[allow(dead_code)]
#[derive(Deserialize, Serialize, JsonSchema)]
#[derive(Deserialize, Serialize, ExtractedParameter)]
#[serde(rename_all = "camelCase")]
struct Pet {
id: Option<i64>,
Expand All @@ -47,20 +46,20 @@ struct Pet {
}

#[allow(dead_code)]
#[derive(Deserialize, Serialize, JsonSchema)]
#[derive(Deserialize, Serialize, ExtractedParameter)]
#[serde(rename_all = "camelCase")]
struct Category {
id: i64,
name: String,
}

#[allow(dead_code)]
#[derive(Deserialize, Serialize, JsonSchema)]
#[derive(Deserialize, Serialize, ExtractedParameter)]
#[serde(rename_all = "camelCase")]
struct Tag {}

#[allow(dead_code)]
#[derive(Deserialize, Serialize, JsonSchema)]
#[derive(Deserialize, Serialize, ExtractedParameter)]
#[serde(rename_all = "lowercase")]
enum PetStatus {
Available,
Expand All @@ -69,7 +68,7 @@ enum PetStatus {
}

#[allow(dead_code)]
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize, ExtractedParameter)]
#[serde(rename_all = "camelCase")]
struct PathParams {
pet_id: i64,
Expand Down Expand Up @@ -113,15 +112,15 @@ async fn update_pet_with_form(
}

#[allow(dead_code)]
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize, ExtractedParameter)]
#[serde(rename_all = "camelCase")]
struct FindByTagsScanParams {
/// Tags to filter for
tags: String,
}

#[allow(dead_code)]
#[derive(Serialize, Deserialize, JsonSchema)]
#[derive(Serialize, Deserialize, ExtractedParameter)]
#[serde(rename_all = "camelCase")]
struct FindByTagsPageSelector {
tags: String,
Expand Down
12 changes: 6 additions & 6 deletions dropshot/src/api_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,14 @@ mod test {
use super::j2oas_schema;
use super::ApiDescription;
use super::ApiEndpoint;
use crate::ExtractedParameter;
use http::Method;
use hyper::Body;
use hyper::Response;
use schemars::JsonSchema;
use serde::Deserialize;
use std::sync::Arc;

#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize, ExtractedParameter)]
#[allow(dead_code)]
struct TestPath {
a: String,
Expand Down Expand Up @@ -927,7 +927,7 @@ mod test {

#[test]
fn test_empty_struct() {
#[derive(JsonSchema)]
#[derive(ExtractedParameter)]
struct Empty {}

let settings = schemars::gen::SchemaSettings::openapi3();
Expand All @@ -940,7 +940,7 @@ mod test {
#[test]
fn test_garbage_barge_structure_conversion() {
#[allow(dead_code)]
#[derive(JsonSchema)]
#[derive(ExtractedParameter)]
struct SuperGarbage {
string: String,
strings: Vec<String>,
Expand All @@ -952,7 +952,7 @@ mod test {
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[derive(ExtractedParameter)]
struct Substruct {
ii32: i32,
uu64: u64,
Expand All @@ -962,7 +962,7 @@ mod test {
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[derive(ExtractedParameter)]
enum Union {
A { a: u32 },
B { b: f32 },
Expand Down
Loading