Skip to content

Commit

Permalink
Add identifier for Info
Browse files Browse the repository at this point in the history
This commit adds `identifier` field to `Info` what was missing since
upgrading to OpenAPI 3.1.

Fixes #1139
  • Loading branch information
juhaku committed Oct 15, 2024
1 parent ab0693d commit 91636ae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions utoipa-gen/src/openapi/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl ToTokens for Info<'_> {
pub(super) struct License<'l> {
name: Cow<'l, str>,
url: Option<Cow<'l, str>>,
identifier: Cow<'l, str>,
}

impl Parse for License<'_> {
Expand All @@ -200,6 +201,11 @@ impl Parse for License<'_> {
parse_utils::parse_next(input, || input.parse::<LitStr>())?.value(),
))
}
"identifier" => {
license.identifier = Cow::Owned(
parse_utils::parse_next(input, || input.parse::<LitStr>())?.value(),
)
}
_ => {
return Err(Error::new(
ident.span(),
Expand All @@ -222,11 +228,18 @@ impl ToTokens for License<'_> {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let name = &self.name;
let url = self.url.as_ref().map(|url| quote! { .url(Some(#url))});
let identifier = if !self.identifier.is_empty() {
let identifier = self.identifier.as_ref();
quote! { .identifier(Some(#identifier))}
} else {
TokenStream2::new()
};

tokens.extend(quote! {
utoipa::openapi::info::LicenseBuilder::new()
.name(#name)
#url
#identifier
.build()
})
}
Expand Down
20 changes: 20 additions & 0 deletions utoipa-gen/tests/openapi_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,26 @@ fn derive_openapi_with_servers() {
)
}

#[test]
fn derive_openapi_with_licence() {
#[derive(OpenApi)]
#[openapi(info(license(name = "licence_name", identifier = "MIT"), version = "1.0.0",))]
struct ApiDoc;

let value = serde_json::to_value(ApiDoc::openapi()).unwrap();
let info = value.pointer("/info/license");
dbg!(&info);

assert_json_include!(
actual: info,
expected:
json!({
"name": "licence_name",
"identifier": "MIT",
})
)
}

#[test]
fn derive_openapi_with_custom_info() {
#[derive(OpenApi)]
Expand Down
15 changes: 15 additions & 0 deletions utoipa/src/openapi/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ builder! {
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,

/// An [SPDX-Licenses][spdx_licence] expression for the API. The _`identifier`_ field
/// is mutually exclusive of the _`url`_ field. E.g. Apache-2.0
///
/// [spdx_licence]: <https://spdx.org/licenses/>
#[serde(skip_serializing_if = "Option::is_none")]
pub identifier: Option<String>,

/// Optional extensions "x-something".
#[serde(skip_serializing_if = "Option::is_none", flatten)]
pub extensions: Option<Extensions>,
Expand Down Expand Up @@ -245,6 +252,14 @@ impl LicenseBuilder {
pub fn extensions(mut self, extensions: Option<Extensions>) -> Self {
set_value!(self extensions extensions)
}

/// Set identifier of the licence as [SPDX-Licenses][spdx_licence] expression for the API.
/// The _`identifier`_ field is mutually exclusive of the _`url`_ field. E.g. Apache-2.0
///
/// [spdx_licence]: <https://spdx.org/licenses/>
pub fn identifier<S: Into<String>>(mut self, identifier: Option<S>) -> Self {
set_value!(self identifier identifier.map(|identifier| identifier.into()))
}
}

#[cfg(test)]
Expand Down

0 comments on commit 91636ae

Please sign in to comment.