mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-06 18:45:23 +00:00
[Kotlin] Correct isInherited flag for Kotlin generators (#4254)
* Correct isInherited flag for Kotlin generators * Update Kotlin Client inheritance test to check variables
This commit is contained in:
parent
79d11d7129
commit
38185d8558
@ -33,7 +33,9 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.openapitools.codegen.utils.StringUtils.*;
|
||||
|
||||
@ -771,7 +773,22 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
public CodegenModel fromModel(String name, Schema schema) {
|
||||
CodegenModel m = super.fromModel(name, schema);
|
||||
m.optionalVars = m.optionalVars.stream().distinct().collect(Collectors.toList());
|
||||
m.allVars.stream().filter(p -> !m.vars.contains(p)).forEach(p -> p.isInherited = true);
|
||||
// Update allVars/requiredVars/optionalVars with isInherited
|
||||
// Each of these lists contains elements that are similar, but they are all cloned
|
||||
// via CodegenModel.removeAllDuplicatedProperty and therefore need to be updated
|
||||
// separately.
|
||||
// First find only the parent vars via baseName matching
|
||||
Map<String, CodegenProperty> allVarsMap = m.allVars.stream()
|
||||
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));
|
||||
allVarsMap.keySet()
|
||||
.removeAll(m.vars.stream().map(CodegenProperty::getBaseName).collect(Collectors.toSet()));
|
||||
// Update the allVars
|
||||
allVarsMap.values().forEach(p -> p.isInherited = true);
|
||||
// Update any other vars (requiredVars, optionalVars)
|
||||
Stream.of(m.requiredVars, m.optionalVars)
|
||||
.flatMap(List::stream)
|
||||
.filter(p -> allVarsMap.containsKey(p.baseName))
|
||||
.forEach(p -> p.isInherited = true);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
@ -617,18 +617,6 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
}
|
||||
}
|
||||
|
||||
// Can't figure out the logic in DefaultCodegen but optional vars are getting duplicated when there's
|
||||
// inheritance involved. Also, isInherited doesn't seem to be getting set properly ¯\_(ツ)_/¯
|
||||
@Override
|
||||
public CodegenModel fromModel(String name, Schema schema) {
|
||||
CodegenModel m = super.fromModel(name, schema);
|
||||
|
||||
m.optionalVars = m.optionalVars.stream().distinct().collect(Collectors.toList());
|
||||
m.allVars.stream().filter(p -> !m.vars.contains(p)).forEach(p -> p.isInherited = true);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the proper model name (capitalized).
|
||||
* In case the name belongs to the TypeSystem it won't be renamed.
|
||||
|
@ -1,12 +1,25 @@
|
||||
package org.openapitools.codegen.kotlin;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.media.ComposedSchema;
|
||||
import io.swagger.v3.oas.models.media.ObjectSchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.media.StringSchema;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.CodegenType;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.TestUtils;
|
||||
import org.openapitools.codegen.languages.AbstractKotlinCodegen;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.openapitools.codegen.CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.*;
|
||||
import static org.testng.Assert.*;
|
||||
@ -197,4 +210,45 @@ public class AbstractKotlinCodegenTest {
|
||||
codegen.processOpts();
|
||||
Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleInheritance() {
|
||||
Schema parent = new ObjectSchema()
|
||||
.addProperties("a", new StringSchema())
|
||||
.addProperties("b", new StringSchema())
|
||||
.addRequiredItem("a")
|
||||
.name("Parent");
|
||||
Schema child = new ComposedSchema()
|
||||
.addAllOfItem(new Schema().$ref("Parent"))
|
||||
.addAllOfItem(new ObjectSchema()
|
||||
.addProperties("c", new StringSchema())
|
||||
.addProperties("d", new StringSchema())
|
||||
.addRequiredItem("c"))
|
||||
.name("Child");
|
||||
OpenAPI openAPI = TestUtils.createOpenAPI();
|
||||
openAPI.getComponents().addSchemas(parent.getName(), parent);
|
||||
openAPI.getComponents().addSchemas(child.getName(), child);
|
||||
|
||||
final DefaultCodegen codegen = new P_AbstractKotlinCodegen();
|
||||
codegen.setOpenAPI(openAPI);
|
||||
|
||||
final CodegenModel pm = codegen
|
||||
.fromModel("Child", child);
|
||||
Map<String, CodegenProperty> allVarsMap = pm.allVars.stream()
|
||||
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));
|
||||
for (CodegenProperty p : pm.requiredVars) {
|
||||
Assert.assertEquals(allVarsMap.get(p.baseName).isInherited, p.isInherited);
|
||||
}
|
||||
Assert.assertEqualsNoOrder(
|
||||
pm.requiredVars.stream().map(CodegenProperty::getBaseName).toArray(),
|
||||
new String[] {"a", "c"}
|
||||
);
|
||||
for (CodegenProperty p : pm.optionalVars) {
|
||||
Assert.assertEquals(allVarsMap.get(p.baseName).isInherited, p.isInherited);
|
||||
}
|
||||
Assert.assertEqualsNoOrder(
|
||||
pm.optionalVars.stream().map(CodegenProperty::getBaseName).toArray(),
|
||||
new String[] {"b", "d"}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user