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

Implement OaSchema for IpAddr types #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion core/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashMap;
use std::{
collections::HashMap,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
};

use openapiv3 as oa;
use openapiv3::{ReferenceOr, Schema};
Expand Down Expand Up @@ -105,6 +108,11 @@ impl_oa_schema!(f64, Schema::new_number());

impl_oa_schema!(String, Schema::new_string());

impl_oa_schema!(IpAddr, Schema::new_string());
impl_oa_schema!(Ipv6Addr, Schema::new_string());
impl_oa_schema!(Ipv4Addr, Schema::new_string());
impl_oa_schema!(SocketAddr, Schema::new_string());

impl<T> OaSchema for Vec<T> where T: OaSchema {
fn schema_ref() -> ReferenceOr<Schema> {
let inner = T::schema_ref();
Expand Down
1 change: 1 addition & 0 deletions oasgen/tests/test-none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ fn run_tests() {
t.pass("tests/test-none/04-enum.rs");
t.pass("tests/test-none/05-serde-attrs.rs");
t.pass("tests/test-none/06-complex-enum.rs");
t.pass("tests/test-none/07-ipaddr.rs");
}
37 changes: 37 additions & 0 deletions oasgen/tests/test-none/07-ipaddr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use oasgen::{oasgen, OaSchema, Server};
use serde::{Deserialize, Serialize};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

#[derive(Deserialize, Serialize, OaSchema)]
pub struct Addresses {
pub ip: IpAddr,
pub ip6: Ipv6Addr,
pub ip4: Ipv4Addr,
pub sock: SocketAddr,
}

#[derive(Serialize, OaSchema)]
pub struct AddressesResponse {
pub ok: bool,
}

#[derive(OaSchema, Serialize, Deserialize)]
pub struct Foo {
#[oasgen(inline)]
addresses: Addresses,
}

#[oasgen]
async fn addresses(_body: Addresses) -> AddressesResponse {
AddressesResponse { ok: false }
}

fn main() {
use pretty_assertions::assert_eq;

let _ = Server::none().get("/hello", addresses);

let schema = Foo::schema();
let spec = serde_yaml::to_string(&schema).unwrap();
assert_eq!(spec, include_str!("07-ipaddr.yaml"));
}
20 changes: 20 additions & 0 deletions oasgen/tests/test-none/07-ipaddr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type: object
properties:
addresses:
type: object
properties:
ip:
type: string
ip6:
type: string
ip4:
type: string
sock:
type: string
required:
- ip
- ip6
- ip4
- sock
required:
- addresses