Skip to content

Commit 811838f

Browse files
committed
fix: resolve parameter type names case-insensitively in reference validation
The EXPR extension makes job and task parameter type names case-insensitive (RFC 0007 §2), so `type: string` is exactly as valid as `type: STRING`. The model accepted such a declaration but then rejected every reference to the parameter: steps[0] -> script -> actions -> onRun -> args[0]: Variable Param.Msg does not exist at this location. The variable-reference prevalidation pass runs on raw template values, before the field validator that folds the `type` discriminator to upper case, and it resolved the type-discriminated parameter-definition union with an exact string comparison. A lowercase or mixed-case type name matched no member of the union, so the definition contributed none of its `Param.*` / `RawParam.*` / `Task.Param.*` symbols and none of its EXPR type information. Compare the discriminator with an ASCII-only case fold when EXPR is active, in both the reference-validation and the definition-collection traversal. Job, task, and environment-template parameters now define their symbols and types whatever the case of the type name. The fold is ASCII-only so Unicode look-alikes such as `ıNT` stay rejected, matching the field validator. Signed-off-by: Mark <399551+mwiebe@users.noreply.github.com>
1 parent 4d7f9b4 commit 811838f

4 files changed

Lines changed: 111 additions & 9 deletions

File tree

‎src/openjd/model/_internal/_variable_reference_validation.py‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ def _validate_model_template_variable_references(
260260
# The errors that we're collecting for this node in the traversal, and will return from the function call.
261261
errors: list[InitErrorDetails] = []
262262

263+
expr_enabled = bool(
264+
context is not None and getattr(context, "_prevalidation_expr_enabled", False)
265+
)
266+
263267
model_origin = typing.get_origin(model)
264268

265269
# Unwrap the Optional types
@@ -345,7 +349,9 @@ def _validate_model_template_variable_references(
345349

346350
# Unwrap a discriminated union to the selected type
347351
if model_origin is Union and discriminator is not None:
348-
unioned_model = _get_model_for_singleton_value(model, value, discriminator)
352+
unioned_model = _get_model_for_singleton_value(
353+
model, value, discriminator, expr_enabled=expr_enabled
354+
)
349355
if unioned_model is not None:
350356
return _validate_model_template_variable_references(
351357
unioned_model,
@@ -398,10 +404,6 @@ def _validate_model_template_variable_references(
398404
# If the node doesn't modify the variable prefix, then symbol_prefix will be the empty string
399405
symbol_prefix += variable_defs.symbol_prefix
400406

401-
expr_enabled = bool(
402-
context is not None and getattr(context, "_prevalidation_expr_enabled", False)
403-
)
404-
405407
# Recursively collect all of the variable definitions at this node and its
406408
# child nodes. Symbol EXPR types are collected only when the EXPR
407409
# extension is active, keeping the Rust expr surface off the non-EXPR
@@ -709,8 +711,17 @@ def _validate_let_bindings(
709711
return errors
710712

711713

714+
# Parameter type names are ASCII (RFC 0007 §2). str.upper() is Unicode-aware
715+
# and folds U+0131 to 'I', which would make 'ıNT' a spelling of 'INT'.
716+
_ASCII_UPPERCASE = str.maketrans("abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
717+
718+
712719
def _get_model_for_singleton_value(
713-
model: Any, value: Any, discriminator: Union[str, Discriminator, None] = None
720+
model: Any,
721+
value: Any,
722+
discriminator: Union[str, Discriminator, None] = None,
723+
*,
724+
expr_enabled: bool = False,
714725
) -> Optional[Type]:
715726
"""Given a FieldInfo and the value that we're given for that field, determine
716727
the actual Model for the value in the event that the FieldInfo may be for
@@ -756,7 +767,20 @@ def _get_model_for_singleton_value(
756767
raise NotImplementedError(
757768
"You have hit an unimplemented code path. Please report this as a bug."
758769
)
759-
if typing.get_args(sub_model_discr_value)[0] == discr_value:
770+
literal_value = typing.get_args(sub_model_discr_value)[0]
771+
if literal_value == discr_value:
772+
return sub_model
773+
# RFC 0007 §2: parameter type names are case-insensitive when the EXPR
774+
# extension is active. The `_normalize_parameter_type_case` field
775+
# validator uppercases the `type` discriminator before pydantic's
776+
# union resolution, but this prevalidation traversal runs earlier and
777+
# sees the raw values — so match the discriminator case-insensitively
778+
# here, or a lowercase-typed parameter defines no Param.* symbols.
779+
if (
780+
expr_enabled
781+
and isinstance(literal_value, str)
782+
and literal_value.translate(_ASCII_UPPERCASE) == discr_value.translate(_ASCII_UPPERCASE)
783+
):
760784
return sub_model
761785

762786
return None
@@ -881,9 +905,13 @@ def _collect_variable_definitions( # noqa: C901 (suppress: too complex)
881905
)
882906
return {"__export__": symtab}
883907

884-
# Unwrap a discriminated union to the selected type
908+
# Unwrap a discriminated union to the selected type. `collect_types` is
909+
# set exactly when the EXPR extension is active, which also governs the
910+
# case-insensitive `type` discriminator matching (RFC 0007 §2).
885911
if model_origin is Union and discriminator is not None:
886-
unioned_model = _get_model_for_singleton_value(model, value, discriminator)
912+
unioned_model = _get_model_for_singleton_value(
913+
model, value, discriminator, expr_enabled=collect_types
914+
)
887915
if unioned_model is not None:
888916
return _collect_variable_definitions(
889917
unioned_model,

‎test/openjd/model_v0/v2023_09/test_environment_template.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,21 @@ def test_with_expr_miscased_accepted(self, canonical: str, miscased: str) -> Non
319319
# which is the state an audit found untested.
320320
self._decode(self._tmpl(miscased))
321321

322+
@pytest.mark.parametrize("canonical, miscased", TYPES)
323+
def test_with_expr_miscased_param_referenceable(self, canonical: str, miscased: str) -> None:
324+
# A non-canonically-cased type name is just as valid under EXPR, so it
325+
# must still define Param.P for the variable-reference
326+
# prevalidation, which resolves the raw `type` discriminator before the
327+
# case fold runs. Regression: the definition was accepted but every
328+
# {{ Param.P }} reference was rejected with "does not exist at this
329+
# location".
330+
template = self._tmpl(miscased)
331+
template["environment"] = {
332+
"name": "E",
333+
"script": {"actions": {"onEnter": {"command": "echo", "args": ["{{ Param.P }}"]}}},
334+
}
335+
self._decode(template)
336+
322337
@pytest.mark.parametrize("type_name", ("\u0131NT", "\u017fTRING", "\ufb02OAT"))
323338
def test_non_ascii_lookalike_rejected_with_expr(self, type_name: str) -> None:
324339
# str.upper() folds U+0131 to 'I', U+017F to 'S' and U+FB02 (fl) to 'FL',

‎test/openjd/model_v0/v2023_09/test_list_parameters.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,49 @@ def test_with_expr_miscased_accepted(self, canonical, miscased, default):
169169
# Case 4 of 4.
170170
_decode(_tmpl(self._param(miscased, default)))
171171

172+
# ── Mis-cased parameters are referenceable ──
173+
174+
@staticmethod
175+
def _referencing_step():
176+
return {
177+
"name": "S",
178+
"script": {
179+
"actions": {
180+
"onRun": {"command": "echo", "args": ["{{ Param.P }}", "{{ RawParam.P }}"]}
181+
}
182+
},
183+
}
184+
185+
@pytest.mark.parametrize("canonical, miscased, default", TYPES)
186+
def test_with_expr_miscased_param_referenceable(self, canonical, miscased, default):
187+
# A non-canonically-cased type name is just as valid under EXPR, so it
188+
# must still define Param.P/RawParam.P for the
189+
# variable-reference prevalidation, which resolves the raw `type`
190+
# discriminator before the case fold runs. Regression: the parameter
191+
# declaration was accepted but every {{ Param.P }} reference was
192+
# rejected with "does not exist at this location".
193+
template = _tmpl(self._param(miscased, default))
194+
template["steps"] = [self._referencing_step()]
195+
_decode(template)
196+
197+
def test_with_expr_miscased_param_type_checked(self):
198+
# The reference prevalidation also collects the parameter's EXPR type
199+
# through the lowercase spelling: string + int is a type error.
200+
template = _tmpl(self._param("string", "hi"))
201+
template["steps"] = [
202+
{
203+
"name": "S",
204+
"script": {
205+
"actions": {"onRun": {"command": "echo", "args": ["{{ Param.P + 1 }}"]}}
206+
},
207+
}
208+
]
209+
with pytest.raises(DecodeValidationError) as excinfo:
210+
_decode(template)
211+
assert "steps[0] -> script -> actions -> onRun -> args[0]" in str(excinfo.value), str(
212+
excinfo.value
213+
)
214+
172215
# ── The fold is ASCII ──
173216

174217
# str.upper() folds each of these wholly into the type-name alphabet: U+0131

‎test/openjd/model_v0/v2023_09/test_parameter_space.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,22 @@ def test_with_expr_miscased_accepted(
900900
self._tmpl(miscased, range_value, chunks, self._extensions_for(canonical, expr=True))
901901
)
902902

903+
@pytest.mark.parametrize("canonical, miscased, range_value, chunks", TYPES)
904+
def test_with_expr_miscased_param_referenceable(
905+
self, canonical: str, miscased: str, range_value: Any, chunks: Any
906+
) -> None:
907+
# A non-canonically-cased type name is just as valid under EXPR, so it
908+
# must still define Task.Param.F for the
909+
# variable-reference prevalidation, which resolves the raw `type`
910+
# discriminator before the case fold runs. Regression: the definition
911+
# was accepted but every {{ Task.Param.F }} reference was rejected
912+
# with "does not exist at this location".
913+
template = self._tmpl(
914+
miscased, range_value, chunks, self._extensions_for(canonical, expr=True)
915+
)
916+
template["steps"][0]["script"]["actions"]["onRun"]["args"] = ["{{ Task.Param.F }}"]
917+
self._decode(template)
918+
903919
# ── The gate is on EXPR, not on the extension that supplies the type ──
904920

905921
def test_chunk_int_miscased_needs_expr_not_only_task_chunking(self) -> None:

0 commit comments

Comments
 (0)