[kotlin-spring] Escape generated values by source context

PI
9m ago
+1204-321

Summary

The kotlin-spring generator now escapes values from the OpenAPI document correctly for the place each one lands in the generated code. Before this change, templates mostly used fields that were already HTML/Javadoc-escaped and wrapped them in hand-written "..." or """...""". When a value contained ", \, $, */, line breaks or control characters, the generated Kotlin could fail to compile. It could also end up with the wrong string value (double-escaped, or $ read as string interpolation), or contain a broken KDoc.

Changes

New escaper: SourceStringEscaper (org.openapitools.codegen.templating). It takes raw, unescaped input and returns one of:

  • a Kotlin string literal or its content: escapes \, " and $, writes control characters as \uXXXX
  • text that is safe inside /* */ doc comments: HTML entities, /* and */ broken up, continuation lines prefixed with *

Templates use it through three new lambdas: kotlinStringLiteral, kotlinStringContent and kotlinDocText.

Raw values available to templates, so each value is escaped once, for its target context:

  • operations: unescapedPath, unescapedSummary
  • responses: unescapedMessage
  • body parameters: unescapedDescription
  • enums: valueRaw
  • security scopes: descriptionRaw
  • properties and parameters: rawDefaultValue, rawDefaultValueText, hasDefaultValue
  • generator metadata: appNameRaw, appVersionRaw, infoEmailRaw, infoNameRaw, infoUrlRaw, licenseInfoRaw, licenseUrlRaw, termsOfServiceRaw, contextPathRaw

These are additions only. Existing fields and other generators' output are unchanged.

kotlin-spring templates now use these values for:

  • annotations (@Operation, @ApiResponse, @Parameter, @Schema, @RequestMapping, security requirements, swagger1 Authorization)
  • path and base-path constants
  • @JsonProperty names
  • enum values
  • discriminator / type-info annotations
  • the SpringDocConfiguration metadata
  • KDoc

KotlinSpringServerCodegen:

  • String and URI defaults and discriminator default values are written as escaped Kotlin literals.
  • Spring's form binding does not apply OpenAPI defaults to form parameters. The generator now logs a warning and the KDoc says "OpenAPI schema default to …" instead of claiming a default is applied.

Samples: the kotlin-spring samples are regenerated. Most of the diff replaces description = """...""" with standard Kotlin string literals. KDoc entities are normalized as well.

Testing

  • New tests in KotlinSpringServerCodegenTest use the new 3_0/spring/escaping-regressions.yaml and 3_0/spring/kotlin-escaping-regressions.yaml fixtures. They cover paths, context paths, OAuth names and scopes, SpringDoc metadata, URI and decimal defaults, discriminators, enum values, form-parameter defaults and KDoc, across the controller, interface-only, delegate, service, swagger1 and declarative-http-interface variants.
  • New SourceStringEscaperTest.
  • KotlinSpringServerCodegenTest, SourceStringEscaperTest, DefaultCodegenTest and DefaultGeneratorTest all pass (555 tests).

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Updates the Kotlin Spring generator to escape generated values per target context, fixing compiled code when quotes, backslashes, dollar signs, or doc-comment delimiters appear in OpenAPI values.

Values now stay unescaped in the code model and are encoded where rendered via SourceStringEscaper (kotlinStringLiteral, kotlinStringContent, kotlinDocText lambdas), covering annotations, path constants, enum values, JSON property names, and KDoc. String/URI defaults and discriminator values are written as Kotlin literals, and KDoc/@Operation output no longer uses triple-quoted strings. Multi-line property examples keep their line breaks and control characters instead of being stripped.

Bug Fixes

  • Keeps date/date-time parameter defaults unset in both the raw snapshot and the template-facing default, so timezone-shifted date-times cannot reach @Parameter, @CookieValue, or KDoc fallbacks that Spring cannot bind; the regression fixture covers header and cookie parameters.
  • Documents that Spring form binding does not apply OpenAPI defaults to form parameters.
  • Adds regression fixtures covering Java and Kotlin escaping cases, and a SourceStringEscaperTest.