Skip to content

Conversation

@nickita-khylkouski
Copy link
Contributor

Summary

  • Adds type validation layer to PromptMetadata.fromConfig() before performing unchecked casts
  • Provides detailed error messages showing field name, expected type, actual type, and value
  • Adds 42 test cases covering all validation scenarios

Problem

The fromConfig() method had 8+ unsafe casts that could cause ClassCastException at runtime when YAML frontmatter contained incorrectly typed values. For example, if tools: "not-a-list" was in YAML, the code would crash with a generic ClassCastException.

Solution

Added validation methods that:

  1. Validate all field types using instanceof checks before casting
  2. Throw IllegalArgumentException with detailed error messages like:
    Field 'tools' has incorrect type. Expected: List, Actual: String, Value: not-a-list
    
  3. Handle nested structures (input, output) with appropriate validation

Test Coverage

Created PromptMetadataTest.java with 42 tests:

  • 12 happy path tests (valid configurations)
  • 15+ type validation error tests (all fields)
  • 10+ nested validation tests (input/output configs)
  • 8+ edge case tests (null, empty, complex structures)

Files Changed

  • PromptMetadata.java: Added validateConfigTypes() and validateFieldIfPresent() methods
  • PromptMetadataTest.java: New comprehensive test file
  • BUILD.bazel: Added test target

🤖 Generated with Claude Code

@google-cla
Copy link

google-cla bot commented Jan 23, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Adds type validation before casting to prevent ClassCastException at runtime.
Provides detailed error messages showing field name, expected type, actual type,
and the problematic value.

Changes:
- Add validateConfigTypes() to validate all field types upfront
- Add validateFieldIfPresent() helper for single field validation
- Update fromConfig() to call validation before casting
- Add PromptMetadataTest with 42 test cases covering:
  - Happy path validation
  - Type mismatch errors for all fields
  - Nested input/output config validation
  - Edge cases (null, empty, complex nested structures)
@nickita-khylkouski nickita-khylkouski force-pushed the fix/promptmetadata-validation-layer-clean branch from 5f13872 to c9b715f Compare January 23, 2026 22:44
MapSubject doesn't have isSameAs() - use (Object) cast with
isSameInstanceAs() to match codebase idiom (DotpromptTest.java:95).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant