#openapi-v3 #swagger #openapi

macro struct2swagger_derive

Create Swagger OpenApi3 from your structs

5 releases

0.1.6 Oct 4, 2020
0.1.5 Oct 3, 2020
0.1.3 Sep 27, 2020
0.1.2 May 8, 2020
0.1.0 Aug 30, 2019

#13 in #openapi-v3

MIT license

49KB
1K SLoC

struct2swagger Build Status

Utilities for generating OpenAPI Specification from your structures

Install

cargo add struct2swagger_derive struct2swagger

Usage


#[macro_use]
extern crate struct2swagger_derive;
#[macro_use]
extern crate struct2swagger;
#[macro_use]
extern crate serde_json;

use struct2swagger::{JsonSchemaDefinition, QueryDefinition, swagger_object::SwaggerObject};
use serde_json::Result;

#[derive(Deserialize, Swagger)]
pub struct Who {
    pub name: Option<String>,
}
#[derive(Serialize, Swagger)]
pub struct HelloWorldResponse {
    pub say: String,
}

fn get_openapi_spec() -> String {
  let mut swagger_object = SwaggerObject::new(
    "the webserver name", // title
    "1.0.0" // version
  );

  swagger_add_router!(
      swagger_object, // obj
      "GET", // method
      "/", // path
      Who, // query parameters
      200, // expected status code
      "say", //  description
      HelloWorldResponse // struct in output
  );

  let json = serde_json::to_string(&address).unwrap();

  json
}

For other examples see tests

Contributing

Every contribution is welcomed: Open an issue and fire a PR!

License

MIT as described here

Dependencies

~0.7–1.6MB
~34K SLoC