Skip to content

Commit 356d2f2

Browse files
committed
Handle default timestamps consistently
1 parent a5285b2 commit 356d2f2

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

mcp/mcp-server/src/it/java/software/amazon/smithy/java/mcp/server/McpServerIntegrationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ void testEchoSchemaTypeMapping() throws Exception {
275275
assertEquals("number", echoProps.path("epochSecondsTimestamp").path("type").asText());
276276
assertEquals("string", echoProps.path("dateTimeTimestamp").path("type").asText());
277277
assertEquals("string", echoProps.path("httpDateTimestamp").path("type").asText());
278+
// Default timestamp (no format trait) should be string (date-time format)
279+
assertEquals("string", echoProps.path("defaultTimestamp").path("type").asText());
278280

279281
// Enum should be string with enum values
280282
assertEquals("string", echoProps.path("enumValue").path("type").asText());
@@ -435,6 +437,15 @@ void testHttpDateTimestampRoundTrip() {
435437
assertEquals(httpDateStr, echo.getMember("httpDateTimestamp").asString());
436438
}
437439

440+
@Test
441+
void testDefaultTimestampRoundTrip() {
442+
initializeLatestProtocol();
443+
// Default format is date-time (ISO 8601 string)
444+
var dateTimeStr = "2023-11-14T22:13:20Z";
445+
var echo = echoSingleField("defaultTimestamp", Document.of(dateTimeStr));
446+
assertEquals(dateTimeStr, echo.getMember("defaultTimestamp").asString());
447+
}
448+
438449
// ========== List Tests ==========
439450

440451
@Test
@@ -796,6 +807,7 @@ void testInputFieldsAreCorrectlyDeserialized() {
796807
echoData.put("epochSecondsTimestamp", Document.of(1700000000.0));
797808
echoData.put("dateTimeTimestamp", Document.of("2023-11-14T22:13:20Z"));
798809
echoData.put("httpDateTimestamp", Document.of("Tue, 14 Nov 2023 22:13:20 GMT"));
810+
echoData.put("defaultTimestamp", Document.of("2023-11-14T22:13:20Z"));
799811
echoData.put("stringList", Document.of(List.of(Document.of("a"), Document.of("b"))));
800812
echoData.put("integerList", Document.of(List.of(Document.of(1), Document.of(2))));
801813
echoData.put("stringMap", Document.of(Map.of("key1", Document.of("value1"))));
@@ -833,6 +845,7 @@ void testInputFieldsAreCorrectlyDeserialized() {
833845
assertNotNull(echo.getEpochSecondsTimestamp());
834846
assertNotNull(echo.getDateTimeTimestamp());
835847
assertNotNull(echo.getHttpDateTimestamp());
848+
assertNotNull(echo.getDefaultTimestamp());
836849

837850
// Verify collections
838851
assertEquals(2, echo.getStringList().size());
@@ -941,6 +954,7 @@ void testAllTypesValidateAgainstOutputSchema() throws Exception {
941954
echoData.put("epochSecondsTimestamp", Document.of(1700000000.0));
942955
echoData.put("dateTimeTimestamp", Document.of("2023-11-14T22:13:20Z"));
943956
echoData.put("httpDateTimestamp", Document.of("Tue, 14 Nov 2023 22:13:20 GMT"));
957+
echoData.put("defaultTimestamp", Document.of("2023-11-14T22:13:20Z"));
944958
echoData.put("stringList", Document.of(List.of(Document.of("a"))));
945959
echoData.put("integerList", Document.of(List.of(Document.of(1))));
946960
echoData.put("nestedList", Document.of(List.of(nested)));

mcp/mcp-server/src/it/resources/META-INF/smithy/main.smithy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ structure Echo {
9696
dateTimeTimestamp: Timestamp
9797
@timestampFormat("http-date")
9898
httpDateTimestamp: Timestamp
99+
// Default timestamp (no format trait - defaults to date-time)
100+
defaultTimestamp: Timestamp
99101

100102
// Collections
101103
stringList: StringList

mcp/mcp-server/src/main/java/software/amazon/smithy/java/mcp/server/McpService.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,15 +690,11 @@ private static String memberDescription(Schema schema) {
690690
}
691691

692692
private static JsonPrimitiveType resolveTimestampType(Schema schema) {
693-
var trait = schema.getTrait(TraitKey.TIMESTAMP_FORMAT_TRAIT);
694-
if (trait == null) {
695-
// default is epoch-seconds
696-
return JsonPrimitiveType.NUMBER;
697-
}
698-
return switch (trait.getFormat()) {
693+
var format = getFormat(schema.getTrait(TraitKey.TIMESTAMP_FORMAT_TRAIT));
694+
return switch (format) {
699695
case EPOCH_SECONDS -> JsonPrimitiveType.NUMBER;
700696
case DATE_TIME, HTTP_DATE -> JsonPrimitiveType.STRING;
701-
case UNKNOWN -> throw new RuntimeException("unknown timestamp format: " + trait.getFormat());
697+
case UNKNOWN -> throw new RuntimeException("unknown timestamp format: " + format);
702698
};
703699
}
704700

0 commit comments

Comments
 (0)