Skip to content

Commit

Permalink
Rename Component to Schema (juhaku#243)
Browse files Browse the repository at this point in the history
Rename `Component` trait to `ToSchema` trait and rename `component` attribute
to `schema` attribute. Also rename `ComponentFormat` to `SchemaFormat` and 
rename `ComponentType` to `SchemaType`. 

Fix broken tests and rename component references as schema. Fix broken CI builld
and test.sh scripts. 

Make `Components::schema` and `Components::schemas_from_iter` accept Into<RefOr<Schema>>.
To allow references within `components`.
  • Loading branch information
kellpossible authored Aug 8, 2022
1 parent 29216ed commit 4c6b1e5
Show file tree
Hide file tree
Showing 36 changed files with 732 additions and 689 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ jobs:
if [[ "${{ matrix.testset }}" == "utoipa" ]] && [[ ${{ steps.changes.outputs.root_changed }} == true ]]; then
cargo test --features uuid,openapi_extensions
cargo test --test path_response_derive_test_no_serde_json --no-default-features
cargo test --test component_derive_no_serde_json --no-default-features
cargo test --test schema_derive_no_serde_json --no-default-features
cargo test --test path_derive_actix --test path_parameter_derive_actix --features actix_extras
cargo test --test component_derive_test --features chrono,decimal,uuid
cargo test --test component_derive_test --features chrono_with_format
cargo test --test schema_derive_test --features chrono,decimal,uuid
cargo test --test schema_derive_test --features chrono_with_format
cargo test --test path_derive_rocket --features rocket_extras,json
cargo test --test path_derive_axum_test --features axum_extras,json
cargo test --test utoipa_gen_test --features json,yaml
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ Please read the incoming changes from here: https://github.com/juhaku/utoipa/dis

## Examples

Create a struct or it could be an enum also. Add `Component` derive macro to it so it can be registered
as a component in OpenApi schema.
Create a struct or it could be an enum also. Add `ToSchema` derive macro to it so it can be registered
as an an OpenAPI schema.
```rust
use utoipa::Component;
use utoipa::ToSchema;

#[derive(Component)]
#[derive(ToSchema)]
struct Pet {
id: u64,
name: String,
Expand Down Expand Up @@ -145,12 +145,12 @@ mod pet_api {
}
```

Tie the `Component` and the endpoint above to the OpenApi schema with following `OpenApi` derive proc macro.
Tie the `Schema` and the endpoint above to the OpenApi schema with following `OpenApi` derive proc macro.
```rust
use utoipa::OpenApi;

#[derive(OpenApi)]
#[openapi(handlers(pet_api::get_pet_by_id), components(Pet))]
#[openapi(handlers(pet_api::get_pet_by_id), components(schemas(Pet)))]
struct ApiDoc;

println!("{}", ApiDoc::openapi().to_pretty_json().unwrap());
Expand Down
10 changes: 5 additions & 5 deletions examples/rocket-todo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ mod todo {
FromForm, Request, State,
};
use serde::{Deserialize, Serialize};
use utoipa::{Component, IntoParams};
use utoipa::{ToSchema, IntoParams};

pub(super) type TodoStore = Arc<Mutex<Vec<Todo>>>;

/// Todo operation error.
#[derive(Serialize, Component, Responder, Debug)]
#[derive(Serialize, ToSchema, Responder, Debug)]
pub(super) enum TodoError {
/// When there is conflict creating a new todo.
#[response(status = 409)]
Expand Down Expand Up @@ -144,13 +144,13 @@ mod todo {
}

/// Task to do.
#[derive(Serialize, Deserialize, Component, Clone)]
#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub(super) struct Todo {
/// Unique todo id.
#[component(example = 1)]
#[schema(example = 1)]
id: i32,
/// Description of a taks.
#[component(example = "Buy groceries")]
#[schema(example = "Buy groceries")]
value: String,
/// Indicatation whether task is done or not.
done: bool,
Expand Down
14 changes: 7 additions & 7 deletions examples/todo-actix/src/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use actix_web::{
HttpResponse, Responder,
};
use serde::{Deserialize, Serialize};
use utoipa::{Component, IntoParams};
use utoipa::{ToSchema, IntoParams};

use crate::{LogApiKey, RequireApiKey};

Expand All @@ -29,30 +29,30 @@ pub(super) fn configure(store: Data<TodoStore>) -> impl FnOnce(&mut ServiceConfi
}

/// Task to do.
#[derive(Serialize, Deserialize, Component, Clone, Debug)]
#[derive(Serialize, Deserialize, ToSchema, Clone, Debug)]
pub(super) struct Todo {
/// Unique id for the todo item.
#[component(example = 1)]
#[schema(example = 1)]
id: i32,
/// Description of the taks to do.
#[component(example = "Remember to buy groceries")]
#[schema(example = "Remember to buy groceries")]
value: String,
/// Mark is the task done or not
checked: bool,
}

/// Request to update existing `Todo` item.
#[derive(Serialize, Deserialize, Component, Clone, Debug)]
#[derive(Serialize, Deserialize, ToSchema, Clone, Debug)]
pub(super) struct TodoUpdateRequest {
/// Optional new value for the `Todo` task.
#[component(example = "Dentist at 14.00")]
#[schema(example = "Dentist at 14.00")]
value: Option<String>,
/// Optional check status to mark is the task done or not.
checked: Option<bool>,
}

/// Todo endpoint error responses
#[derive(Serialize, Deserialize, Clone, Component)]
#[derive(Serialize, Deserialize, Clone, ToSchema)]
pub(super) enum ErrorResponse {
/// When Todo is not found by search term.
NotFound(String),
Expand Down
14 changes: 7 additions & 7 deletions examples/todo-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,31 @@ mod todo {
use hyper::{HeaderMap, StatusCode};
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use utoipa::{Component, IntoParams};
use utoipa::{ToSchema, IntoParams};

/// In-memonry todo store
pub(super) type Store = Mutex<Vec<Todo>>;

/// Item to do.
#[derive(Serialize, Deserialize, Component, Clone)]
#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub(super) struct Todo {
id: i32,
#[component(example = "Buy groceries")]
#[schema(example = "Buy groceries")]
value: String,
done: bool,
}

/// Todo operation errors
#[derive(Serialize, Deserialize, Component)]
#[derive(Serialize, Deserialize, ToSchema)]
pub(super) enum TodoError {
/// Todo already exists conflict.
#[component(example = "Todo already exists")]
#[schema(example = "Todo already exists")]
Conflict(String),
/// Todo not found by id.
#[component(example = "id = 1")]
#[schema(example = "id = 1")]
NotFound(String),
/// Todo operation unauthorized
#[component(example = "missing api key")]
#[schema(example = "missing api key")]
Unauthorized(String),
}

Expand Down
10 changes: 5 additions & 5 deletions examples/todo-tide/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ mod todo {
use serde::{Deserialize, Serialize};
use serde_json::json;
use tide::{Request, Response};
use utoipa::Component;
use utoipa::ToSchema;

/// Item to complete
#[derive(Serialize, Deserialize, Component, Clone)]
#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub(super) struct Todo {
/// Unique database id for `Todo`
#[component(example = 1)]
#[schema(example = 1)]
id: i32,
/// Description of task to complete
#[component(example = "Buy coffee")]
#[schema(example = "Buy coffee")]
value: String,
/// Indicates whether task is done or not
done: bool,
}

/// Error that might occur when managing `Todo` items
#[derive(Serialize, Deserialize, Component)]
#[derive(Serialize, Deserialize, ToSchema)]
pub(super) enum TodoError {
/// Happens when Todo item alredy exists
Config(String),
Expand Down
10 changes: 5 additions & 5 deletions examples/todo-warp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ mod todo {
};

use serde::{Deserialize, Serialize};
use utoipa::{Component, IntoParams};
use utoipa::{ToSchema, IntoParams};
use warp::{hyper::StatusCode, Filter, Reply};

pub type Store = Arc<Mutex<Vec<Todo>>>;

/// Item to complete.
#[derive(Serialize, Deserialize, Component, Clone)]
#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub struct Todo {
/// Unique database id.
#[component(example = 1)]
#[schema(example = 1)]
id: i64,
/// Description of what need to be done.
#[component(example = "Buy movie tickets")]
#[schema(example = "Buy movie tickets")]
value: String,
}

#[derive(Debug, Deserialize, Component)]
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum Order {
AscendingId,
Expand Down
6 changes: 3 additions & 3 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ echo "Testing crate: $crate..."
if [[ "$crate" == "utoipa" ]]; then
cargo test --features uuid
cargo test --test path_response_derive_test_no_serde_json --no-default-features
cargo test --test component_derive_no_serde_json --no-default-features
cargo test --test schema_derive_no_serde_json --no-default-features
cargo test --test path_derive_actix --test path_parameter_derive_actix --features actix_extras
cargo test --test component_derive_test --features chrono,decimal,uuid
cargo test --test component_derive_test --features chrono_with_format
cargo test --test schema_derive_test --features chrono,decimal,uuid
cargo test --test schema_derive_test --features chrono_with_format
cargo test --test path_derive_rocket --features rocket_extras,json
cargo test --test path_derive_axum_test --features axum_extras,json
elif [[ "$crate" == "utoipa-gen" ]]; then
Expand Down
Loading

0 comments on commit 4c6b1e5

Please sign in to comment.