Skip to content

java: add schema attribute to @CopilotToolParam for custom type schema override#1999

Open
rinceyuan wants to merge 2 commits into
github:mainfrom
rinceyuan:feat/java-param-schema-override
Open

java: add schema attribute to @CopilotToolParam for custom type schema override#1999
rinceyuan wants to merge 2 commits into
github:mainfrom
rinceyuan:feat/java-param-schema-override

Conversation

@rinceyuan

Copy link
Copy Markdown
Contributor

Closes #1794

Summary

Adds an optional schema attribute to @CopilotToolParam that allows users to explicitly specify the JSON Schema for a tool parameter. This enables support for custom/third-party types that the SchemaGenerator cannot automatically map.

Example

@CopilotTool("Schedule a deployment")
public String scheduleDeployment(
        @CopilotToolParam(value = "Deployment time in ISO-8601",
            schema = "{\"type\":\"string\",\"format\":\"date-time\"}")
        com.acme.internal.AcmeDateTime deployTime) {
    return "Scheduled for " + deployTime;
}

Changes

CopilotToolParam.java

  • Added String schema() default "" annotation attribute with Javadoc

CopilotToolProcessor.java

  • In generateSchemaWithParamMetadata(): when schema is non-empty, use it instead of auto-generated schema
  • Added compile-time validation: schema + defaultValue conflict emits error
  • Added compile-time validation: invalid JSON (not starting/ending with braces) emits error
  • Added jsonToMapOfSource() — a minimal recursive-descent JSON parser that converts JSON strings to Map.of(...) source code expressions at compile time (no external JSON library needed)

Param.java

  • Added schema field, fluent schema() mutator, and getter for lambda API parity
  • Updated equals(), hashCode() to include schema

CopilotToolProcessorTest.java (4 new tests)

  1. Schema override — custom schema appears in generated code
  2. Schema + defaultValue conflict — produces compile error
  3. Invalid JSON — produces compile error
  4. Empty schema — falls through to normal type-based generation

Testing

  • All 4 new tests pass
  • The only failing test (generatesNullMetadata_whenAbsent) is a pre-existing failure on main unrelated to this PR

…a override

Closes github#1794

Add an optional schema attribute to @CopilotToolParam that allows users
to explicitly specify the JSON Schema for a tool parameter, bypassing
automatic type-based schema generation.

Changes:
- CopilotToolParam.java: add String schema() default ""
- CopilotToolProcessor.java: use custom schema when provided, with
  compile-time JSON validation and schema+defaultValue conflict check.
  Includes a minimal recursive-descent JSON-to-Map.of() converter.
- Param.java: add schema field + fluent mutator for lambda API parity
- CopilotToolProcessorTest.java: 4 new tests
@rinceyuan
rinceyuan requested a review from a team as a code owner July 16, 2026 02:07
@rinceyuan

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree company=Microsoft

@rinceyuan

Copy link
Copy Markdown
Contributor Author

@edburns Hi! This implements #1794 (sibling of #1836) — adds schema attribute to @CopilotToolParam for custom type schema overrides. 4 files changed, 4 new tests. Would appreciate your review. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Java] @CopilotTool ergonomics: Add schema attribute to @Param for custom type schema override

1 participant