diff --git a/preprocess_schemas.py b/preprocess_schemas.py index 6346ccf..14d0464 100644 --- a/preprocess_schemas.py +++ b/preprocess_schemas.py @@ -449,16 +449,21 @@ def rewrite_refs_to_variants(root, op, file_path, variant_needs): for node in iter_nodes(root): if isinstance(node, dict) and "$ref" in node: ref = node["$ref"] - if "#" not in ref: # External file reference - abs_target = (file_path.parent / ref).resolve() - if ( - str(abs_target) in variant_needs - and op in variant_needs[str(abs_target)] - ): - ref_path = Path(ref) - node["$ref"] = str( - ref_path.parent / f"{ref_path.stem}_{op}_request.json" - ) + ref_file, separator, fragment = ref.partition("#") + if not ref_file: + continue + abs_target = (file_path.parent / ref_file).resolve() + if ( + str(abs_target) in variant_needs + and op in variant_needs[str(abs_target)] + ): + ref_path = Path(ref_file) + variant_ref = str( + ref_path.parent / f"{ref_path.stem}_{op}_request.json" + ) + node["$ref"] = variant_ref + ( + separator + fragment if separator else "" + ) def _apply_request_rules_to_object( @@ -592,8 +597,9 @@ def extract_external_refs(schema, path): for node in iter_nodes(data): if isinstance(node, dict) and "$ref" in node: ref = node["$ref"] - if "#" not in ref: - abs_path = str((path.parent / ref).resolve()) + ref_file, _, _ = ref.partition("#") + if ref_file: + abs_path = str((path.parent / ref_file).resolve()) refs.append((name, abs_path)) return refs diff --git a/src/ucp_sdk/models/schemas/shopping/payment_complete_request.py b/src/ucp_sdk/models/schemas/shopping/payment_complete_request.py index 0c9415b..392cd20 100644 --- a/src/ucp_sdk/models/schemas/shopping/payment_complete_request.py +++ b/src/ucp_sdk/models/schemas/shopping/payment_complete_request.py @@ -20,7 +20,7 @@ from pydantic import BaseModel, ConfigDict -from .types import payment_instrument +from .types import payment_instrument_complete_request class PaymentCompleteRequest(BaseModel): @@ -31,9 +31,10 @@ class PaymentCompleteRequest(BaseModel): model_config = ConfigDict( extra="allow", ) - instruments: list[payment_instrument.SelectedPaymentInstrument] | None = ( - None - ) + instruments: ( + list[payment_instrument_complete_request.SelectedPaymentInstrument] + | None + ) = None """ The payment instruments available for this payment. Each instrument is associated with a specific handler via the handler_id field. Handlers can extend the base payment_instrument schema to add handler-specific fields. """ diff --git a/src/ucp_sdk/models/schemas/shopping/payment_create_request.py b/src/ucp_sdk/models/schemas/shopping/payment_create_request.py index 4bd95d6..5219f5f 100644 --- a/src/ucp_sdk/models/schemas/shopping/payment_create_request.py +++ b/src/ucp_sdk/models/schemas/shopping/payment_create_request.py @@ -20,7 +20,7 @@ from pydantic import BaseModel, ConfigDict -from .types import payment_instrument +from .types import payment_instrument_create_request class PaymentCreateRequest(BaseModel): @@ -31,9 +31,9 @@ class PaymentCreateRequest(BaseModel): model_config = ConfigDict( extra="allow", ) - instruments: list[payment_instrument.SelectedPaymentInstrument] | None = ( - None - ) + instruments: ( + list[payment_instrument_create_request.SelectedPaymentInstrument] | None + ) = None """ The payment instruments available for this payment. Each instrument is associated with a specific handler via the handler_id field. Handlers can extend the base payment_instrument schema to add handler-specific fields. """ diff --git a/src/ucp_sdk/models/schemas/shopping/payment_update_request.py b/src/ucp_sdk/models/schemas/shopping/payment_update_request.py index a63bbfa..59c926a 100644 --- a/src/ucp_sdk/models/schemas/shopping/payment_update_request.py +++ b/src/ucp_sdk/models/schemas/shopping/payment_update_request.py @@ -20,7 +20,7 @@ from pydantic import BaseModel, ConfigDict -from .types import payment_instrument +from .types import payment_instrument_update_request class PaymentUpdateRequest(BaseModel): @@ -31,9 +31,9 @@ class PaymentUpdateRequest(BaseModel): model_config = ConfigDict( extra="allow", ) - instruments: list[payment_instrument.SelectedPaymentInstrument] | None = ( - None - ) + instruments: ( + list[payment_instrument_update_request.SelectedPaymentInstrument] | None + ) = None """ The payment instruments available for this payment. Each instrument is associated with a specific handler via the handler_id field. Handlers can extend the base payment_instrument schema to add handler-specific fields. """ diff --git a/src/ucp_sdk/models/schemas/shopping/types/payment_credential_complete_request.py b/src/ucp_sdk/models/schemas/shopping/types/payment_credential_complete_request.py new file mode 100644 index 0000000..0fe4d9c --- /dev/null +++ b/src/ucp_sdk/models/schemas/shopping/types/payment_credential_complete_request.py @@ -0,0 +1,35 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# generated by datamodel-codegen +# pylint: disable=all +# pyformat: disable + +from __future__ import annotations + +from pydantic import BaseModel, ConfigDict + + +class PaymentCredentialCompleteRequest(BaseModel): + """ + The base definition for any payment credential. Handlers define specific credential types. + """ + + model_config = ConfigDict( + extra="allow", + ) + type: str + """ + The credential type discriminator. Specific schemas will constrain this to a constant value. + """ diff --git a/src/ucp_sdk/models/schemas/shopping/types/payment_credential_create_request.py b/src/ucp_sdk/models/schemas/shopping/types/payment_credential_create_request.py new file mode 100644 index 0000000..0c79bf1 --- /dev/null +++ b/src/ucp_sdk/models/schemas/shopping/types/payment_credential_create_request.py @@ -0,0 +1,35 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# generated by datamodel-codegen +# pylint: disable=all +# pyformat: disable + +from __future__ import annotations + +from pydantic import BaseModel, ConfigDict + + +class PaymentCredentialCreateRequest(BaseModel): + """ + The base definition for any payment credential. Handlers define specific credential types. + """ + + model_config = ConfigDict( + extra="allow", + ) + type: str + """ + The credential type discriminator. Specific schemas will constrain this to a constant value. + """ diff --git a/src/ucp_sdk/models/schemas/shopping/types/payment_credential_update_request.py b/src/ucp_sdk/models/schemas/shopping/types/payment_credential_update_request.py new file mode 100644 index 0000000..9a6eb10 --- /dev/null +++ b/src/ucp_sdk/models/schemas/shopping/types/payment_credential_update_request.py @@ -0,0 +1,35 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# generated by datamodel-codegen +# pylint: disable=all +# pyformat: disable + +from __future__ import annotations + +from pydantic import BaseModel, ConfigDict + + +class PaymentCredentialUpdateRequest(BaseModel): + """ + The base definition for any payment credential. Handlers define specific credential types. + """ + + model_config = ConfigDict( + extra="allow", + ) + type: str + """ + The credential type discriminator. Specific schemas will constrain this to a constant value. + """ diff --git a/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_complete_request.py b/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_complete_request.py new file mode 100644 index 0000000..2aac44e --- /dev/null +++ b/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_complete_request.py @@ -0,0 +1,78 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# generated by datamodel-codegen +# pylint: disable=all +# pyformat: disable + +from __future__ import annotations + +from typing import Any + +from pydantic import BaseModel, ConfigDict + +from . import ( + payment_credential_complete_request, + postal_address_complete_request, +) + + +class PaymentInstrumentCompleteRequest(BaseModel): + """ + The base definition for any payment instrument. It links the instrument to a specific payment handler. + """ + + model_config = ConfigDict( + extra="allow", + ) + id: str + """ + A unique identifier for this instrument instance, assigned by the platform. + """ + handler_id: str + """ + The unique identifier for the handler instance that produced this instrument. This corresponds to the 'id' field in the Payment Handler definition. + """ + type: str + """ + The broad category of the instrument (e.g., 'card', 'tokenized_card'). Specific schemas will constrain this to a constant value. + """ + billing_address: ( + postal_address_complete_request.PostalAddressCompleteRequest | None + ) = None + """ + The billing address associated with this payment method. + """ + credential: ( + payment_credential_complete_request.PaymentCredentialCompleteRequest + | None + ) = None + display: dict[str, Any] | None = None + """ + Display information for this payment instrument. Each payment instrument schema defines its specific display properties, as outlined by the payment handler. + """ + + +class SelectedPaymentInstrument(PaymentInstrumentCompleteRequest): + """ + A payment instrument with selection state. + """ + + model_config = ConfigDict( + extra="allow", + ) + selected: bool | None = None + """ + Whether this instrument is selected by the user. + """ diff --git a/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_create_request.py b/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_create_request.py new file mode 100644 index 0000000..4d7117e --- /dev/null +++ b/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_create_request.py @@ -0,0 +1,74 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# generated by datamodel-codegen +# pylint: disable=all +# pyformat: disable + +from __future__ import annotations + +from typing import Any + +from pydantic import BaseModel, ConfigDict + +from . import payment_credential_create_request, postal_address_create_request + + +class PaymentInstrumentCreateRequest(BaseModel): + """ + The base definition for any payment instrument. It links the instrument to a specific payment handler. + """ + + model_config = ConfigDict( + extra="allow", + ) + id: str + """ + A unique identifier for this instrument instance, assigned by the platform. + """ + handler_id: str + """ + The unique identifier for the handler instance that produced this instrument. This corresponds to the 'id' field in the Payment Handler definition. + """ + type: str + """ + The broad category of the instrument (e.g., 'card', 'tokenized_card'). Specific schemas will constrain this to a constant value. + """ + billing_address: ( + postal_address_create_request.PostalAddressCreateRequest | None + ) = None + """ + The billing address associated with this payment method. + """ + credential: ( + payment_credential_create_request.PaymentCredentialCreateRequest | None + ) = None + display: dict[str, Any] | None = None + """ + Display information for this payment instrument. Each payment instrument schema defines its specific display properties, as outlined by the payment handler. + """ + + +class SelectedPaymentInstrument(PaymentInstrumentCreateRequest): + """ + A payment instrument with selection state. + """ + + model_config = ConfigDict( + extra="allow", + ) + selected: bool | None = None + """ + Whether this instrument is selected by the user. + """ diff --git a/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_update_request.py b/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_update_request.py new file mode 100644 index 0000000..0a72443 --- /dev/null +++ b/src/ucp_sdk/models/schemas/shopping/types/payment_instrument_update_request.py @@ -0,0 +1,74 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# generated by datamodel-codegen +# pylint: disable=all +# pyformat: disable + +from __future__ import annotations + +from typing import Any + +from pydantic import BaseModel, ConfigDict + +from . import payment_credential_update_request, postal_address_update_request + + +class PaymentInstrumentUpdateRequest(BaseModel): + """ + The base definition for any payment instrument. It links the instrument to a specific payment handler. + """ + + model_config = ConfigDict( + extra="allow", + ) + id: str + """ + A unique identifier for this instrument instance, assigned by the platform. + """ + handler_id: str + """ + The unique identifier for the handler instance that produced this instrument. This corresponds to the 'id' field in the Payment Handler definition. + """ + type: str + """ + The broad category of the instrument (e.g., 'card', 'tokenized_card'). Specific schemas will constrain this to a constant value. + """ + billing_address: ( + postal_address_update_request.PostalAddressUpdateRequest | None + ) = None + """ + The billing address associated with this payment method. + """ + credential: ( + payment_credential_update_request.PaymentCredentialUpdateRequest | None + ) = None + display: dict[str, Any] | None = None + """ + Display information for this payment instrument. Each payment instrument schema defines its specific display properties, as outlined by the payment handler. + """ + + +class SelectedPaymentInstrument(PaymentInstrumentUpdateRequest): + """ + A payment instrument with selection state. + """ + + model_config = ConfigDict( + extra="allow", + ) + selected: bool | None = None + """ + Whether this instrument is selected by the user. + """ diff --git a/src/ucp_sdk/models/schemas/shopping/types/postal_address_complete_request.py b/src/ucp_sdk/models/schemas/shopping/types/postal_address_complete_request.py new file mode 100644 index 0000000..c282a9d --- /dev/null +++ b/src/ucp_sdk/models/schemas/shopping/types/postal_address_complete_request.py @@ -0,0 +1,63 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# generated by datamodel-codegen +# pylint: disable=all +# pyformat: disable + +from __future__ import annotations + +from pydantic import BaseModel, ConfigDict + + +class PostalAddressCompleteRequest(BaseModel): + model_config = ConfigDict( + extra="allow", + ) + extended_address: str | None = None + """ + An address extension such as an apartment number, C/O or alternative name. + """ + street_address: str | None = None + """ + The street address. + """ + address_locality: str | None = None + """ + The locality in which the street address is, and which is in the region. For example, Mountain View. + """ + address_region: str | None = None + """ + The region in which the locality is, and which is in the country. Required for applicable countries (i.e. state in US, province in CA). For example, California or another appropriate first-level Administrative division. + """ + address_country: str | None = None + """ + The country. Recommended to be in 2-letter ISO 3166-1 alpha-2 format, for example "US". For backward compatibility, a 3-letter ISO 3166-1 alpha-3 country code such as "SGP" or a full country name such as "Singapore" can also be used. + """ + postal_code: str | None = None + """ + The postal code. For example, 94043. + """ + first_name: str | None = None + """ + Optional. First name of the contact associated with the address. + """ + last_name: str | None = None + """ + Optional. Last name of the contact associated with the address. + """ + phone_number: str | None = None + """ + Optional. Phone number of the contact associated with the address. + """ diff --git a/tests/test_codegen_pipeline.py b/tests/test_codegen_pipeline.py index d601a64..df79a6d 100644 --- a/tests/test_codegen_pipeline.py +++ b/tests/test_codegen_pipeline.py @@ -247,6 +247,33 @@ def test_eval_prop_inclusion_applies_operation_overrides(self) -> None: class VariantGenerationTest(unittest.TestCase): """Tests request variant construction and output.""" + def test_rewrite_external_ref_preserves_fragment(self) -> None: + """External refs target variants without losing their fragments.""" + schema = { + "properties": { + "child": {"$ref": "nested/child.json#/$defs/item"}, + "local": {"$ref": "#/$defs/local"}, + } + } + file_path = Path("/schemas/parent.json") + child_path = str((file_path.parent / "nested" / "child.json").resolve()) + + preprocess_schemas.rewrite_refs_to_variants( + schema, + "create", + file_path, + {child_path: {"create"}}, + ) + + self.assertEqual( + schema["properties"]["child"]["$ref"], + "nested/child_create_request.json#/$defs/item", + ) + self.assertEqual( + schema["properties"]["local"]["$ref"], + "#/$defs/local", + ) + def test_object_variant_filters_fields_and_rewrites_refs(self) -> None: """Object variants filter fields and target child variants.""" schema = { @@ -564,6 +591,95 @@ def test_main_preprocesses_schema_tree_end_to_end(self) -> None: self.assertEqual(set(parent_variant["required"]), {"id", "child"}) self.assertEqual(child_variant["required"], ["value"]) + def test_propagation_with_fragment(self) -> None: + """Propagation should work even if the reference has a fragment.""" + with tempfile.TemporaryDirectory() as temp_dir: + root = Path(temp_dir) + preprocess_schemas.save_json( + { + "$defs": { + "entity": { + "type": "object", + "properties": {"id": {"type": "string"}}, + "required": ["id"], + } + } + }, + root / "ucp.json", + ) + preprocess_schemas.save_json( + { + "$id": "https://ucp.dev/schemas/child.json", + "title": "Child", + "type": "object", + "$defs": { + "item": { + "type": "object", + "properties": { + "grandchild": {"$ref": "grandchild.json"} + }, + } + }, + "properties": {"dummy": {"type": "string"}}, + }, + root / "child.json", + ) + preprocess_schemas.save_json( + { + "$id": "https://ucp.dev/schemas/grandchild.json", + "title": "Grandchild", + "type": "object", + "properties": { + "value": { + "type": "string", + "ucp_request": {"create": "required"}, + } + }, + }, + root / "grandchild.json", + ) + preprocess_schemas.save_json( + { + "$id": "https://ucp.dev/schemas/parent.json", + "title": "Parent", + "allOf": [{"$ref": "ucp.json#/$defs/entity"}], + "properties": { + "child_item": { + "$ref": "child.json#/$defs/item", + "ucp_request": {"create": "required"}, + } + }, + }, + root / "parent.json", + ) + + with ( + mock.patch.object( + sys, + "argv", + ["preprocess_schemas.py", str(root)], + ), + contextlib.redirect_stdout(io.StringIO()), + ): + preprocess_schemas.main() + + self.assertTrue( + (root / "child_create_request.json").exists(), + "child_create_request.json was not generated", + ) + self.assertTrue( + (root / "grandchild_create_request.json").exists(), + "grandchild_create_request.json was not generated", + ) + + parent_variant = preprocess_schemas.load_json( + root / "parent_create_request.json" + ) + self.assertEqual( + parent_variant["properties"]["child_item"]["$ref"], + "child_create_request.json#/$defs/item", + ) + class MetadataUnionTest(unittest.TestCase): """The UcpMetadata root union is derived from ucp.json $defs."""