Skip to content

Commit 9981a40

Browse files
authored
[kotlin] Added path sanitization in javadoc comments (#20767)
* [kotlin] Added path sanitization in javadoc comments * added sample as regression test * Added samples/client/others/kotlin-jvm-okhttp-path-comments to github workflow
1 parent 91630b8 commit 9981a40

File tree

43 files changed

+1577
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1577
-7
lines changed

.github/workflows/samples-kotlin-client.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jobs:
6565
- samples/client/echo_api/kotlin-jvm-spring-3-restclient
6666
- samples/client/petstore/kotlin-name-parameter-mappings
6767
- samples/client/others/kotlin-jvm-okhttp-parameter-tests
68+
- samples/client/others/kotlin-jvm-okhttp-path-comments
6869
steps:
6970
- uses: actions/checkout@v4
7071
- uses: actions/setup-java@v4
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
generatorName: kotlin
2+
outputDir: samples/client/others/kotlin-jvm-okhttp-path-comments
3+
library: jvm-okhttp4
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue20618-path-comments.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
6+
additionalProperties:
7+
artifactId: kotlin-petstore-okhttp4-path-comments
8+

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,35 @@
4040
import java.util.stream.Collectors;
4141
import java.util.stream.Stream;
4242

43+
import com.samskivert.mustache.Mustache;
44+
import lombok.Getter;
45+
import lombok.Setter;
46+
import org.apache.commons.lang3.StringUtils;
47+
import org.openapitools.codegen.CliOption;
48+
import org.openapitools.codegen.CodegenConstants;
49+
import org.openapitools.codegen.CodegenModel;
50+
import org.openapitools.codegen.CodegenOperation;
51+
import org.openapitools.codegen.CodegenParameter;
52+
import org.openapitools.codegen.CodegenProperty;
53+
import org.openapitools.codegen.CodegenType;
54+
import org.openapitools.codegen.SupportingFile;
55+
import org.openapitools.codegen.VendorExtension;
56+
import org.openapitools.codegen.meta.features.ClientModificationFeature;
57+
import org.openapitools.codegen.meta.features.DocumentationFeature;
58+
import org.openapitools.codegen.meta.features.GlobalFeature;
59+
import org.openapitools.codegen.meta.features.ParameterFeature;
60+
import org.openapitools.codegen.meta.features.SchemaSupportFeature;
61+
import org.openapitools.codegen.meta.features.SecurityFeature;
62+
import org.openapitools.codegen.meta.features.WireFormatFeature;
63+
import org.openapitools.codegen.model.ModelMap;
64+
import org.openapitools.codegen.model.ModelsMap;
65+
import org.openapitools.codegen.model.OperationMap;
66+
import org.openapitools.codegen.model.OperationsMap;
67+
import org.openapitools.codegen.templating.mustache.ReplaceAllLambda;
68+
import org.openapitools.codegen.utils.ProcessUtils;
69+
import org.slf4j.Logger;
70+
import org.slf4j.LoggerFactory;
71+
4372
import static java.util.Collections.sort;
4473

4574
public class KotlinClientCodegen extends AbstractKotlinCodegen {
@@ -536,6 +565,10 @@ public void processOpts() {
536565
}
537566
writer.write(content);
538567
});
568+
// When a path is added to a Javadoc, if it ends with a `/*` is will cause a compiler error
569+
// as the parser interrupts that as a start of a multiline comment.
570+
// We replace paths like `/v1/foo/*` with `/v1/foo/<*>` to avoid this
571+
additionalProperties.put("sanitizePathComment", new ReplaceAllLambda("\\/\\*", "/<*>"));
539572
}
540573

