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

Add identifier for Info #1140

Merged
merged 1 commit into from
Oct 15, 2024
Merged
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
1 change: 1 addition & 0 deletions utoipa-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

* Add `identifier` for `Info` (https://github.com/juhaku/utoipa/pull/1140)
* Add `no_recursion` feature for `ToSchema` (https://github.com/juhaku/utoipa/pull/1137)

### Fixed
Expand Down
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
1 change: 1 addition & 0 deletions utoipa/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ to look into changes introduced to **`utoipa-gen`**.

### Added

* Add `identifier` for `Info` (https://github.com/juhaku/utoipa/pull/1140)
* Implement schema traits for indexset (https://github.com/juhaku/utoipa/pull/1129)
* Add `ToSchema` implementation for serde_json::Value (https://github.com/juhaku/utoipa/pull/1132)

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
Loading