Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ class RamlObjectTypeRenderer constructor(override val vrapTypeProvider: VrapType
| <<${property.type.enum.joinToString("\n") { "- '${it.value}'" }}>>""" else ""}${if (examples.isNotEmpty()) """
| examples:
| <<${examples.joinToString("\n") { renderExample(it) }}>>""" else ""}${if (discriminatorProp != property.name && property.type.default != null) """
| default: ${property.type.default.toYaml()}""" else ""}${if (property.type?.isInlineType == true && property.type?.annotations != null) """
| default: ${if (property.type.default is ObjectInstance || property.type.default is ArrayInstance) """
| <<${property.type.default.toYaml().escapeAll()}>>""" else property.type.default.toYaml()}""" else ""}${if (property.type?.isInlineType == true && property.type?.annotations != null) """
| <<${property.type.annotations.joinToString("\n") { it.renderAnnotation() }}>>""" else ""}
| required: ${property.required}
| (inherited): $inherited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ class RamlResourceRenderer constructor(val api: Api, val vrapTypeProvider: VrapT
}
return """
|${parameterName}:${if (queryParameter.type.default != null) """
| default: ${queryParameter.type.default.toYaml()}""" else ""}
| default: ${if (queryParameter.type.default is ObjectInstance || queryParameter.type.default is ArrayInstance) """
| <<${queryParameter.type.default.toYaml().escapeAll()}>>""" else queryParameter.type.default.toYaml()}""" else ""}
| required: ${queryParameter.required}
| <<${queryParameter.type.renderType()}>>${if (parameterExamples.isNotEmpty()) """
| examples:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,106 @@ class TestCodeGenerator {
.doesNotContain("value: |")
}

@Test
fun testDefaultValueRenders() {
val generatorConfig = CodeGeneratorConfig(
basePackageName = "com/commercetools/importer",
outputFolder = Paths.get("build/gensrc"),
inlineExamples = true
)

val apiProvider = RamlApiProvider(Paths.get("src/test/resources/arraydefault.raml"))

val dataSink = MemoryDataSink()
val generatorModule = RamlGeneratorModule(apiProvider, generatorConfig, RamldocBaseTypes, dataSink = dataSink)
val generatorComponent = RamlGeneratorComponent(generatorModule, RamldocModelModule)
generatorComponent.generateFiles()

Assertions.assertThat(dataSink.files).isNotEmpty()
val typeContent = dataSink.files.get("types/foo.raml")
Assertions.assertThat(typeContent).isNotNull()
Assertions.assertThat(typeContent).isEqualTo("""
#%RAML 1.0 DataType
displayName: foo
type: object
(builtinType): object
properties:
foo:
type: object
(builtinType): object
default:
test: 1
required: true
(inherited): false
bar:
type: array
items:
type: string
(builtinType): array
default:
- "foo"
- "bar"
required: true
(inherited): false
foobaz:
type: array
items:
type: string
(builtinType): array
default:
- "foo"
required: true
(inherited): false
fooz:
type: array
items:
type: string
(builtinType): array
default:
- "foo"
required: true
(inherited): false
baz:
type: string
(builtinType): string
default: "baz"
required: true
(inherited): false
foobar:
type: object
(builtinType): object
default:
test: 1
required: true
(inherited): false
""".trimIndent().trimStart())
}

@Test
fun testArrayAnnotationWithSingleElementRendersAsSequence() {
val generatorConfig = CodeGeneratorConfig(
basePackageName = "com/commercetools/importer",
outputFolder = Paths.get("build/gensrc")
)

val apiProvider = RamlApiProvider(Paths.get("src/test/resources/arrayannotation.raml"))

val generatorModule = RamlGeneratorModule(apiProvider, generatorConfig, RamldocBaseTypes)
val generatorComponent = RamlGeneratorComponent(generatorModule, RamldocModelModule)
generatorComponent.generateFiles()

val api = apiProvider.api
val t = api.getAnnotation("test").renderAnnotation()

// Guards against regressing renderAnnotation() (ArrayAnnotationType) into rendering a
// single-item array as a bare scalar - it must stay a YAML sequence, unlike the
// `default:` single-line rendering which intentionally unwraps single-item arrays.
Assertions.assertThat(t).isEqualTo("""
(test):
- "foo"
""".trimIndent().trimStart())
}

@Test
fun ramlRenderToRamlDoc() {
val generatorConfig = CodeGeneratorConfig(
Expand Down
8 changes: 8 additions & 0 deletions languages/ramldoc/src/test/resources/arrayannotation.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#%RAML 1.0
---
title: Array Annotation Example Test API
annotationTypes:
test:
type: string[]
baseUri: https://localhost
(test): [ foo ]
28 changes: 28 additions & 0 deletions languages/ramldoc/src/test/resources/arraydefault.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#%RAML 1.0
---
title: Array Default Example Test API
version: 1.0
baseUri: http://example.com/api
types:
foo:
properties:
foo:
type: object
default: { test: 1 }
bar:
type: string[]
default: [ "foo", "bar" ]
foobaz:
type: string[]
default: [ "foo" ]
fooz:
type: string[]
default:
- "foo"
baz:
type: string
default: "baz"
foobar:
type: object
default:
test: 1
Loading