Skip to content

Commit 6ebc2fc

Browse files
menchauserwing328
authored andcommitted
Fix support for 'class' properties in Java codegen (#4237)
* Fix support for 'class' properties in Java codegen Currently Java codegen works successfully for property named 'class' but fails on '_class', '__class', etc, because of 'Object.getClass()' overloading. This fix is intended to avoid all Object method overloading cases. * Regenerated samples for Java petstore-security-test
1 parent 4e2c037 commit 6ebc2fc

File tree

41 files changed

+1040
-636
lines changed

Some content is hidden

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

41 files changed

+1040
-636
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public String toVarName(String name) {
361361
// sanitize name
362362
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
363363

364-
if ("class".equals(name.toLowerCase())) {
364+
if (name.toLowerCase().matches("^_*class$")) {
365365
return "propertyClass";
366366
}
367367

modules/swagger-codegen/src/test/java/io/swagger/codegen/java/AbstractJavaCodegenTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public void toEnumVarNameShouldNotShortenUnderScore() throws Exception {
3232
Assert.assertEquals("__", fakeJavaCodegen.toEnumVarName("_,.", "String"));
3333
}
3434

35+
@Test
36+
public void toVarNameShouldAvoidOverloadingGetClassMethod() throws Exception {
37+
Assert.assertEquals("propertyClass", fakeJavaCodegen.toVarName("class"));
38+
Assert.assertEquals("propertyClass", fakeJavaCodegen.toVarName("_class"));
39+
Assert.assertEquals("propertyClass", fakeJavaCodegen.toVarName("__class"));
40+
}
41+
3542
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/java/JavaModelTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,29 @@ public void modelNameTest(String name, String expectedName) {
522522
Assert.assertEquals(cm.name, name);
523523
Assert.assertEquals(cm.classname, expectedName);
524524
}
525+
526+
@DataProvider(name = "classProperties")
527+
public static Object[][] classProperties() {
528+
return new Object[][] {
529+
{"class", "getPropertyClass", "setPropertyClass", "propertyClass"},
530+
{"_class", "getPropertyClass", "setPropertyClass", "propertyClass"},
531+
{"__class", "getPropertyClass", "setPropertyClass", "propertyClass"}
532+
};
533+
}
534+
535+
@Test(dataProvider = "classProperties", description = "handle 'class' properties")
536+
public void classPropertyTest(String baseName, String getter, String setter, String name) {
537+
final Model model = new ModelImpl()
538+
.description("a sample model")
539+
.property(baseName, new StringProperty());
540+
final DefaultCodegen codegen = new JavaClientCodegen();
541+
final CodegenModel cm = codegen.fromModel("sample", model);
542+
543+
final CodegenProperty property = cm.vars.get(0);
544+
Assert.assertEquals(property.baseName, baseName);
545+
Assert.assertEquals(property.getter, getter);
546+
Assert.assertEquals(property.setter, setter);
547+
Assert.assertEquals(property.name, name);
548+
}
549+
525550
}

modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -958,9 +958,14 @@ definitions:
958958
type: integer
959959
format: int32
960960
class:
961-
type: string
961+
type: string
962962
xml:
963963
name: Name
964+
ClassModel:
965+
description: Model for testing model with "_class" property
966+
properties:
967+
_class:
968+
type: string
964969
Dog:
965970
allOf:
966971
- $ref: '#/definitions/Animal'

samples/client/petstore-security-test/java/okhttp-gson/.travis.yml

-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
#
22
# Generated by: https://github.com/swagger-api/swagger-codegen.git
33
#
4-
# Licensed under the Apache License, Version 2.0 (the "License");
5-
# you may not use this file except in compliance with the License.
6-
# You may obtain a copy of the License at
7-
#
8-
# http://www.apache.org/licenses/LICENSE-2.0
9-
#
10-
# Unless required by applicable law or agreed to in writing, software
11-
# distributed under the License is distributed on an "AS IS" BASIS,
12-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
# See the License for the specific language governing permissions and
14-
# limitations under the License.
15-
#
164
language: java
175
jdk:
186
- oraclejdk8

samples/client/petstore-security-test/java/okhttp-gson/LICENSE

-201
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# FakeApi
22

3-
All URIs are relative to *https://petstore.swagger.io ' \" =end/v2 ' \" =end*
3+
All URIs are relative to *https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
7-
[**testCodeInjectEnd**](FakeApi.md#testCodeInjectEnd) | **PUT** /fake | To test code injection ' \" =end
7+
[**testCodeInjectEndRnNR**](FakeApi.md#testCodeInjectEndRnNR) | **PUT** /fake | To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
88

99

10-
<a name="testCodeInjectEnd"></a>
11-
# **testCodeInjectEnd**
12-
> testCodeInjectEnd(testCodeInjectEnd)
10+
<a name="testCodeInjectEndRnNR"></a>
11+
# **testCodeInjectEndRnNR**
12+
> testCodeInjectEndRnNR(testCodeInjectEndRnNR)
1313
14-
To test code injection &#39; \&quot; &#x3D;end
14+
To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
1515

1616
### Example
1717
```java
@@ -21,11 +21,11 @@ To test code injection &#39; \&quot; &#x3D;end
2121

2222

2323
FakeApi apiInstance = new FakeApi();
24-
String testCodeInjectEnd = "testCodeInjectEnd_example"; // String | To test code injection ' \" =end
24+
String testCodeInjectEndRnNR = "testCodeInjectEndRnNR_example"; // String | To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
2525
try {
26-
apiInstance.testCodeInjectEnd(testCodeInjectEnd);
26+
apiInstance.testCodeInjectEndRnNR(testCodeInjectEndRnNR);
2727
} catch (ApiException e) {
28-
System.err.println("Exception when calling FakeApi#testCodeInjectEnd");
28+
System.err.println("Exception when calling FakeApi#testCodeInjectEndRnNR");
2929
e.printStackTrace();
3030
}
3131
```
@@ -34,7 +34,7 @@ try {
3434

3535
Name | Type | Description | Notes
3636
------------- | ------------- | ------------- | -------------
37-
**testCodeInjectEnd** | **String**| To test code injection &#39; \&quot; &#x3D;end | [optional]
37+
**testCodeInjectEndRnNR** | **String**| To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r | [optional]
3838

3939
### Return type
4040

@@ -46,6 +46,6 @@ No authorization required
4646

4747
### HTTP request headers
4848

49-
- **Content-Type**: application/json, */ ' =end
50-
- **Accept**: application/json, */ ' =end
49+
- **Content-Type**: application/json, *_/ ' =end --
50+
- **Accept**: application/json, *_/ ' =end --
5151

samples/client/petstore-security-test/java/okhttp-gson/docs/ModelReturn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
## Properties
55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**_return** | **Integer** | property description &#39; \&quot; &#x3D;end | [optional]
7+
**_return** | **Integer** | property description *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r | [optional]
88

99

1010

0 commit comments

Comments
 (0)