oxidecomputer / oxidecomputer/typify

"anyOf" with "required" doesn't appear to be generating the right enum variants

Open
#669 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

anyOf
Dominant language
Rust
Stars
898
Forks
114
Avg merge
4h 18m
Merged PRs (30d)
14

Description

I am running against v0.1.0.

I have a type definition in the json schema called ResourceDescriptor. It should require at least one of content, digest, uri fields to be set. However, what I'm seeing is that it makes them exclusive with each other.

Here's my json schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "SummarySCAI",
  "type": "object",
  "properties": {
    "_type": {
      "type": "string"
    },
    "subject": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/ResourceDescriptor"
      }
    },
    "predicateType": {
      "type": "string"
    },
    "predicate": {
      "type": "object",
      "properties": {
        "attributes": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "attribute": {
                "type": "string",
                "enum": [
                  "PASSED_DEVELOPMENT_ENVIRONMENT",
                  "PASSED_SOURCE",
                  "PASSED_BUILD",
                  "PASSED_PACKAGE",
                  "PASSED_DEPLOY"
                ]
              },
              "conditions": {
                "type": "object",
                "properties": {
                  "policy": {
                    "type": "string"
                  }
                }
              },
              "evidence": {
                "$ref": "#/$defs/ResourceDescriptor"
              }
            },
            "required": ["attribute", "evidence"]
          }
        },
        "producer": {
          "$ref": "#/$defs/ResourceDescriptor"
        }
      },
      "required": ["attributes", "producer"]
    }
  },
  "required": ["_type", "subject", "predicateType", "predicate"],
  "$defs": {
    "ResourceDescriptor": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "uri": {
          "type": "string"
        },
        "digest": {
          "type": "object",
          "properties": {
            "sha256": {
              "type": "string"
            }
          },
          "required": ["sha256"]
        },
        "content": {
          "type": "string"
        },
        "downloadLocation": {
          "type": "string"
        },
        "mediaType": {
          "type": "string"
        },
        "annotations": {
          "type": "object",
          "additionalProperties": true
        }
      },
      "anyOf": [
        {
          "required": ["uri"]
        },
        {
          "required": ["digest"]
        },
        {
          "required": ["content"]
        }
      ],
      "additionalProperties": false
    }
  }
}

It generates an enum like:

pub enum ResourceDescriptor {
    Variant0 {
        #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
        annotations: serde_json::Map<String, serde_json::Value>,
        #[serde(
            rename = "downloadLocation",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        download_location: Option<String>,
        #[serde(rename = "mediaType", default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        uri: String,
    },
    Variant1 {
        #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
        annotations: serde_json::Map<String, serde_json::Value>,
        digest: ResourceDescriptorVariant1Digest,
        #[serde(
            rename = "downloadLocation",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        download_location: Option<String>,
        #[serde(rename = "mediaType", default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    },
    Variant2 {
        #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
        annotations: serde_json::Map<String, serde_json::Value>,
        content: String,
        #[serde(
            rename = "downloadLocation",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        download_location: Option<String>,
        #[serde(rename = "mediaType", default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    },
}

I would expect the enum variants to look more like:

pub enum ResourceDescriptor {
    Variant0 {
        #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
        annotations: serde_json::Map<String, serde_json::Value>,
        content: Option<String>,
        digest: Option<ResourceDescriptorVariant1Digest>,
        #[serde(
            rename = "downloadLocation",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        download_location: Option<String>,
        #[serde(rename = "mediaType", default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        uri: String,
    },
    Variant1 {
        #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
        annotations: serde_json::Map<String, serde_json::Value>,
        content: Option<String>,
        digest: ResourceDescriptorVariant1Digest,
        #[serde(
            rename = "downloadLocation",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        download_location: Option<String>,
        #[serde(rename = "mediaType", default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        uri: Option<String>,
    },
    Variant2 {
        #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
        annotations: serde_json::Map<String, serde_json::Value>,
        content: String,
        digest: Option<ResourceDescriptorVariant1Digest>,
        #[serde(
            rename = "downloadLocation",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        download_location: Option<String>,
        #[serde(rename = "mediaType", default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        uri: Option<String>,
    },
}

If I run this schema against other validators it does work as intended where as long I have one of content, digest, or uri set it validates.

Here is an example json that does not validate with an error: Error: data did not match any variant of untagged enum ResourceDescriptor at line 20 column 7

{
  "_type": "https://in-toto.io/Statement/v1",
  "subject": [
    {
      "name": "example-software-artifact",
      "digest": { "sha256": "a1b2c3d4e5f6..." }
    }
  ],
  "predicateType": "https://in-toto.io/attestation/scai/attribute-report/v0.2",
  "predicate": {
    "attributes": [
      {
        "attribute": "PASSED_DEVELOPMENT_ENVIRONMENT",
        "evidence": {
          "name": "a1b2c3d4e5f6.development.jsonl",
          "uri": "https://example.com/scai/a1b2c3d4e5f6.development.jsonl",
          "digest": { "sha256": "d1e2f3a4b5c6..." },
          "mediaType": "application/x.dsse+json"
        }
      },
      {
        "attribute": "PASSED_SOURCE",
        "evidence": {
          "name": "a1b2c3d4e5f6.source.jsonl",
          "uri": "https://example.com/scai/a1b2c3d4e5f6.source.jsonl",
          "digest": { "sha256": "e2f3a4b5c6d1..." },
          "mediaType": "application/x.dsse+json"
        }
      },
      {
        "attribute": "PASSED_BUILD",
        "evidence": {
          "name": "a1b2c3d4e5f6.build.jsonl",
          "uri": "https://example.com/scai/a1b2c3d4e5f6.build.jsonl",
          "digest": { "sha256": "f3a4b5c6d1e2..." },
          "mediaType": "application/x.dsse+json"
        }
      },
      {
        "attribute": "PASSED_PACKAGE",
        "evidence": {
          "name": "a1b2c3d4e5f6.package.jsonl",
          "uri": "https://example.com/scai/a1b2c3d4e5f6.package.jsonl",
          "digest": { "sha256": "a4b5c6d1e2f3..." },
          "mediaType": "application/x.dsse+json"
        }
      },
      {
        "attribute": "PASSED_DEPLOY",
        "evidence": {
          "name": "a1b2c3d4e5f6.deploy.jsonl",
          "uri": "https://example.com/scai/a1b2c3d4e5f6.deploy.jsonl",
          "digest": { "sha256": "b5c6d1e2f3a4..." },
          "mediaType": "application/x.dsse+json"
        }
      }
    ],
    "producer": {
      "uri": "https://example.com/gatekeeping-attestor",
      "name": "Gatekeeping Attestor",
      "digest": {
        "sha256": "0123456789abcdef..."
      }
    }
  }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the v0.1.0 behavior with the ResourceDescriptor schema and example JSON, focusing on how anyOf and required are converted into the generated Rust enum. Compare the generated variants with validation behavior from other validators. Done means objects containing any one or more of content, digest, or uri are accepted while objects containing none are rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.