Skip to content

Commit 82c5021

Browse files
authored
Revert "Bugfix swift5 code generation 2966 (#7301)" and provide a better (#8066)
fix for the stackoverflow issue
1 parent 634c4c0 commit 82c5021

File tree

23 files changed

+86
-113
lines changed

23 files changed

+86
-113
lines changed

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

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public Swift5ClientCodegen() {
246246
cliOptions.add(new CliOption(POD_DOCUMENTATION_URL,
247247
"Documentation URL used for Podspec"));
248248
cliOptions.add(new CliOption(READONLY_PROPERTIES, "Make properties "
249-
+ "readonly (default: false)"));
249+
+ "readonly (default: false)"));
250250
cliOptions.add(new CliOption(SWIFT_USE_API_NAMESPACE,
251251
"Flag to make all the API classes inner-class "
252252
+ "of {{projectName}}API"));
@@ -997,15 +997,15 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
997997
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
998998
for (CodegenOperation operation : operations) {
999999
for (CodegenParameter cp : operation.allParams) {
1000-
cp.vendorExtensions.put("x-swift-example", constructExampleCode(cp, modelMaps, new ExampleCodeGenerationContext()));
1000+
cp.vendorExtensions.put("x-swift-example", constructExampleCode(cp, modelMaps, new HashSet()));
10011001
}
10021002
}
10031003
return objs;
10041004
}
10051005

1006-
public String constructExampleCode(CodegenParameter codegenParameter, HashMap<String, CodegenModel> modelMaps, ExampleCodeGenerationContext context) {
1006+
public String constructExampleCode(CodegenParameter codegenParameter, HashMap<String, CodegenModel> modelMaps, Set visitedModels) {
10071007
if (codegenParameter.isArray) { // array
1008-
return "[" + constructExampleCode(codegenParameter.items, modelMaps, context) + "]";
1008+
return "[" + constructExampleCode(codegenParameter.items, modelMaps, visitedModels) + "]";
10091009
} else if (codegenParameter.isMap) { // TODO: map, file type
10101010
return "\"TODO\"";
10111011
} else if (languageSpecificPrimitives.contains(codegenParameter.dataType)) { // primitive type
@@ -1035,17 +1035,23 @@ public String constructExampleCode(CodegenParameter codegenParameter, HashMap<St
10351035
} else { // model
10361036
// look up the model
10371037
if (modelMaps.containsKey(codegenParameter.dataType)) {
1038-
return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps, context);
1038+
if (visitedModels.contains(modelMaps.get(codegenParameter.dataType))) {
1039+
// recursive/self-referencing model, simply return nil to avoid stackoverflow
1040+
return "nil";
1041+
} else {
1042+
visitedModels.add(modelMaps.get(codegenParameter.dataType));
1043+
return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps, visitedModels);
1044+
}
10391045
} else {
10401046
//LOGGER.error("Error in constructing examples. Failed to look up the model " + codegenParameter.dataType);
10411047
return "TODO";
10421048
}
10431049
}
10441050
}
10451051

1046-
private String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps, ExampleCodeGenerationContext context) {
1052+
public String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps, Set visitedModels) {
10471053
if (codegenProperty.isArray) { // array
1048-
return "[" + constructExampleCode(codegenProperty.items, modelMaps, context) + "]";
1054+
return "[" + constructExampleCode(codegenProperty.items, modelMaps, visitedModels) + "]";
10491055
} else if (codegenProperty.isMap) { // TODO: map, file type
10501056
return "\"TODO\"";
10511057
} else if (languageSpecificPrimitives.contains(codegenProperty.dataType)) { // primitive type
@@ -1075,59 +1081,28 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
10751081
} else {
10761082
// look up the model
10771083
if (modelMaps.containsKey(codegenProperty.dataType)) {
1078-
return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps, context);
1084+
if (visitedModels.contains(modelMaps.get(codegenProperty.dataType))) {
1085+
// recursive/self-referencing model, simply return nil to avoid stackoverflow
1086+
return "nil";
1087+
} else {
1088+
return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps, visitedModels);
1089+
}
10791090
} else {
10801091
//LOGGER.error("Error in constructing examples. Failed to look up the model " + codegenProperty.dataType);
10811092
return "\"TODO\"";
10821093
}
10831094
}
10841095
}
10851096

