From b08a0d7426b479dc9a4b189819f50bd9752c59da Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 17 Jan 2024 15:33:06 +0100 Subject: [PATCH] fix issue with array values in eAttributes --- .../ramldoc/extensions/VrapExtensions.kt | 9 ++++++ .../languages/ramldoc/TestCodeGenerator.kt | 32 +++++++++++++++++++ .../ramldoc/src/test/resources/filetype.raml | 12 +++++++ 3 files changed, 53 insertions(+) create mode 100644 languages/ramldoc/src/test/resources/filetype.raml diff --git a/languages/ramldoc/src/main/kotlin/io/vrap/codegen/languages/ramldoc/extensions/VrapExtensions.kt b/languages/ramldoc/src/main/kotlin/io/vrap/codegen/languages/ramldoc/extensions/VrapExtensions.kt index 4c16dece4..8a6ddf5da 100644 --- a/languages/ramldoc/src/main/kotlin/io/vrap/codegen/languages/ramldoc/extensions/VrapExtensions.kt +++ b/languages/ramldoc/src/main/kotlin/io/vrap/codegen/languages/ramldoc/extensions/VrapExtensions.kt @@ -21,6 +21,7 @@ import io.vrap.rmf.raml.model.types.* import io.vrap.rmf.raml.model.types.Annotation import io.vrap.rmf.raml.model.util.StringCaseFormat import io.vrap.rmf.raml.model.values.RegExp +import org.eclipse.emf.ecore.EAttribute import org.eclipse.emf.ecore.EObject import java.io.IOException import java.util.stream.Collectors @@ -47,6 +48,12 @@ fun UriTemplate.normalize(): UriTemplate { return UriTemplate.fromTemplate(this.template.replace("{ID}", "{id}", ignoreCase = true)) } +fun AnyType.renderFileType(): String { + if (!this.isInlineType) { + return "type: ${this.name}" + } + return this.renderEAttributes().plus("type: ${this.name ?: "file"}").joinToString("\n") +} fun AnyType.renderScalarType(): String { if (!this.isInlineType) { return "type: ${this.name}" @@ -76,6 +83,7 @@ fun AnyType.renderEAttributes(): List { .map { eAttribute -> when(val eValue = this.eGet(eAttribute)) { is RegExp -> "${eAttribute.name}: \"${eValue}\"" is String -> "${eAttribute.name}: \"${eValue}\"" + is Collection<*> -> "${eAttribute.name}:\n" + (this.eGet(eAttribute) as Collection<*>).joinToString("\n") { " - ${it?.toYaml()}" } else -> "${eAttribute.name}: ${this.eGet(eAttribute)}" } } } @@ -109,6 +117,7 @@ fun AnyType.renderTypeFacet(): String { is UnionType -> this.renderUnionType() is ObjectType -> this.renderObjectType() is NumberType -> this.renderNumberType() + is FileType -> this.renderFileType() else -> this.renderScalarType()} } diff --git a/languages/ramldoc/src/test/kotlin/io/vrap/codegen/languages/ramldoc/TestCodeGenerator.kt b/languages/ramldoc/src/test/kotlin/io/vrap/codegen/languages/ramldoc/TestCodeGenerator.kt index 920a6f12f..73b8fbc29 100644 --- a/languages/ramldoc/src/test/kotlin/io/vrap/codegen/languages/ramldoc/TestCodeGenerator.kt +++ b/languages/ramldoc/src/test/kotlin/io/vrap/codegen/languages/ramldoc/TestCodeGenerator.kt @@ -45,6 +45,38 @@ class TestCodeGenerator { }""".trimIndent()) } + @Test + fun testFileTypeExample() { + val generatorConfig = CodeGeneratorConfig( + basePackageName = "com/commercetools/importer", + outputFolder = Paths.get("build/gensrc"), + inlineExamples = true + ) + + val apiProvider = RamlApiProvider(Paths.get("src/test/resources/filetype.raml")) + + val dataSink = MemoryDataSink() + val generatorModule = RamlGeneratorModule(apiProvider, generatorConfig, RamldocBaseTypes, dataSink = dataSink) + val generatorComponent = RamlGeneratorComponent(generatorModule, RamldocModelModule) + generatorComponent.generateFiles() + + Assertions.assertThat(dataSink.files).hasSize(2) + Assertions.assertThat(dataSink.files.get("types/foo.raml")?.trim()).isEqualTo(""" + #%RAML 1.0 DataType + displayName: foo + type: object + (builtinType): object + properties: + bar: + fileTypes: + - "*.js" + type: file + (builtinType): file + required: true + (inherited): false + """.trimIndent()) + } + @Test fun testMarkdown() { val generatorConfig = CodeGeneratorConfig( diff --git a/languages/ramldoc/src/test/resources/filetype.raml b/languages/ramldoc/src/test/resources/filetype.raml new file mode 100644 index 000000000..90f554f42 --- /dev/null +++ b/languages/ramldoc/src/test/resources/filetype.raml @@ -0,0 +1,12 @@ +#%RAML 1.0 +--- +title: Commercetools Import API +version: 1.0 +baseUri: http://com.foo.bar/api +types: + foo: + properties: + bar: + type: file + fileTypes: ['*.js'] +