diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java index bf7a33d7b00..81425d35415 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java @@ -999,13 +999,13 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig List operations = (List) objectMap.get("operation"); for (CodegenOperation operation : operations) { for (CodegenParameter cp : operation.allParams) { - cp.vendorExtensions.put("x-swift-example", constructExampleCode(cp, modelMaps, new HashSet())); + cp.vendorExtensions.put("x-swift-example", constructExampleCode(cp, modelMaps, new HashSet())); } } return objs; } - public String constructExampleCode(CodegenParameter codegenParameter, HashMap modelMaps, Set visitedModels) { + public String constructExampleCode(CodegenParameter codegenParameter, HashMap modelMaps, Set visitedModels) { if (codegenParameter.isArray) { // array return "[" + constructExampleCode(codegenParameter.items, modelMaps, visitedModels) + "]"; } else if (codegenParameter.isMap) { // TODO: map, file type @@ -1037,11 +1037,11 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } else { // model // look up the model if (modelMaps.containsKey(codegenParameter.dataType)) { - if (visitedModels.contains(modelMaps.get(codegenParameter.dataType))) { + if (visitedModels.contains(codegenParameter.dataType)) { // recursive/self-referencing model, simply return nil to avoid stackoverflow return "nil"; } else { - visitedModels.add(modelMaps.get(codegenParameter.dataType)); + visitedModels.add(codegenParameter.dataType); return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps, visitedModels); } } else { @@ -1051,7 +1051,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } } - public String constructExampleCode(CodegenProperty codegenProperty, HashMap modelMaps, Set visitedModels) { + public String constructExampleCode(CodegenProperty codegenProperty, HashMap modelMaps, Set visitedModels) { if (codegenProperty.isArray) { // array return "[" + constructExampleCode(codegenProperty.items, modelMaps, visitedModels) + "]"; } else if (codegenProperty.isMap) { // TODO: map, file type @@ -1083,10 +1083,11 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } else { // look up the model if (modelMaps.containsKey(codegenProperty.dataType)) { - if (visitedModels.contains(modelMaps.get(codegenProperty.dataType))) { + if (visitedModels.contains(codegenProperty.dataType)) { // recursive/self-referencing model, simply return nil to avoid stackoverflow return "nil"; } else { + visitedModels.add(codegenProperty.dataType); return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps, visitedModels); } } else { @@ -1096,7 +1097,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } } - public String constructExampleCode(CodegenModel codegenModel, HashMap modelMaps, Set visitedModels) { + public String constructExampleCode(CodegenModel codegenModel, HashMap modelMaps, Set visitedModels) { String example; example = codegenModel.name + "("; List propertyExamples = new ArrayList<>(); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java index 28a3c2684a1..f505c221cbd 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java @@ -185,4 +185,37 @@ public class Swift5ClientCodegenTest { } } + @Test(description = "Bug example code generation 2", enabled = true) + public void crashSwift5ExampleCodeGenerationStackOverflowBug_2Test() throws IOException { + //final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/bugs/Swift5CodeGenerationStackOverflow#2966.yaml"); + Path target = Files.createTempDirectory("test"); + File output = target.toFile(); + try { + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("swift5") + .setValidateSpec(false) +// .setInputSpec("http://localhost:8080/api/openapi.yaml") + .setInputSpec("src/test/resources/bugs/Swift5CodeGenarationBug2.yaml") + //.setInputSpec("http://localhost:8080/api/openapi.yaml") + .setEnablePostProcessFile(true) + .setOutputDir(target.toAbsolutePath().toString()); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(false); + + generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.API_DOCS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.ENABLE_POST_PROCESS_FILE, "true"); + + List files = generator.opts(clientOptInput).generate(); + Assert.assertTrue(files.size() > 0, "No files generated"); + } finally { + output.delete(); + } + } + } diff --git a/modules/openapi-generator/src/test/resources/bugs/Swift5CodeGenarationBug2.yaml b/modules/openapi-generator/src/test/resources/bugs/Swift5CodeGenarationBug2.yaml new file mode 100644 index 00000000000..de5d6bba3e2 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/Swift5CodeGenarationBug2.yaml @@ -0,0 +1,101 @@ +openapi: 3.0.1 +info: + title: XXX + description: Das ist jetzt der erste OpenAPI 3.0 Endpoint + contact: + email: XXX.XXX@XXX.de + version: "1.0" +paths: + /api/v1/petresource/pet: + post: + tags: + - v1/petresource + summary: Save a Pet. + description: Save a Pet. + operationId: savePet + requestBody: + content: + '*/*': + schema: + $ref: '#/components/schemas/Pet' + responses: + default: + description: PetResponse + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + /api/v1/petresource/petstore: + post: + tags: + - v1/petresource + summary: Save a Petstore. + description: Save a Petstore. + operationId: savePetStore + requestBody: + content: + '*/*': + schema: + $ref: '#/components/schemas/PetStore' + responses: + default: + description: PetStore + content: + application/json: + schema: + $ref: '#/components/schemas/PetStore' +components: + schemas: + Cat: + required: + - type + type: object + description: Cat + allOf: + - $ref: '#/components/schemas/Pet' + - type: object + properties: + catFood: + type: string + Dog: + required: + - type + type: object + description: Dog + allOf: + - $ref: '#/components/schemas/Pet' + - type: object + properties: + dogFood: + type: string + Pet: + required: + - type + type: object + properties: + name: + type: string + description: Name + store: + $ref: '#/components/schemas/PetStore' + mother: + $ref: '#/components/schemas/Pet' + father: + $ref: '#/components/schemas/Pet' + type: + type: string + description: Type Diskriminator + description: Base Pet + discriminator: + propertyName: type + mapping: + Dog: '#/components/schemas/Dog' + Cat: '#/components/schemas/Cat' + PetStore: + type: object + properties: + pets: + type: array + items: + $ref: '#/components/schemas/Pet' + description: PetStore diff --git a/samples/client/petstore/swift5/alamofireLibrary/docs/FakeAPI.md b/samples/client/petstore/swift5/alamofireLibrary/docs/FakeAPI.md index 83d9a2873c5..543869c0d2e 100644 --- a/samples/client/petstore/swift5/alamofireLibrary/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/alamofireLibrary/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else { diff --git a/samples/client/petstore/swift5/combineLibrary/docs/FakeAPI.md b/samples/client/petstore/swift5/combineLibrary/docs/FakeAPI.md index 83d9a2873c5..543869c0d2e 100644 --- a/samples/client/petstore/swift5/combineLibrary/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/combineLibrary/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else { diff --git a/samples/client/petstore/swift5/default/docs/FakeAPI.md b/samples/client/petstore/swift5/default/docs/FakeAPI.md index 83d9a2873c5..543869c0d2e 100644 --- a/samples/client/petstore/swift5/default/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/default/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else { diff --git a/samples/client/petstore/swift5/nonPublicApi/docs/FakeAPI.md b/samples/client/petstore/swift5/nonPublicApi/docs/FakeAPI.md index b7dbe0b2260..3b14781480f 100644 --- a/samples/client/petstore/swift5/nonPublicApi/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/nonPublicApi/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else { diff --git a/samples/client/petstore/swift5/objcCompatible/docs/FakeAPI.md b/samples/client/petstore/swift5/objcCompatible/docs/FakeAPI.md index a5a97eb2aeb..712fa591f2f 100644 --- a/samples/client/petstore/swift5/objcCompatible/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/objcCompatible/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else { diff --git a/samples/client/petstore/swift5/promisekitLibrary/docs/FakeAPI.md b/samples/client/petstore/swift5/promisekitLibrary/docs/FakeAPI.md index 72d6935fb92..f2b3e2abe1e 100644 --- a/samples/client/petstore/swift5/promisekitLibrary/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/promisekitLibrary/docs/FakeAPI.md @@ -216,7 +216,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body).then { // when the promise is fulfilled diff --git a/samples/client/petstore/swift5/readonlyProperties/docs/FakeAPI.md b/samples/client/petstore/swift5/readonlyProperties/docs/FakeAPI.md index 83d9a2873c5..543869c0d2e 100644 --- a/samples/client/petstore/swift5/readonlyProperties/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/readonlyProperties/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else { diff --git a/samples/client/petstore/swift5/resultLibrary/docs/FakeAPI.md b/samples/client/petstore/swift5/resultLibrary/docs/FakeAPI.md index 83d9a2873c5..543869c0d2e 100644 --- a/samples/client/petstore/swift5/resultLibrary/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/resultLibrary/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else { diff --git a/samples/client/petstore/swift5/rxswiftLibrary/docs/FakeAPI.md b/samples/client/petstore/swift5/rxswiftLibrary/docs/FakeAPI.md index 0f616c1db2d..9ca07180e07 100644 --- a/samples/client/petstore/swift5/rxswiftLibrary/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/rxswiftLibrary/docs/FakeAPI.md @@ -192,7 +192,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | // TODO RxSwift sample code not yet implemented. To contribute, please open a ticket via http://github.com/OpenAPITools/openapi-generator/issues/new ``` diff --git a/samples/client/petstore/swift5/urlsessionLibrary/docs/FakeAPI.md b/samples/client/petstore/swift5/urlsessionLibrary/docs/FakeAPI.md index 83d9a2873c5..543869c0d2e 100644 --- a/samples/client/petstore/swift5/urlsessionLibrary/docs/FakeAPI.md +++ b/samples/client/petstore/swift5/urlsessionLibrary/docs/FakeAPI.md @@ -228,7 +228,7 @@ For this test, the body for this request much reference a schema named `File`. // The following code samples are still beta. For any issue, please report via http://github.com/OpenAPITools/openapi-generator/issues/new import PetstoreClient -let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [File(sourceURI: "sourceURI_example")]) // FileSchemaTestClass | +let body = FileSchemaTestClass(file: File(sourceURI: "sourceURI_example"), files: [nil]) // FileSchemaTestClass | FakeAPI.testBodyWithFileSchema(body: body) { (response, error) in guard error == nil else {