1086-
private String constructExampleCode(CodegenModel codegenModel, HashMap<String, CodegenModel> modelMaps, ExampleCodeGenerationContext context) {
1087-
if (context.isTypeVisted(codegenModel.dataType)) {
1088-
String exampleCode = context.getExampleCode(codegenModel.dataType);
1089-
if (exampleCode != null) {
1090-
// Reuse already generated exampleCode
1091-
return exampleCode;
1092-
} else {
1093-
// Visited but no Example Code. Circuit Breaker --> No StackOverflow
1094-
return "{...}";
1095-
}
1096-
} else {
1097-
context.visitType(codegenModel.dataType);
1098-
String example = codegenModel.name + "(";
1099-
List<String> propertyExamples = new ArrayList<>();
1100-
for (CodegenProperty codegenProperty : codegenModel.vars) {
1101-
String propertyExample = constructExampleCode(codegenProperty, modelMaps, context);
1102-
propertyExamples.add(codegenProperty.name + ": " + propertyExample);
1103-
}
1104-
example += StringUtils.join(propertyExamples, ", ");
1105-
example += ")";
1106-
1107-
context.setExampleCode(codegenModel.dataType, example);
1108-
return example;
1097+
public String constructExampleCode(CodegenModel codegenModel, HashMap<String, CodegenModel> modelMaps, Set visitedModels) {
1098+
String example;
1099+
example = codegenModel.name + "(";
1100+
List<String> propertyExamples = new ArrayList<>();
1101+
for (CodegenProperty codegenProperty : codegenModel.vars) {
1102+
propertyExamples.add(codegenProperty.name + ": " + constructExampleCode(codegenProperty, modelMaps, visitedModels));
11091103
}
1110-
}
1111-
1112-
private static class ExampleCodeGenerationContext {
1113-
1114-
private Map<String, String> modelExampleCode = new HashMap<>();
1115-
1116-
public boolean isTypeVisted(String type) {
1117-
return modelExampleCode.containsKey(type);
1118-
}
1119-
1120-
public void visitType(String type) {
1121-
modelExampleCode.put(type, null);
1122-
}
1123-
1124-
public void setExampleCode(String type, String code) {
1125-
modelExampleCode.put(type, code);
1126-
}
1127-
1128-
public String getExampleCode(String type) {
1129-
return modelExampleCode.get(type);
1130-
}
1131-
1104+
example += StringUtils.join(propertyExamples, ", ");
1105+
example += ")";
1106+
return example;
11321107
}
11331108
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Arrays;
3838
import java.util.List;
3939

40-
4140
public class Swift5ClientCodegenTest {
4241

4342
Swift5ClientCodegen swiftCodegen = new Swift5ClientCodegen();
@@ -129,6 +128,32 @@ public void dateTest() {
129128
Assert.assertEquals(op.bodyParam.dataType, "Date");
130129
}
131130

131+
@Test(enabled = true)
132+
public void testDefaultPodAuthors() throws Exception {
133+
// Given
134+
135+
// When
136+
swiftCodegen.processOpts();
137+
138+
// Then
139+
final String podAuthors = (String) swiftCodegen.additionalProperties().get(Swift5ClientCodegen.POD_AUTHORS);
140+
Assert.assertEquals(podAuthors, Swift5ClientCodegen.DEFAULT_POD_AUTHORS);
141+
}
142+
143+
@Test(enabled = true)
144+
public void testPodAuthors() throws Exception {
145+
// Given
146+
final String openAPIDevs = "OpenAPI Devs";
147+
swiftCodegen.additionalProperties().put(Swift5ClientCodegen.POD_AUTHORS, openAPIDevs);
148+
149+
// When
150+
swiftCodegen.processOpts();
151+
152+
// Then
153+
final String podAuthors = (String) swiftCodegen.additionalProperties().get(Swift5ClientCodegen.POD_AUTHORS);
154+
Assert.assertEquals(podAuthors, openAPIDevs);
155+
}
156+
132157
@Test(description = "Bug example code generation", enabled = true)
133158
public void crashSwift5ExampleCodeGenerationStackOverflowTest() throws IOException {
134159
//final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/bugs/Swift5CodeGenerationStackOverflow#2966.yaml");
@@ -160,31 +185,4 @@ public void crashSwift5ExampleCodeGenerationStackOverflowTest() throws IOExcepti
160185
}
161186
}
162187

163-
164-
@Test(enabled = true)
165-
public void testDefaultPodAuthors() throws Exception {
166-
// Given
167-
168-
// When
169-
swiftCodegen.processOpts();
170-
171-
// Then
172-
final String podAuthors = (String) swiftCodegen.additionalProperties().get(Swift5ClientCodegen.POD_AUTHORS);
173-
Assert.assertEquals(podAuthors, Swift5ClientCodegen.DEFAULT_POD_AUTHORS);
174-
}
175-
176-
@Test(enabled = true)
177-
public void testPodAuthors() throws Exception {
178-
// Given
179-
final String openAPIDevs = "OpenAPI Devs";
180-
swiftCodegen.additionalProperties().put(Swift5ClientCodegen.POD_AUTHORS, openAPIDevs);
181-
182-
// When
183-
swiftCodegen.processOpts();
184-
185-
// Then
186-
final String podAuthors = (String) swiftCodegen.additionalProperties().get(Swift5ClientCodegen.POD_AUTHORS);
187-
Assert.assertEquals(podAuthors, openAPIDevs);
188-
}
189-
190188
}

samples/client/petstore/swift5/alamofireLibrary/docs/FakeAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`.
228228
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
229229
import PetstoreClient
230230

231-
let body = FileSchemaTestClass(file: {...}, files: [{...}]) // FileSchemaTestClass |
231+
let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass |
232232

233233
FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in
234234
guard error == nil else {

samples/client/petstore/swift5/alamofireLibrary/docs/PetAPI.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Add a new pet to the store
2727
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
2828
import PetstoreClient
2929

30-
let body = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
30+
let body = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
3131

3232
// Add a new pet to the store
3333
PetAPI.addPet(body: body) { (response, error) in
@@ -275,7 +275,7 @@ Update an existing pet
275275
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
276276
import PetstoreClient
277277

278-
let body = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
278+
let body = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
279279

280280
// Update an existing pet
281281
PetAPI.updatePet(body: body) { (response, error) in

samples/client/petstore/swift5/combineLibrary/docs/FakeAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`.
228228
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
229229
import PetstoreClient
230230

231-
let body = FileSchemaTestClass(file: {...}, files: [{...}]) // FileSchemaTestClass |
231+
let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass |
232232

233233
FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in
234234
guard error == nil else {

samples/client/petstore/swift5/combineLibrary/docs/PetAPI.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Add a new pet to the store
2727
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
2828
import PetstoreClient
2929

30-
let body = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
30+
let body = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
3131

3232
// Add a new pet to the store
3333
PetAPI.addPet(body: body) { (response, error) in
@@ -275,7 +275,7 @@ Update an existing pet
275275
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
276276
import PetstoreClient
277277

278-
let body = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
278+
let body = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
279279

280280
// Update an existing pet
281281
PetAPI.updatePet(body: body) { (response, error) in

samples/client/petstore/swift5/default/docs/FakeAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`.
228228
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
229229
import PetstoreClient
230230

231-
let body = FileSchemaTestClass(file: {...}, files: [{...}]) // FileSchemaTestClass |
231+
let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass |
232232

233233
FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in
234234
guard error == nil else {

samples/client/petstore/swift5/default/docs/PetAPI.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Add a new pet to the store
2727
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
2828
import PetstoreClient
2929

30-
let body = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
30+
let body = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
3131

3232
// Add a new pet to the store
3333
PetAPI.addPet(body: body) { (response, error) in
@@ -275,7 +275,7 @@ Update an existing pet
275275
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
276276
import PetstoreClient
277277

278-
let body = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
278+
let body = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
279279

280280
// Update an existing pet
281281
PetAPI.updatePet(body: body) { (response, error) in

samples/client/petstore/swift5/deprecated/docs/PetAPI.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Add a new pet to the store
2626
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
2727
import PetstoreClient
2828

29-
let pet = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
29+
let pet = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
3030

3131
// Add a new pet to the store
3232
PetAPI.addPet(pet: pet) { (response, error) in
@@ -274,7 +274,7 @@ Update an existing pet
274274
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
275275
import PetstoreClient
276276

277-
let pet = Pet(id: 123, category: {...}, name: "name_example", photoUrls: ["photoUrls_example"], tags: [{...}], status: "status_example") // Pet | Pet object that needs to be added to the store
277+
let pet = Pet(id: 123, category: Category(id: 123, name: "name_example"), name: "name_example", photoUrls: ["photoUrls_example"], tags: [Tag(id: 123, name: "name_example")], status: "status_example") // Pet | Pet object that needs to be added to the store
278278

279279
// Update an existing pet
280280
PetAPI.updatePet(pet: pet) { (response, error) in

samples/client/petstore/swift5/nonPublicApi/docs/FakeAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`.
228228
// The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new
229229
import PetstoreClient
230230

231-
let body = FileSchemaTestClass(file: {...}, files: [{...}]) // FileSchemaTestClass |
231+
let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass |
232232

233233
FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in
234234
guard error == nil else {

0 commit comments

Comments
 (0)