541574
private void processDateLibrary() {

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-ktor/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
4343
4444
{{#operation}}
4545
/**
46-
* {{{httpMethod}}} {{{path}}}
46+
* {{{httpMethod}}} {{#sanitizePathComment}}{{{path}}}{{/sanitizePathComment}}
4747
* {{summary}}
4848
* {{notes}}
4949
{{#allParams}} * @param {{{paramName}}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ import {{packageName}}.infrastructure.toMultiValue
123123
{{/isEnum}}
124124
{{/allParams}}
125125
/**
126-
* {{{httpMethod}}} {{{path}}}
126+
* {{{httpMethod}}} {{#sanitizePathComment}}{{{path}}}{{/sanitizePathComment}}
127127
* {{summary}}
128128
* {{notes}}
129129
{{#allParams}}* @param {{{paramName}}} {{description}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{#required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}{{/required}}
@@ -161,7 +161,7 @@ import {{packageName}}.infrastructure.toMultiValue
161161
}
162162

163163
/**
164-
* {{{httpMethod}}} {{{path}}}
164+
* {{{httpMethod}}} {{#sanitizePathComment}}{{{path}}}{{/sanitizePathComment}}
165165
* {{summary}}
166166
* {{notes}}
167167
{{#allParams}}* @param {{{paramName}}} {{description}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{#required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}{{/required}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ import okhttp3.MultipartBody
131131
{{/isEnum}}
132132
{{/allParams}}
133133
/**
134-
* {{{httpMethod}}} {{{path}}}
134+
* {{{httpMethod}}} {{#sanitizePathComment}}{{{path}}}{{/sanitizePathComment}}
135135
* {{summary}}
136136
* {{notes}}
137137
* Responses:{{#responses}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-vertx/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import {{packageName}}.infrastructure.*
8888
{{/isEnum}}
8989
{{/allParams}}
9090
/**
91-
* {{{httpMethod}}} {{{path}}}
91+
* {{{httpMethod}}} {{#sanitizePathComment}}{{{path}}}{{/sanitizePathComment}}
9292
* {{summary}}
9393
* {{notes}}
9494
{{#allParams}}* @param {{{paramName}}} {{description}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{#required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}{{/required}}
@@ -123,7 +123,7 @@ import {{packageName}}.infrastructure.*
123123
}
124124

125125
/**
126-
* {{{httpMethod}}} {{{path}}}
126+
* {{{httpMethod}}} {{#sanitizePathComment}}{{{path}}}{{/sanitizePathComment}}
127127
* {{summary}}
128128
* {{notes}}
129129
{{#allParams}}* @param {{{paramName}}} {{description}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{#required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}{{/required}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-volley/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {{packageName}}.infrastructure.CollectionFormats.*
3737
3838
{{#operation}}
3939
/**
40-
* {{{httpMethod}}} {{{path}}}
40+
* {{{httpMethod}}} {{#sanitizePathComment}}{{{path}}}{{/sanitizePathComment}}
4141
* {{summary}}
4242
* {{notes}}
4343
{{#allParams}}* @param {{{paramName}}} {{description}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{#required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}{{/required}}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See: https://github.com/OpenAPITools/openapi-generator/pull/20618#issuecomment-2690874645
2+
openapi: 3.0.0
3+
servers:
4+
- url: 'https://example.org/v1'
5+
info:
6+
description: >-
7+
Example created
8+
version: 1.0.0
9+
title: OpenAPI Stuff API created to reproduce issue
10+
license:
11+
name: Apache-2.0
12+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
13+
tags:
14+
- name: stuff
15+
description: All about the stuff
16+
security:
17+
- bearerAuth: []
18+
paths:
19+
/foo/*:
20+
get:
21+
tags:
22+
- stuff
23+
summary: Finds stuff
24+
description: Finds stuff
25+
operationId: findStuff
26+
responses:
27+
'200':
28+
description: successful operation
29+
content:
30+
application/json:
31+
schema:
32+
type: object
33+
properties:
34+
id:
35+
type: string
36+
components:
37+
securitySchemes:
38+
bearerAuth:
39+
type: http
40+
scheme: bearer
41+
bearerFormat: JWT
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md

0 commit comments

Comments
 (0)