Skip to content

Commit

Permalink
fix issue with array values in eAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Jan 17, 2024
1 parent f0ea4c9 commit b08a0d7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down Expand Up @@ -76,6 +83,7 @@ fun AnyType.renderEAttributes(): List<String> {
.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)}"
} }
}
Expand Down Expand Up @@ -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()}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions languages/ramldoc/src/test/resources/filetype.raml
Original file line number Diff line number Diff line change
@@ -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']

0 comments on commit b08a0d7

Please sign in to comment.