Skip to content

Commit

Permalink
fix issue where arrays/maps with no defaults were still getting them
Browse files Browse the repository at this point in the history
  • Loading branch information
rvowles committed Oct 14, 2023
1 parent ecab68f commit 85c32bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
29 changes: 16 additions & 13 deletions src/main/kotlin/com/bluetrainsoftware/openapi/DartV3ApiGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class DartV3ApiGenerator : DartClientCodegen() {
// this requires knowledge of inheritance
correctInternalsOfModels(allModels)

checkForOptionalArrays(allModels)
filterNullableArraysByNonInheritedProperties(allModels)

return originalModels
}
Expand All @@ -436,7 +436,7 @@ class DartV3ApiGenerator : DartClientCodegen() {
cm.vendorExtensions["dartClassName"] = StringUtils.camelize(cm.getClassname())

if (cm.vars != null) {
val optionalArrayOrMaps = mutableListOf<CodegenProperty>()
val arraysWithDefaults = mutableListOf<CodegenProperty>()

cm.vars.forEach { cp: CodegenProperty ->
var correctingSettings: CodegenProperty? = cp
Expand Down Expand Up @@ -465,14 +465,14 @@ class DartV3ApiGenerator : DartClientCodegen() {
correctingSettings = correctingSettings.items
}

if ((cp.isArray || cp.isMap) && !cp.required && !cp.isInherited) {
optionalArrayOrMaps.add(cp)
if ((cp.isArray || cp.isMap) && cp.isNullable && !cp.isInherited && cp.defaultValue != null) {
arraysWithDefaults.add(cp)
}
}

if (optionalArrayOrMaps.isNotEmpty()) {
cm.vendorExtensions["x-opt-arrays"] = optionalArrayOrMaps
cm.vendorExtensions["x-has-opt-arrays"] = "true"
if (arraysWithDefaults.isNotEmpty()) {
cm.vendorExtensions["x-nullable-arrays-with-defaults"] = arraysWithDefaults
cm.vendorExtensions["x-has-nullable-arrays-with-defaults"] = "true"
}
}
}
Expand Down Expand Up @@ -551,22 +551,25 @@ class DartV3ApiGenerator : DartClientCodegen() {
}
}

private fun checkForOptionalArrays(models: Map<String, CodegenModel>) {
/**
* This removes any inherited properties from the nullable arrays
*/
private fun filterNullableArraysByNonInheritedProperties(models: Map<String, CodegenModel>) {
for (cm in models.values) {
val ownVars = cm.vars.filter { v -> !v.isInherited }

cm.vendorExtensions["x-dart-ownVars"] = ownVars
cm.vendorExtensions["x-dart-hasOwnVars"] = ownVars.isNotEmpty()

if (cm.vendorExtensions.containsKey("x-has-opt-arrays")) {
val optArrays = (cm.vendorExtensions["x-opt-arrays"] as List<CodegenProperty>)
if (cm.vendorExtensions.containsKey("x-has-nullable-arrays-with-defaults")) {
val optArrays = (cm.vendorExtensions["x-nullable-arrays-with-defaults"] as List<CodegenProperty>)
.filter { cp -> ownVars.find { ov -> cp.name == ov.name } != null }
.toList()
if (optArrays.isEmpty()) { // none left
cm.vendorExtensions.remove("x-has-opt-arrays")
cm.vendorExtensions.remove("x-opt-arrays")
cm.vendorExtensions.remove("x-has-nullable-arrays-with-defaults")
cm.vendorExtensions.remove("x-nullable-arrays-with-defaults")
} else {
cm.vendorExtensions["x-opt-arrays"] = optArrays
cm.vendorExtensions["x-nullable-arrays-with-defaults"] = optArrays
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/dart2-v3template/class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class {{{classname}}}{{#parent}} extends {{parent}}{{/parent}} {
{{^-last}}, {{/-last}}
{{/vars}} }{{/hasVars}}
)
{{#vendorExtensions.x-has-opt-arrays}}:{{/vendorExtensions.x-has-opt-arrays}}
{{^vendorExtensions.x-has-opt-arrays}}{{#parent}}:{{/parent}}{{/vendorExtensions.x-has-opt-arrays}}
{{#vendorExtensions.x-opt-arrays}}this.{{{name}}} = {{{name}}} ?? {{#isArray}}[]{{/isArray}}{{#isMap}}{}{{/isMap}}{{^-last}}, {{/-last}}{{/vendorExtensions.x-opt-arrays}}
{{#parent}}{{#vendorExtensions.x-has-opt-arrays}},{{/vendorExtensions.x-has-opt-arrays}} super({{#vars}}{{#isInherited}}{{name}}: {{name}}{{#defaultValue}} ?? {{{defaultValue}}}{{/defaultValue}},{{/isInherited}}{{/vars}}) {{/parent}}
{{#vendorExtensions.x-has-nullable-arrays-with-defaults}}:{{/vendorExtensions.x-has-nullable-arrays-with-defaults}}
{{^vendorExtensions.x-has-nullable-arrays-with-defaults}}{{#parent}}:{{/parent}}{{/vendorExtensions.x-has-nullable-arrays-with-defaults}}
{{#vendorExtensions.x-nullable-arrays-with-defaults}}{{#defaultValue}}this.{{{name}}} = {{{name}}} ?? {{{defaultValue}}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/vendorExtensions.x-nullable-arrays-with-defaults}}
{{#parent}}{{#vendorExtensions.x-has-nullable-arrays-with-defaults}},{{/vendorExtensions.x-has-nullable-arrays-with-defaults}} super({{#vars}}{{#isInherited}}{{name}}: {{name}}{{#defaultValue}} ?? {{{defaultValue}}}{{/defaultValue}},{{/isInherited}}{{/vars}}) {{/parent}}
;

@override
Expand Down

0 comments on commit 85c32bb

Please sign in to comment.