From 09457bfe707de18d175de96b4a9045312e556f64 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Tue, 30 Jun 2026 11:01:22 +0200 Subject: [PATCH] Add MessageAdapter content parts serialization test coverage --- .../aiguard/AIGuardInternalTests.groovy | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dd-java-agent/agent-aiguard/src/test/groovy/com/datadog/aiguard/AIGuardInternalTests.groovy b/dd-java-agent/agent-aiguard/src/test/groovy/com/datadog/aiguard/AIGuardInternalTests.groovy index 406b16c7025..e355b2eec3c 100644 --- a/dd-java-agent/agent-aiguard/src/test/groovy/com/datadog/aiguard/AIGuardInternalTests.groovy +++ b/dd-java-agent/agent-aiguard/src/test/groovy/com/datadog/aiguard/AIGuardInternalTests.groovy @@ -861,6 +861,28 @@ class AIGuardInternalTests extends DDSpecification { } } + void 'test adapter serializes content parts'() { + given: + final adapter = new Moshi.Builder().add(new AIGuardInternal.AIGuardFactory()).build() + .adapter(AIGuard.Message) + + expect: + // STRICT enforces array element ordering (object key order is always ignored), so a regression + // that serialized the content parts out of sequence would be caught here + JSONAssert.assertEquals(expected, adapter.toJson(message), JSONCompareMode.STRICT) + + where: + message | expected + AIGuard.Message.message('user', [] as List) | '{"role": "user", "content": []}' + AIGuard.Message.message('user', [AIGuard.ContentPart.text('Hello world')]) | '{"role": "user", "content": [{"type": "text", "text": "Hello world"}]}' + AIGuard.Message.message('user', [AIGuard.ContentPart.imageUrl('https://example.com/image.jpg')]) | '{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}]}' + AIGuard.Message.message('user', [ + AIGuard.ContentPart.text('Describe this image:'), + AIGuard.ContentPart.imageUrl('https://example.com/image.jpg'), + AIGuard.ContentPart.text('What do you see?') + ]) | '{"role": "user", "content": [{"type": "text", "text": "Describe this image:"}, {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}, {"type": "text", "text": "What do you see?"}]}' + } + void 'test backward compatibility with string content'() { given: final aiguard = mockClient(200, [data: [attributes: [action: 'ALLOW', reason: 'Good']]])