Skip to content

Commit

Permalink
Refactor some code to make it cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
david-marconis committed Sep 26, 2024
1 parent 452e95b commit 9c0a126
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3575,30 +3575,24 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
discriminator.getMappedModels().addAll(uniqueDescendants);

// check current schema, all parents and descendants to see if the discriminator property is an enum string
List<Schema> schemasToCheckForEnumDiscriminator = new ArrayList<>();
schemasToCheckForEnumDiscriminator.add(schema);
if (ModelUtils.isComposedSchema(schema)) {
ModelUtils.getAllParentsName(schema, openAPI.getComponents().getSchemas(), true).stream()
.map(n -> ModelUtils.getSchema(openAPI, n))
.forEach(schemasToCheckForEnumDiscriminator::add);
}
uniqueDescendants.stream().map(MappedModel::getModelName)
.map(n->ModelUtils.getSchema(openAPI, n))
.forEach(schemasToCheckForEnumDiscriminator::add);
for (Schema schemaToCheck : schemasToCheckForEnumDiscriminator) {
if (schemaToCheck == null) {
continue;
}
boolean hasDiscriminatorEnum = Optional.ofNullable(schemaToCheck.getProperties())
.map(p -> (Schema) p.get(discriminatorPropertyName))
.map(s -> ModelUtils.getReferencedSchema(openAPI, s))
.filter(s -> s instanceof StringSchema && s.getEnum() != null && !s.getEnum().isEmpty())
.isPresent();
if (hasDiscriminatorEnum) {
discriminator.setIsEnum(true);
break;
}
}
Stream<Schema> schemasToCheckForEnumDiscriminator = Stream.concat(
Stream.of(schema),
Stream.concat(
ModelUtils.isComposedSchema(schema)
? ModelUtils.getAllParentsName(schema, openAPI.getComponents().getSchemas(), true).stream()
: Stream.of(),
uniqueDescendants.stream().map(MappedModel::getModelName)
).flatMap(s -> Optional.ofNullable(ModelUtils.getSchema(openAPI, s)).stream())
);

boolean isEnum = schemasToCheckForEnumDiscriminator.anyMatch(s -> Optional
.ofNullable(s.getProperties())
.map(p -> (Schema) p.get(discriminatorPropertyName))
.map(s1 -> ModelUtils.getReferencedSchema(openAPI, s1))
.filter(s1 -> s1 instanceof StringSchema && s1.getEnum() != null && !s1.getEnum().isEmpty())
.isPresent()
);
discriminator.setIsEnum(isEnum);

return discriminator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,21 +440,22 @@ public CodegenModel fromModel(String name, Schema model) {

for (final CodegenProperty property : codegenModel.readWriteVars) {
CodegenDiscriminator discriminator = parentCodegenModel.discriminator;
if (property.defaultValue == null && discriminator != null && property.name.equals(discriminator.getPropertyName())) {
if (discriminator.getIsEnum()) {
String enumValue = name;
Map<String, String> mapping = Optional.ofNullable(discriminator.getMapping()).orElseGet(Collections::emptyMap);
for (Map.Entry<String, String> e : mapping.entrySet()) {
String schemaName = e.getValue().indexOf('/') < 0 ? e.getValue() : ModelUtils.getSimpleRef(e.getValue());
if (name.equals(schemaName)) {
enumValue = e.getKey();
break;
}
if (property.defaultValue != null || discriminator == null || !property.name.equals(discriminator.getPropertyName())) {
continue;
}
if (discriminator.getIsEnum()) {
String enumValue = name;
Map<String, String> mapping = Optional.ofNullable(discriminator.getMapping()).orElseGet(Collections::emptyMap);
for (Map.Entry<String, String> e : mapping.entrySet()) {
String schemaName = e.getValue().indexOf('/') < 0 ? e.getValue() : ModelUtils.getSimpleRef(e.getValue());
if (name.equals(schemaName)) {
enumValue = e.getKey();
break;
}
property.defaultValue = toEnumDefaultValue(property, enumValue);
} else {
property.defaultValue = "\"" + name + "\"";
}
property.defaultValue = toEnumDefaultValue(property, enumValue);
} else {
property.defaultValue = "\"" + name + "\"";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,8 @@ public static List<String> getAllParentsName(Schema composedSchema, Map<String,
return getAllParentsName(composedSchema, allSchemas, includeAncestors, new HashSet<>());
}

public static List<String> getAllParentsName(
// Use a set of seen names to avoid infinite recursion
private static List<String> getAllParentsName(
Schema composedSchema, Map<String, Schema> allSchemas, boolean includeAncestors, Set<String> seenNames) {
List<Schema> interfaces = getInterfaces(composedSchema);
List<String> names = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2287,15 +2287,16 @@ public void testAllOfWithSinglePrimitiveTypeRef() {
assertNull(files.get("pom.xml"));
}

@Test public void testEnumDiscriminatorDefaultValueIsNotString() throws IOException {
@Test
public void testEnumDiscriminatorDefaultValueIsNotString() {
final Path output = newTempFolder();
final OpenAPI openAPI = TestUtils.parseFlattenSpec(
"src/test/resources/3_0/enum_discriminator_inheritance.yaml");
"src/test/resources/3_0/enum_discriminator_inheritance.yaml");
JavaClientCodegen codegen = new JavaClientCodegen();
codegen.setOutputDir(output.toString());

Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));

List<String> entities = List.of(
"Cat",
Expand Down

0 comments on commit 9c0a126

Please sign in to comment.