Skip to content

Commit

Permalink
Info::from_env() sets License::identifier (#1233)
Browse files Browse the repository at this point in the history
`Info::from_env()` sets `info.license.identifier` along with `info.license.name`.
  • Loading branch information
masnagam authored and juhaku committed Dec 18, 2024
1 parent 24dd6d4 commit 4f09bc8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions utoipa-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

* Fix tagged enum with flatten fields (https://github.com/juhaku/utoipa/pull/1208)

### Changed

* `Info::from_env()` sets `License::identifier` (https://github.com/juhaku/utoipa/pull/1233)

## 5.2.0 - Nov 2024

### Fixed
Expand Down
45 changes: 44 additions & 1 deletion utoipa-gen/src/openapi/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ impl Info<'_> {
Some(contact)
}
});
let license = std::env::var("CARGO_PKG_LICENSE").ok().map(License::from);
let license = std::env::var("CARGO_PKG_LICENSE")
.ok()
.map(|spdx_expr| License {
name: Cow::Owned(spdx_expr.clone()),
// CARGO_PKG_LICENSE contains an SPDX expression as described in the Cargo Book.
// It can be set to `info.license.identifier`.
identifier: Cow::Owned(spdx_expr),
..Default::default()
});

Info {
title: name.map(|name| name.into()),
Expand Down Expand Up @@ -427,4 +435,39 @@ mod tests {
assert!(contact.name.is_none(), "Contact name should be empty");
assert!(contact.email.is_none(), "Contact email should be empty");
}

#[test]
fn info_from_env() {
let info = Info::from_env();

match info.title {
Some(LitStrOrExpr::LitStr(title)) => assert_eq!(title.value(), env!("CARGO_PKG_NAME")),
_ => panic!(),
}

match info.version {
Some(LitStrOrExpr::LitStr(version)) => {
assert_eq!(version.value(), env!("CARGO_PKG_VERSION"))
}
_ => panic!(),
}

match info.description {
Some(LitStrOrExpr::LitStr(description)) => {
assert_eq!(description.value(), env!("CARGO_PKG_DESCRIPTION"))
}
_ => panic!(),
}

assert!(matches!(info.terms_of_service, None));

match info.license {
Some(license) => {
assert_eq!(license.name, env!("CARGO_PKG_LICENSE"));
assert_eq!(license.identifier, env!("CARGO_PKG_LICENSE"));
assert_eq!(license.url, None);
}
None => panic!(),
}
}
}

0 comments on commit 4f09bc8

Please sign in to comment.