Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Decoder for the shared cross-SDK JSON Schema conversion corpus. |
| 2 | |
| 3 | The corpus is one logical fixture with a byte-identical copy per language. This |
| 4 | module resolves the Python copy module-relatively and decodes it into typed |
| 5 | models, so a malformed corpus fails at the test boundary rather than surfacing |
| 6 | as a confusing assertion error deep inside a converter test. |
| 7 | """ |
| 8 | |
| 9 | import json |
| 10 | import typing as t |
| 11 | from pathlib import Path |
| 12 | |
| 13 | from pydantic import BaseModel, ConfigDict, Field |
| 14 | |
| 15 | CORPUS_PATH = Path(__file__).parent / "json-schema-conversion" / "object-cases.json" |
| 16 | STRICT_CORPUS_PATH = ( |
| 17 | Path(__file__).parent / "json-schema-conversion" / "strict-cases.json" |
| 18 | ) |
| 19 | |
| 20 | Language = t.Literal["zod", "effect", "python"] |
| 21 | LANGUAGES: t.Tuple[Language, ...] = ("zod", "effect", "python") |
| 22 | |
| 23 | _UNSET = object() |
| 24 | |
| 25 | |
| 26 | class LanguageExpectation(BaseModel): |
| 27 | """Per-language expectation. `output` is absent when the case does not pin one.""" |
| 28 | |
| 29 | model_config = ConfigDict(extra="forbid") |
| 30 | |
| 31 | output: t.Any = _UNSET |
| 32 | accepted: t.Optional[bool] = None |
| 33 | |
| 34 | @property |
| 35 | def has_output(self) -> bool: |
| 36 | return self.output is not _UNSET |
| 37 | |
| 38 | |
| 39 | class Divergence(BaseModel): |
| 40 | model_config = ConfigDict(extra="forbid") |
| 41 | |
| 42 | reason: str = Field(min_length=1) |
| 43 | |
| 44 | |
| 45 | class CorpusInstance(BaseModel): |
| 46 | model_config = ConfigDict(extra="forbid") |
| 47 | |
| 48 | input: t.Dict[str, t.Any] |
| 49 | accepted: bool |
| 50 | zod: t.Optional[LanguageExpectation] = None |
| 51 | effect: t.Optional[LanguageExpectation] = None |
| 52 | python: t.Optional[LanguageExpectation] = None |
| 53 | divergence: t.Optional[Divergence] = None |
| 54 | |
| 55 | def accepted_for(self, language: Language) -> bool: |
| 56 | """Acceptance for one language: the shared flag unless a declared divergence overrides it.""" |
| 57 | expectation: t.Optional[LanguageExpectation] = getattr(self, language) |
| 58 | if expectation is not None and expectation.accepted is not None: |
| 59 | return expectation.accepted |
| 60 | return self.accepted |
| 61 | |
| 62 | |
| 63 | class Ingress(BaseModel): |
| 64 | model_config = ConfigDict(extra="forbid") |
| 65 | |
| 66 | accepted: bool |
| 67 | preserved: t.Dict[str, t.Any] |
| 68 | |
| 69 | |
| 70 | class CorpusCase(BaseModel): |
| 71 | model_config = ConfigDict(extra="forbid") |
| 72 | |
| 73 | id: str = Field(min_length=1) |
| 74 | description: t.Optional[str] = None |
| 75 | divergesFromJsonSchema: t.Optional[str] = None # noqa: N815 |
| 76 | schema_: t.Dict[str, t.Any] = Field(alias="schema") |
| 77 | ingress: t.Optional[Ingress] = None |
| 78 | instances: t.List[CorpusInstance] = Field(min_length=1) |
| 79 | |
| 80 | |
| 81 | class Corpus(BaseModel): |
| 82 | model_config = ConfigDict(extra="forbid") |
| 83 | |
| 84 | cases: t.List[CorpusCase] = Field(min_length=1) |
| 85 | |
| 86 | |
| 87 | def assert_corpus_invariants(cases: t.Sequence[CorpusCase]) -> None: |
| 88 | """Ids are unique, and a language may only disagree about acceptance when it says why.""" |
| 89 | seen: t.Set[str] = set() |
| 90 | for case in cases: |
| 91 | if case.id in seen: |
| 92 | raise ValueError(f"Duplicate corpus case id: {case.id}") |
| 93 | seen.add(case.id) |
| 94 | |
| 95 | for index, instance in enumerate(case.instances): |
| 96 | for language in LANGUAGES: |
| 97 | expectation: t.Optional[LanguageExpectation] = getattr( |
| 98 | instance, language |
| 99 | ) |
| 100 | override = expectation.accepted if expectation else None |
| 101 | if ( |
| 102 | override is not None |
| 103 | and override != instance.accepted |
| 104 | and instance.divergence is None |
| 105 | ): |
| 106 | raise ValueError( |
| 107 | f"Corpus case {case.id} instance {index} overrides {language} " |
| 108 | "acceptance without a divergence reason" |
| 109 | ) |
| 110 | |
| 111 | |
| 112 | _cached: t.Optional[t.List[CorpusCase]] = None |
| 113 | |
| 114 | |
| 115 | def load_object_cases() -> t.List[CorpusCase]: |
| 116 | """Decode the shared corpus and enforce its cross-language invariants.""" |
| 117 | global _cached |
| 118 | if _cached is None: |
| 119 | cases = Corpus.model_validate( |
| 120 | json.loads(CORPUS_PATH.read_text(encoding="utf-8")) |
| 121 | ).cases |
| 122 | assert_corpus_invariants(cases) |
| 123 | _cached = cases |
| 124 | return _cached |
| 125 | |
| 126 | |
| 127 | def find_case(case_id: str) -> CorpusCase: |
| 128 | """Look up one case by id, failing loudly when the corpus no longer carries it.""" |
| 129 | for case in load_object_cases(): |
| 130 | if case.id == case_id: |
| 131 | return case |
| 132 | raise LookupError(f"Corpus is missing the {case_id} case") |
| 133 | |
| 134 | |
| 135 | class StrictIncompatibility(BaseModel): |
| 136 | model_config = ConfigDict(extra="forbid") |
| 137 | |
| 138 | path: str |
| 139 | keyword: str = Field(min_length=1) |
| 140 | |
| 141 | |
| 142 | class StrictChange(BaseModel): |
| 143 | model_config = ConfigDict(extra="forbid") |
| 144 | |
| 145 | path: str |
| 146 | reason: str = Field(min_length=1) |
| 147 | |
| 148 | |
| 149 | class StrictArguments(BaseModel): |
| 150 | model_config = ConfigDict(extra="forbid") |
| 151 | |
| 152 | input: t.Dict[str, t.Any] |
| 153 | output: t.Dict[str, t.Any] |
| 154 | |
| 155 | |
| 156 | class StrictExpectation(BaseModel): |
| 157 | """Either the exact strict schema, or the constructs reported as unsupported.""" |
| 158 | |
| 159 | model_config = ConfigDict(extra="forbid") |
| 160 | |
| 161 | schema_: t.Optional[t.Dict[str, t.Any]] = Field(default=None, alias="schema") |
| 162 | unsupported: t.Optional[t.List[StrictIncompatibility]] = Field( |
| 163 | default=None, min_length=1 |
| 164 | ) |
| 165 | |
| 166 | |
| 167 | class StrictCase(BaseModel): |
| 168 | model_config = ConfigDict(extra="forbid") |
| 169 | |
| 170 | id: str = Field(min_length=1) |
| 171 | description: t.Optional[str] = None |
| 172 | schema_: t.Dict[str, t.Any] = Field(alias="schema") |
| 173 | strict: StrictExpectation |
| 174 | changes: t.Optional[t.List[StrictChange]] = None |
| 175 | arguments: t.Optional[t.List[StrictArguments]] = None |
| 176 | |
| 177 | |
| 178 | class StrictCorpus(BaseModel): |
| 179 | model_config = ConfigDict(extra="forbid") |
| 180 | |
| 181 | cases: t.List[StrictCase] = Field(min_length=1) |
| 182 | |
| 183 | |
| 184 | _cached_strict: t.Optional[t.List[StrictCase]] = None |
| 185 | |
| 186 | |
| 187 | def load_strict_cases() -> t.List[StrictCase]: |
| 188 | """Decode the shared strict-mode corpus (byte-identical copy per language).""" |
| 189 | global _cached_strict |
| 190 | if _cached_strict is None: |
| 191 | cases = StrictCorpus.model_validate( |
| 192 | json.loads(STRICT_CORPUS_PATH.read_text(encoding="utf-8")) |
| 193 | ).cases |
| 194 | seen: t.Set[str] = set() |
| 195 | for case in cases: |
| 196 | if case.id in seen: |
| 197 | raise ValueError(f"Duplicate strict case id: {case.id}") |
| 198 | seen.add(case.id) |
| 199 | if (case.strict.schema_ is None) == (case.strict.unsupported is None): |
| 200 | raise ValueError(f"Strict case {case.id} must pin exactly one outcome") |
| 201 | _cached_strict = cases |
| 202 | return _cached_strict |