Skip to content

Commit 828bdeb

Browse files
[MARKDOWN] Fix issue 6089 with property and parameter names (#6105)
Co-authored-by: Jim Schubert <[email protected]>
1 parent 5b22d08 commit 828bdeb

File tree

6 files changed

+75
-40
lines changed

6 files changed

+75
-40
lines changed

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package org.openapitools.codegen.languages;
22

3-
import org.openapitools.codegen.*;
4-
import io.swagger.models.properties.ArrayProperty;
5-
import io.swagger.models.properties.MapProperty;
6-
import io.swagger.models.properties.Property;
7-
import io.swagger.models.parameters.Parameter;
8-
9-
import java.io.File;
10-
import java.util.*;
11-
12-
import org.apache.commons.lang3.StringUtils;
13-
3+
import org.openapitools.codegen.CodegenConfig;
4+
import org.openapitools.codegen.CodegenType;
5+
import org.openapitools.codegen.DefaultCodegen;
6+
import org.openapitools.codegen.SupportingFile;
147
import org.openapitools.codegen.meta.GeneratorMetadata;
158
import org.openapitools.codegen.meta.Stability;
169
import org.slf4j.Logger;
1710
import org.slf4j.LoggerFactory;
1811

12+
import java.io.File;
13+
14+
import static org.openapitools.codegen.utils.StringUtils.escape;
15+
1916
public class MarkdownDocumentationCodegen extends DefaultCodegen implements CodegenConfig {
2017
public static final String PROJECT_NAME = "projectName";
2118

@@ -49,4 +46,37 @@ public MarkdownDocumentationCodegen() {
4946
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
5047
// TODO: Fill this out.
5148
}
49+
50+
@Override
51+
protected void initalizeSpecialCharacterMapping() {
52+
// escape only those symbols that can mess up markdown
53+
specialCharReplacements.put("\\", "\\\\");
54+
specialCharReplacements.put("/", "\\/");
55+
specialCharReplacements.put("`", "\\`");
56+
specialCharReplacements.put("*", "\\*");
57+
specialCharReplacements.put("_", "\\_");
58+
specialCharReplacements.put("[", "\\[");
59+
specialCharReplacements.put("]", "\\]");
60+
61+
// todo Current markdown api and model mustache templates display properties and parameters in tables. Pipe
62+
// symbol in a table can be commonly escaped with a backslash (e.g. GFM supports this). However, in some cases
63+
// it may be necessary to choose a different approach.
64+
specialCharReplacements.put("|", "\\|");
65+
}
66+
67+
/**
68+
* Works identically to {@link DefaultCodegen#toParamName(String)} but doesn't camelize.
69+
*
70+
* @param name Codegen property object
71+
* @return the sanitized parameter name
72+
*/
73+
@Override
74+
public String toParamName(String name) {
75+
if (reservedWords.contains(name)) {
76+
return escapeReservedWord(name);
77+
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains("" + ((char) character)))) {
78+
return escape(name, specialCharReplacements, null, null);
79+
}
80+
return name;
81+
}
5282
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Apis/PetApi.md
2+
Apis/StoreApi.md
3+
Apis/UserApi.md
4+
Models/ApiResponse.md
5+
Models/Category.md
6+
Models/InlineObject.md
7+
Models/InlineObject1.md
8+
Models/Order.md
9+
Models/Pet.md
10+
Models/Tag.md
11+
Models/User.md
12+
README.md

samples/documentation/markdown/Apis/PetApi.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ Method | HTTP request | Description
1616

1717
<a name="addPet"></a>
1818
# **addPet**
19-
> Pet addPet(pet)
19+
> Pet addPet(Pet)
2020
2121
Add a new pet to the store
2222

2323
### Parameters
2424

2525
Name | Type | Description | Notes
2626
------------- | ------------- | ------------- | -------------
27-
**pet** | [**Pet**](..//Models/Pet.md)| Pet object that needs to be added to the store |
27+
**Pet** | [**Pet**](..//Models/Pet.md)| Pet object that needs to be added to the store |
2828

2929
### Return type
3030

@@ -41,7 +41,7 @@ Name | Type | Description | Notes
4141

4242
<a name="deletePet"></a>
4343
# **deletePet**
44-
> deletePet(petId, apiKey)
44+
> deletePet(petId, api\_key)
4545
4646
Deletes a pet
4747

@@ -50,7 +50,7 @@ Deletes a pet
5050
Name | Type | Description | Notes
5151
------------- | ------------- | ------------- | -------------
5252
**petId** | **Long**| Pet id to delete | [default to null]
53-
**apiKey** | **String**| | [optional] [default to null]
53+
**api\_key** | **String**| | [optional] [default to null]
5454

5555
### Return type
5656

@@ -148,15 +148,15 @@ Name | Type | Description | Notes
148148

149149
<a name="updatePet"></a>
150150
# **updatePet**
151-
> Pet updatePet(pet)
151+
> Pet updatePet(Pet)
152152
153153
Update an existing pet
154154

155155
### Parameters
156156

157157
Name | Type | Description | Notes
158158
------------- | ------------- | ------------- | -------------
159-
**pet** | [**Pet**](..//Models/Pet.md)| Pet object that needs to be added to the store |
159+
**Pet** | [**Pet**](..//Models/Pet.md)| Pet object that needs to be added to the store |
160160

161161
### Return type
162162

samples/documentation/markdown/Apis/StoreApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ No authorization required
9090

9191
<a name="placeOrder"></a>
9292
# **placeOrder**
93-
> Order placeOrder(order)
93+
> Order placeOrder(Order)
9494
9595
Place an order for a pet
9696

9797
### Parameters
9898

9999
Name | Type | Description | Notes
100100
------------- | ------------- | ------------- | -------------
101-
**order** | [**Order**](..//Models/Order.md)| order placed for purchasing the pet |
101+
**Order** | [**Order**](..//Models/Order.md)| order placed for purchasing the pet |
102102

103103
### Return type
104104

samples/documentation/markdown/Apis/UserApi.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Method | HTTP request | Description
1616

1717
<a name="createUser"></a>
1818
# **createUser**
19-
> createUser(user)
19+
> createUser(User)
2020
2121
Create user
2222

@@ -26,15 +26,15 @@ Create user
2626

2727
Name | Type | Description | Notes
2828
------------- | ------------- | ------------- | -------------
29-
**user** | [**User**](..//Models/User.md)| Created user object |
29+
**User** | [**User**](..//Models/User.md)| Created user object |
3030

3131
### Return type
3232

3333
null (empty response body)
3434

3535
### Authorization
3636

37-
[auth_cookie](../README.md#auth_cookie)
37+
[api_key](../README.md#api_key)
3838

3939
### HTTP request headers
4040

@@ -43,23 +43,23 @@ null (empty response body)
4343

4444
<a name="createUsersWithArrayInput"></a>
4545
# **createUsersWithArrayInput**
46-
> createUsersWithArrayInput(user)
46+
> createUsersWithArrayInput(User)
4747
4848
Creates list of users with given input array
4949

5050
### Parameters
5151

5252
Name | Type | Description | Notes
5353
------------- | ------------- | ------------- | -------------
54-
**user** | [**List**](..//Models/User.md)| List of user object |
54+
**User** | [**List**](..//Models/User.md)| List of user object |
5555

5656
### Return type
5757

5858
null (empty response body)
5959

6060
### Authorization
6161

62-
[auth_cookie](../README.md#auth_cookie)
62+
[api_key](../README.md#api_key)
6363

6464
### HTTP request headers
6565

@@ -68,23 +68,23 @@ null (empty response body)
6868

6969
<a name="createUsersWithListInput"></a>
7070
# **createUsersWithListInput**
71-
> createUsersWithListInput(user)
71+
> createUsersWithListInput(User)
7272
7373
Creates list of users with given input array
7474

7575
### Parameters
7676

7777
Name | Type | Description | Notes
7878
------------- | ------------- | ------------- | -------------
79-
**user** | [**List**](..//Models/User.md)| List of user object |
79+
**User** | [**List**](..//Models/User.md)| List of user object |
8080

8181
### Return type
8282

8383
null (empty response body)
8484

8585
### Authorization
8686

87-
[auth_cookie](../README.md#auth_cookie)
87+
[api_key](../README.md#api_key)
8888

8989
### HTTP request headers
9090

@@ -111,7 +111,7 @@ null (empty response body)
111111

112112
### Authorization
113113

114-
[auth_cookie](../README.md#auth_cookie)
114+
[api_key](../README.md#api_key)
115115

116116
### HTTP request headers
117117

@@ -184,7 +184,7 @@ null (empty response body)
184184

185185
### Authorization
186186

187-
[auth_cookie](../README.md#auth_cookie)
187+
[api_key](../README.md#api_key)
188188

189189
### HTTP request headers
190190

@@ -193,7 +193,7 @@ null (empty response body)
193193

194194
<a name="updateUser"></a>
195195
# **updateUser**
196-
> updateUser(username, user)
196+
> updateUser(username, User)
197197
198198
Updated user
199199

@@ -204,15 +204,15 @@ Updated user
204204
Name | Type | Description | Notes
205205
------------- | ------------- | ------------- | -------------
206206
**username** | **String**| name that need to be deleted | [default to null]
207-
**user** | [**User**](..//Models/User.md)| Updated user object |
207+
**User** | [**User**](..//Models/User.md)| Updated user object |
208208

209209
### Return type
210210

211211
null (empty response body)
212212

213213
### Authorization
214214

215-
[auth_cookie](../README.md#auth_cookie)
215+
[api_key](../README.md#api_key)
216216

217217
### HTTP request headers
218218

samples/documentation/markdown/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ Class | Method | HTTP request | Description
5252
- **API key parameter name**: api_key
5353
- **Location**: HTTP header
5454

55-
<a name="auth_cookie"></a>
56-
### auth_cookie
57-
58-
- **Type**: API key
59-
- **API key parameter name**: AUTH_KEY
60-
- **Location**:
61-
6255
<a name="petstore_auth"></a>
6356
### petstore_auth
6457

0 commit comments

Comments
 (0)