Use wire JSON schema for conflict items (#11196)

## Summary

Closes https://github.com/astral-sh/uv/issues/11180.
This commit is contained in:
Charlie Marsh 2025-02-03 16:22:13 -05:00 committed by GitHub
parent 1be8ba7df1
commit efbc77bc37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 64 deletions

View file

@ -5,9 +5,7 @@ use uv_normalize::{ExtraName, GroupName, PackageName};
/// This is useful to force the resolver to fork according to extras that have
/// unavoidable conflicts with each other. (The alternative is that resolution
/// will fail.)
#[derive(
Debug, Default, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize, schemars::JsonSchema,
)]
#[derive(Debug, Default, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct Conflicts(Vec<ConflictSet>);
impl Conflicts {
@ -60,7 +58,7 @@ impl Conflicts {
///
/// A `TryFrom<Vec<ConflictItem>>` impl may be used to build a set from a
/// sequence. Note though that at least 2 items are required.
#[derive(Debug, Default, Clone, Eq, PartialEq, serde::Serialize, schemars::JsonSchema)]
#[derive(Debug, Default, Clone, Eq, PartialEq, serde::Serialize)]
pub struct ConflictSet(Vec<ConflictItem>);
impl ConflictSet {
@ -120,16 +118,7 @@ impl TryFrom<Vec<ConflictItem>> for ConflictSet {
/// Each item is a pair of a package and a corresponding extra name for that
/// package.
#[derive(
Debug,
Clone,
Eq,
Hash,
PartialEq,
PartialOrd,
Ord,
serde::Deserialize,
serde::Serialize,
schemars::JsonSchema,
Debug, Clone, Eq, Hash, PartialEq, PartialOrd, Ord, serde::Deserialize, serde::Serialize,
)]
#[serde(
deny_unknown_fields,
@ -252,7 +241,7 @@ impl hashbrown::Equivalent<ConflictItem> for ConflictItemRef<'_> {
/// The actual conflicting data for a package.
///
/// That is, either an extra or a group name.
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd, Ord, schemars::JsonSchema)]
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum ConflictPackage {
Extra(ExtraName),
Group(GroupName),
@ -426,7 +415,7 @@ impl SchemaConflicts {
/// schema format does not allow specifying the package name (or will make it
/// optional in the future), where as the in-memory format needs the package
/// name.
#[derive(Debug, Default, Clone, Eq, PartialEq, serde::Serialize, schemars::JsonSchema)]
#[derive(Debug, Default, Clone, Eq, PartialEq, schemars::JsonSchema, serde::Serialize)]
pub struct SchemaConflictSet(Vec<SchemaConflictItem>);
/// Like [`ConflictItem`], but for deserialization in `pyproject.toml`.
@ -436,16 +425,7 @@ pub struct SchemaConflictSet(Vec<SchemaConflictItem>);
/// optional in the future), where as the in-memory format needs the package
/// name.
#[derive(
Debug,
Clone,
Eq,
Hash,
PartialEq,
PartialOrd,
Ord,
serde::Deserialize,
serde::Serialize,
schemars::JsonSchema,
Debug, Clone, Eq, Hash, PartialEq, PartialOrd, Ord, serde::Deserialize, serde::Serialize,
)]
#[serde(
deny_unknown_fields,
@ -457,6 +437,16 @@ pub struct SchemaConflictItem {
conflict: ConflictPackage,
}
impl schemars::JsonSchema for SchemaConflictItem {
fn schema_name() -> String {
"SchemaConflictItem".to_string()
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<ConflictItemWire as schemars::JsonSchema>::json_schema(gen)
}
}
impl<'de> serde::Deserialize<'de> for SchemaConflictSet {
fn deserialize<D>(deserializer: D) -> Result<SchemaConflictSet, D::Error>
where
@ -480,7 +470,11 @@ impl TryFrom<Vec<SchemaConflictItem>> for SchemaConflictSet {
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
/// A single item in a conflicting set.
///
/// Each item is a pair of an (optional) package and a corresponding extra or group name for that
/// package.
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
struct ConflictItemWire {
#[serde(default)]
package: Option<PackageName>,