mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-05-09 08:00:41 +00:00
basic structure for openapi_type crate
This commit is contained in:
parent
2251c29d7b
commit
90870e3b6a
9 changed files with 261 additions and 1 deletions
20
openapi_type/Cargo.toml
Normal file
20
openapi_type/Cargo.toml
Normal file
|
@ -0,0 +1,20 @@
|
|||
# -*- eval: (cargo-minor-mode 1) -*-
|
||||
|
||||
[package]
|
||||
workspace = ".."
|
||||
name = "openapi_type"
|
||||
version = "0.1.0-dev"
|
||||
authors = ["Dominic Meiser <git@msrd0.de>"]
|
||||
edition = "2018"
|
||||
description = "OpenAPI type information for Rust structs and enums"
|
||||
keywords = ["openapi", "type"]
|
||||
license = "Apache-2.0"
|
||||
repository = "https://gitlab.com/msrd0/gotham-restful/-/tree/master/openapi_type"
|
||||
|
||||
[dependencies]
|
||||
indexmap = "1.6"
|
||||
openapi_type_derive = "0.1.0-dev"
|
||||
openapiv3 = "=0.3.2"
|
||||
|
||||
[dev-dependencies]
|
||||
trybuild = "1.0"
|
76
openapi_type/src/lib.rs
Normal file
76
openapi_type/src/lib.rs
Normal file
|
@ -0,0 +1,76 @@
|
|||
#![warn(missing_debug_implementations, rust_2018_idioms)]
|
||||
#![forbid(unsafe_code)]
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(clippy::tabs_in_doc_comments))]
|
||||
/*!
|
||||
TODO
|
||||
*/
|
||||
|
||||
pub use indexmap;
|
||||
pub use openapi_type_derive::OpenapiType;
|
||||
pub use openapiv3 as openapi;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use openapi::{Schema, SchemaData, SchemaKind};
|
||||
|
||||
// TODO update the documentation
|
||||
/**
|
||||
This struct needs to be available for every type that can be part of an OpenAPI Spec. It is
|
||||
already implemented for primitive types, String, Vec, Option and the like. To have it available
|
||||
for your type, simply derive from [OpenapiType].
|
||||
*/
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct OpenapiSchema {
|
||||
/// The name of this schema. If it is None, the schema will be inlined.
|
||||
pub name: Option<String>,
|
||||
/// Whether this particular schema is nullable. Note that there is no guarantee that this will
|
||||
/// make it into the final specification, it might just be interpreted as a hint to make it
|
||||
/// an optional parameter.
|
||||
pub nullable: bool,
|
||||
/// The actual OpenAPI schema.
|
||||
pub schema: SchemaKind,
|
||||
/// Other schemas that this schema depends on. They will be included in the final OpenAPI Spec
|
||||
/// along with this schema.
|
||||
pub dependencies: IndexMap<String, OpenapiSchema>
|
||||
}
|
||||
|
||||
impl OpenapiSchema {
|
||||
/// Create a new schema that has no name.
|
||||
pub fn new(schema: SchemaKind) -> Self {
|
||||
Self {
|
||||
name: None,
|
||||
nullable: false,
|
||||
schema,
|
||||
dependencies: IndexMap::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this schema to a [Schema] that can be serialized to the OpenAPI Spec.
|
||||
pub fn into_schema(self) -> Schema {
|
||||
Schema {
|
||||
schema_data: SchemaData {
|
||||
nullable: self.nullable,
|
||||
title: self.name,
|
||||
..Default::default()
|
||||
},
|
||||
schema_kind: self.schema
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
This trait needs to be implemented by every type that is being used in the OpenAPI Spec. It gives
|
||||
access to the [OpenapiSchema] of this type. It is provided for primitive types, String and the
|
||||
like. For use on your own types, there is a derive macro:
|
||||
|
||||
```
|
||||
# #[macro_use] extern crate openapi_type_derive;
|
||||
#
|
||||
#[derive(OpenapiType)]
|
||||
struct MyResponse {
|
||||
message: String
|
||||
}
|
||||
```
|
||||
*/
|
||||
pub trait OpenapiType {
|
||||
fn schema() -> OpenapiSchema;
|
||||
}
|
12
openapi_type/tests/fail/generics_not_openapitype.rs
Normal file
12
openapi_type/tests/fail/generics_not_openapitype.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use openapi_type::OpenapiType;
|
||||
|
||||
#[derive(OpenapiType)]
|
||||
struct Foo<T> {
|
||||
bar: T
|
||||
}
|
||||
|
||||
struct Bar;
|
||||
|
||||
fn main() {
|
||||
<Foo<Bar>>::schema();
|
||||
}
|
21
openapi_type/tests/fail/generics_not_openapitype.stderr
Normal file
21
openapi_type/tests/fail/generics_not_openapitype.stderr
Normal file
|
@ -0,0 +1,21 @@
|
|||
error[E0599]: no function or associated item named `schema` found for struct `Foo<Bar>` in the current scope
|
||||
--> $DIR/generics_not_openapitype.rs:11:14
|
||||
|
|
||||
4 | struct Foo<T> {
|
||||
| -------------
|
||||
| |
|
||||
| function or associated item `schema` not found for this
|
||||
| doesn't satisfy `Foo<Bar>: OpenapiType`
|
||||
...
|
||||
8 | struct Bar;
|
||||
| ----------- doesn't satisfy `Bar: OpenapiType`
|
||||
...
|
||||
11 | <Foo<Bar>>::schema();
|
||||
| ^^^^^^ function or associated item not found in `Foo<Bar>`
|
||||
|
|
||||
= note: the method `schema` exists but the following trait bounds were not satisfied:
|
||||
`Bar: OpenapiType`
|
||||
which is required by `Foo<Bar>: OpenapiType`
|
||||
= help: items from traits can only be used if the trait is implemented and in scope
|
||||
= note: the following trait defines an item `schema`, perhaps you need to implement it:
|
||||
candidate #1: `OpenapiType`
|
6
openapi_type/tests/pass/unit_struct.rs
Normal file
6
openapi_type/tests/pass/unit_struct.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use openapi_type_derive::OpenapiType;
|
||||
|
||||
#[derive(OpenapiType)]
|
||||
struct Foo;
|
||||
|
||||
fn main() {}
|
8
openapi_type/tests/trybuild.rs
Normal file
8
openapi_type/tests/trybuild.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
use trybuild::TestCases;
|
||||
|
||||
#[test]
|
||||
fn trybuild() {
|
||||
let t = TestCases::new();
|
||||
t.pass("tests/pass/*.rs");
|
||||
t.compile_fail("tests/fail/*.rs");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue