Skip to content

Commit

Permalink
No Issue - Add a warn in the logs when elements have duplicated vars (#…
Browse files Browse the repository at this point in the history
…823)

* No Issue - Add a warn in the logs when elements have duplicated vars

Signed-off-by: Ricardo Zanini <[email protected]>

* Fix imports

Signed-off-by: Ricardo Zanini <[email protected]>

* Add the Override annotation

Signed-off-by: Ricardo Zanini <[email protected]>

---------

Signed-off-by: Ricardo Zanini <[email protected]>
  • Loading branch information
ricardozanini authored Oct 17, 2024
1 parent 5eb1b18 commit adf964d
Show file tree
Hide file tree
Showing 3 changed files with 3,502 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.languages.JavaClientCodegen;
Expand All @@ -18,6 +22,7 @@
import org.slf4j.LoggerFactory;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;

public class QuarkusJavaClientCodegen extends JavaClientCodegen {
Expand Down Expand Up @@ -130,6 +135,31 @@ public void postProcess() {

}

@Override
public CodegenModel fromModel(String name, Schema model) {
CodegenModel codegenModel = super.fromModel(name, model);
warnIfDuplicated(codegenModel);
return codegenModel;
}

private void warnIfDuplicated(CodegenModel m) {
Set<String> propertyNames = new TreeSet<>();
for (CodegenProperty element : m.allVars) {
if (element.deprecated) {
continue;
}

// We can have baseName as `my-type` and `myType`, that are duplicates
if (propertyNames.contains(element.name)) {
LOGGER.warn(
"Variable {} is duplicated in the OpenAPI spec file. Consider adding the 'deprecated' attribute to it or remove it from the file. Java class {} will fail to compile.",
element.baseName, m.classFilename);
} else {
propertyNames.add(element.name);
}
}
}

@Override
public String toEnumVarName(String value, String datatype) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@

public class OpenApiClientGeneratorWrapperTest {

@Test
void verifyFlink() throws URISyntaxException, FileNotFoundException {
OpenApiClientGeneratorWrapper generatorWrapper = createGeneratorWrapper("issue-flink.yaml");
final List<File> generatedFiles = generatorWrapper.generate("org.acme.flink");
assertNotNull(generatedFiles);
assertFalse(generatedFiles.isEmpty());

final Optional<File> duplicatedVars = generatedFiles.stream()
.filter(f -> f.getName().endsWith("AsynchronousOperationResultOperation.java")).findFirst();
assertThat(duplicatedVars).isPresent();

CompilationUnit compilationUnit = StaticJavaParser.parse(duplicatedVars.orElseThrow());
List<VariableDeclarator> vars = compilationUnit.findAll(VariableDeclarator.class);
assertThat(vars).isNotEmpty();

// This openApi file has a duplicated field
assertThat(vars.stream()
.filter(v -> "failureCause".equals(v.getNameAsString())).count()).isEqualTo(4);
}

@Test
void verifySuffixPrefix() throws URISyntaxException {
OpenApiClientGeneratorWrapper generatorWrapper = createGeneratorWrapper("suffix-prefix-openapi.json");
Expand Down
Loading

0 comments on commit adf964d

Please sign in to comment.