|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | +"""Tests for etag-typed header handling in the preprocess plugin.""" |
| 7 | +from pygen.preprocess import PreProcessPlugin |
| 8 | + |
| 9 | + |
| 10 | +def _plugin() -> PreProcessPlugin: |
| 11 | + return PreProcessPlugin( |
| 12 | + output_folder="", |
| 13 | + options={ |
| 14 | + "version-tolerant": True, |
| 15 | + "models-mode": "dpg", |
| 16 | + "show-operations": True, |
| 17 | + "show-send-request": True, |
| 18 | + "builders-visibility": "public", |
| 19 | + }, |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def _header_param(client_name: str, wire_name: str, etag_role: str | None) -> dict: |
| 24 | + p: dict = { |
| 25 | + "clientName": client_name, |
| 26 | + "wireName": wire_name, |
| 27 | + "location": "header", |
| 28 | + "optional": True, |
| 29 | + "implementation": "Method", |
| 30 | + "type": {"type": "string"}, |
| 31 | + } |
| 32 | + if etag_role is not None: |
| 33 | + p["etagRole"] = etag_role |
| 34 | + return p |
| 35 | + |
| 36 | + |
| 37 | +def _client_yaml(operation_params: list[dict]) -> dict: |
| 38 | + return { |
| 39 | + "name": "TestClient", |
| 40 | + "namespace": "test", |
| 41 | + "moduleName": "test", |
| 42 | + "url": "", |
| 43 | + "description": "test", |
| 44 | + "parameters": [], |
| 45 | + "operationGroups": [ |
| 46 | + { |
| 47 | + "operations": [ |
| 48 | + { |
| 49 | + "name": "copyFromUrl", |
| 50 | + "parameters": operation_params, |
| 51 | + } |
| 52 | + ] |
| 53 | + } |
| 54 | + ], |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +def _get_op(client: dict) -> dict: |
| 59 | + return client["operationGroups"][0]["operations"][0] |
| 60 | + |
| 61 | + |
| 62 | +def test_etag_role_preserved_when_only_standard_pair_present(): |
| 63 | + """Standard If-Match/If-None-Match keep their etagRole.""" |
| 64 | + if_match = _header_param("if_match", "If-Match", "ifMatch") |
| 65 | + if_none_match = _header_param("if_none_match", "If-None-Match", "ifNoneMatch") |
| 66 | + client = _client_yaml([if_match, if_none_match]) |
| 67 | + |
| 68 | + _plugin().update_client(client) |
| 69 | + |
| 70 | + op = _get_op(client) |
| 71 | + standard_match = next(p for p in op["parameters"] if p["wireName"] == "If-Match") |
| 72 | + standard_none = next(p for p in op["parameters"] if p["wireName"] == "If-None-Match") |
| 73 | + assert standard_match.get("etagRole") == "ifMatch" |
| 74 | + assert standard_none.get("etagRole") == "ifNoneMatch" |
| 75 | + assert op["hasEtag"] is True |
| 76 | + |
| 77 | + |
| 78 | +def test_etag_role_preserved_when_only_custom_pair_present(): |
| 79 | + """Custom etag headers alone are promoted to the etag/match_condition slot.""" |
| 80 | + source_match = _header_param("source_if_match", "x-ms-source-if-match", "ifMatch") |
| 81 | + source_none = _header_param( |
| 82 | + "source_if_none_match", "x-ms-source-if-none-match", "ifNoneMatch" |
| 83 | + ) |
| 84 | + client = _client_yaml([source_match, source_none]) |
| 85 | + |
| 86 | + _plugin().update_client(client) |
| 87 | + |
| 88 | + assert source_match.get("etagRole") == "ifMatch" |
| 89 | + assert source_none.get("etagRole") == "ifNoneMatch" |
| 90 | + |
| 91 | + |
| 92 | +def test_standard_etag_wins_over_custom_when_both_present(): |
| 93 | + """When both standard and custom etag headers are present in the same operation, |
| 94 | + the standard If-Match/If-None-Match pair takes the etag/match_condition slot and |
| 95 | + the custom headers have their etagRole stripped so they retain their natural |
| 96 | + clientName (e.g. source_if_match) instead of colliding with the standard pair. |
| 97 | +
|
| 98 | + Regression test for PR #10494 which caused operations like Storage's copyFromUrl |
| 99 | + to emit two parameters named "etag" and two named "match_condition". |
| 100 | + """ |
| 101 | + source_match = _header_param( |
| 102 | + "source_if_match", "x-ms-source-if-match", "ifMatch" |
| 103 | + ) |
| 104 | + source_none = _header_param( |
| 105 | + "source_if_none_match", "x-ms-source-if-none-match", "ifNoneMatch" |
| 106 | + ) |
| 107 | + if_match = _header_param("if_match", "If-Match", "ifMatch") |
| 108 | + if_none_match = _header_param("if_none_match", "If-None-Match", "ifNoneMatch") |
| 109 | + |
| 110 | + # Mirror the parameter ordering in routes.tsp: source headers come first. |
| 111 | + client = _client_yaml([source_match, source_none, if_match, if_none_match]) |
| 112 | + _plugin().update_client(client) |
| 113 | + |
| 114 | + # Standard pair keeps etagRole and so will be transformed into etag/match_condition. |
| 115 | + assert if_match.get("etagRole") == "ifMatch" |
| 116 | + assert if_none_match.get("etagRole") == "ifNoneMatch" |
| 117 | + # Custom pair has etagRole stripped, so update_parameter will NOT rename them. |
| 118 | + assert "etagRole" not in source_match |
| 119 | + assert "etagRole" not in source_none |
| 120 | + |
| 121 | + # The selected pair should be at the end of the operation parameters. |
| 122 | + op = _get_op(client) |
| 123 | + assert op["parameters"][-2] is if_match |
| 124 | + assert op["parameters"][-1] is if_none_match |
| 125 | + assert op["hasEtag"] is True |
| 126 | + |
| 127 | + |
| 128 | +def test_first_custom_pair_chosen_when_multiple_custom_pairs_present(): |
| 129 | + """With multiple custom etag pairs and no standard pair, the first candidate wins.""" |
| 130 | + blob_match = _header_param("blob_if_match", "x-ms-blob-if-match", "ifMatch") |
| 131 | + blob_none = _header_param( |
| 132 | + "blob_if_none_match", "x-ms-blob-if-none-match", "ifNoneMatch" |
| 133 | + ) |
| 134 | + source_match = _header_param( |
| 135 | + "source_if_match", "x-ms-source-if-match", "ifMatch" |
| 136 | + ) |
| 137 | + source_none = _header_param( |
| 138 | + "source_if_none_match", "x-ms-source-if-none-match", "ifNoneMatch" |
| 139 | + ) |
| 140 | + client = _client_yaml([blob_match, blob_none, source_match, source_none]) |
| 141 | + |
| 142 | + _plugin().update_client(client) |
| 143 | + |
| 144 | + # First-encountered pair wins; the rest have etagRole stripped. |
| 145 | + assert blob_match.get("etagRole") == "ifMatch" |
| 146 | + assert blob_none.get("etagRole") == "ifNoneMatch" |
| 147 | + assert "etagRole" not in source_match |
| 148 | + assert "etagRole" not in source_none |
| 149 | + |
| 150 | + |
| 151 | +def test_synthetic_partner_still_works_with_only_one_custom_etag(): |
| 152 | + """When only a single custom etag header is present (no partner), the existing |
| 153 | + synthetic-partner code path still creates a matching ifNoneMatch (or ifMatch) |
| 154 | + copy. The fix must not regress this behaviour. |
| 155 | + """ |
| 156 | + source_match = _header_param( |
| 157 | + "source_if_match", "x-ms-source-if-match", "ifMatch" |
| 158 | + ) |
| 159 | + client = _client_yaml([source_match]) |
| 160 | + |
| 161 | + _plugin().update_client(client) |
| 162 | + |
| 163 | + op = _get_op(client) |
| 164 | + assert op.get("hasEtag") is True |
| 165 | + # The original custom param plus a synthetic partner are pushed to the end. |
| 166 | + last_two = op["parameters"][-2:] |
| 167 | + assert last_two[0]["etagRole"] == "ifMatch" |
| 168 | + assert last_two[1]["etagRole"] == "ifNoneMatch" |
| 169 | + |
| 170 | + |
| 171 | +def test_full_update_yaml_does_not_collide_client_names(): |
| 172 | + """End-to-end: after update_client + update_parameter, the four etag headers |
| 173 | + have distinct clientNames. |
| 174 | +
|
| 175 | + Without the fix, both source_if_match and if_match end up with clientName="etag", |
| 176 | + and both source_if_none_match and if_none_match end up with clientName="match_condition". |
| 177 | + """ |
| 178 | + source_match = _header_param( |
| 179 | + "source_if_match", "x-ms-source-if-match", "ifMatch" |
| 180 | + ) |
| 181 | + source_none = _header_param( |
| 182 | + "source_if_none_match", "x-ms-source-if-none-match", "ifNoneMatch" |
| 183 | + ) |
| 184 | + if_match = _header_param("if_match", "If-Match", "ifMatch") |
| 185 | + if_none_match = _header_param("if_none_match", "If-None-Match", "ifNoneMatch") |
| 186 | + client = _client_yaml([source_match, source_none, if_match, if_none_match]) |
| 187 | + |
| 188 | + plugin = _plugin() |
| 189 | + plugin.update_client(client) |
| 190 | + # update_client does the slot/strip; update_parameter does the rename. |
| 191 | + op = _get_op(client) |
| 192 | + for p in op["parameters"]: |
| 193 | + plugin.update_parameter(p) |
| 194 | + |
| 195 | + client_names = [p["clientName"] for p in op["parameters"]] |
| 196 | + assert len(client_names) == len(set(client_names)), ( |
| 197 | + f"Duplicate clientNames after preprocess: {client_names}" |
| 198 | + ) |
| 199 | + # The standard pair was promoted; the custom pair retains its natural names. |
| 200 | + assert "etag" in client_names |
| 201 | + assert "match_condition" in client_names |
| 202 | + assert "source_if_match" in client_names |
| 203 | + assert "source_if_none_match" in client_names |
0 commit comments