From 5f60599c412b4dc71f8cbd5a066bda0938bc158c Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 8 Mar 2021 17:33:49 +0100 Subject: [PATCH] more enum stuff [skip ci] --- openapi_type/tests/custom_types.rs | 65 ++++++++++++++++++++++++++++++ openapi_type_derive/src/codegen.rs | 22 +++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/openapi_type/tests/custom_types.rs b/openapi_type/tests/custom_types.rs index fce5e57..5119033 100644 --- a/openapi_type/tests/custom_types.rs +++ b/openapi_type/tests/custom_types.rs @@ -77,3 +77,68 @@ test_type!(EnumWithOneField = { }, "required": ["Success"] }); + +#[derive(OpenapiType)] +enum EnumWithFields { + Success { value: isize }, + Error { msg: String } +} +test_type!(EnumWithFields = { + "title": "EnumWithFields", + "oneOf": [{ + "type": "object", + "properties": { + "Success": { + "type": "object", + "properties": { + "value": { + "type": "integer" + } + }, + "required": ["value"] + } + }, + "required": ["Success"] + }, { + "type": "object", + "properties": { + "Error": { + "type": "object", + "properties": { + "msg": { + "type": "string" + } + }, + "required": ["msg"] + } + }, + "required": ["Error"] + }] +}); + +#[derive(OpenapiType)] +enum EnumExternallyTagged { + Success { value: isize }, + Error +} +test_type!(EnumExternallyTagged = { + "title": "EnumExternallyTagged", + "oneOf": [{ + "type": "object", + "properties": { + "Success": { + "type": "object", + "properties": { + "value": { + "type": "integer" + } + }, + "required": ["value"] + } + }, + "required": ["Success"] + }, { + "type": "string", + "enum": ["Error"] + }] +}); diff --git a/openapi_type_derive/src/codegen.rs b/openapi_type_derive/src/codegen.rs index 1ad7d02..a56c97c 100644 --- a/openapi_type_derive/src/codegen.rs +++ b/openapi_type_derive/src/codegen.rs @@ -8,8 +8,8 @@ impl ParseData { match self { Self::Struct(fields) => gen_struct(fields), Self::Enum(variants) => gen_enum(variants), - Self::Unit => gen_unit(), - _ => unimplemented!() + Self::Alternatives(alt) => gen_alt(alt), + Self::Unit => gen_unit() } } } @@ -108,6 +108,24 @@ fn gen_enum(variants: &[LitStr]) -> TokenStream { } } +fn gen_alt(alt: &[ParseData]) -> TokenStream { + let openapi = path!(::openapi_type::openapi); + let schema = alt.iter().map(|data| data.gen_schema()); + quote! { + { + let mut alternatives = <::std::vec::Vec< + #openapi::ReferenceOr<#openapi::Schema> + >>::new(); + #(alternatives.push(#openapi::ReferenceOr::Item( + ::openapi_type::OpenapiSchema::new(#schema).into_schema() + ));)* + #openapi::SchemaKind::OneOf { + one_of: alternatives + } + } + } +} + fn gen_unit() -> TokenStream { let openapi = path!(::openapi_type::openapi); quote! {