From 9172ce849719878684eb93c72fdf07ecc205295d Mon Sep 17 00:00:00 2001 From: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> Date: Wed, 28 Jan 2026 16:24:39 +0100 Subject: [PATCH] [go] Fix text escaping in example code generation Wrap example values with escapeText() to properly escape special characters in generated Go client code examples Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> --- .../codegen/languages/GoClientCodegen.java | 8 +-- .../codegen/go/GoClientCodegenTest.java | 23 ++++++++ ...tstore-with-special-chars-in-examples.yaml | 52 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/go/petstore-with-special-chars-in-examples.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index 9364c2582717..a35aa26bbd50 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -608,7 +608,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap files = generator.opts(configurator.toClientOptInput()).generate(); + files.forEach(File::deleteOnExit); + + Path docPath = Paths.get(output + "/docs/TestAPI.md"); + // Verify that quotes are properly escaped in parameter examples + TestUtils.assertFileContains(docPath, "stringWithQuotes := \"John \\\"Johnny\\\" Doe\""); + // Verify that backslashes are properly escaped in parameter examples + TestUtils.assertFileContains(docPath, "stringWithBackslash := \"C:\\\\path\\\\to\\\\file\""); + // Verify that quotes are properly escaped in email parameter examples + TestUtils.assertFileContains(docPath, "emailWithQuotes := \"test\\\"user@example.com\""); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-special-chars-in-examples.yaml b/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-special-chars-in-examples.yaml new file mode 100644 index 000000000000..f9ad0fd52de0 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-special-chars-in-examples.yaml @@ -0,0 +1,52 @@ +openapi: 3.0.0 +info: + title: Test Escaping in Examples + version: 1.0.0 +paths: + /test: + get: + operationId: testEscaping + tags: + - test + parameters: + - name: stringWithQuotes + in: query + required: true + schema: + type: string + example: 'John "Johnny" Doe' + - name: stringWithBackslash + in: query + required: true + schema: + type: string + example: 'C:\path\to\file' + - name: emailWithQuotes + in: query + required: true + schema: + type: string + format: email + example: 'test"user@example.com' + responses: + '200': + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/TestResponse' +components: + schemas: + TestResponse: + type: object + properties: + name: + type: string + example: 'Name with "quotes" inside' + path: + type: string + example: 'C:\Windows\System32' + email: + type: string + format: email + example: 'user"name@example.com'