'OpenApi enum with multiple values
I am new to OpenApi and want to define my api with an api.yaml (OpenApi version 3.0.1). My problem is the generated enum just contains the name and not the values.
This is the enum in my code:
TEST1(1, "Test 1", "T1"),
TEST2(2, "Test 2", "T2"),
TEST3(3, "Test 3", "T2");
And this is the enum after generating it with OpenApi:
TEST1("TEST1"),
TEST2("TEST2"),
TEST3("TEST3");
The enum is automatically defined like this:
testenum:
type: string
description: desciption of the enum
enum:
- TEST1
- TEST2
- TEST3
How can I define the enum in my api.yaml to look like the first example?
Solution 1:[1]
That's probably not natively supported.
Check the template which is responsible for generating the code, e.g: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/enumOuterClass.mustache. Note, that's for spring. However you should easily be able to navigate to your desired framework.
So, you could (just some ideas):
- provide your own templates (see: https://openapi-generator.tech/docs/templating) or
- make usage of the ignore file https://openapi-generator.tech/docs/customization/#ignore-file-format and define/code your enum manually
Solution 2:[2]
This may not be in the direction of code that smells good, however, there is always the option of writing a program / build task that will edit the generated file after its generation, but before compilation, in the way that you need it to. This is likely to be an awful idea in terms of fundamentals, but while dirty, it is likely to work.
File f = new File("PATH/TO/ENUM/CLASS");
ArrayList<String> fileLines = Files.readAllLines(f.toPath());
ArrayList<String> outputList = new ArrayList<>();
for(String s : files){
if(s.contains( SOMETHING THAT YOU NEED TO CHANGE) ) {
outList.add(CHANGED VALUE);
}
else {
outList.add(s);
}
}
//However you prefer to, write to the file
//file.delete()
//file.createNewFile();
//outList.forEach(WRITE TO FILE);
//file.save()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | limazh |
Solution 2 | Java-Enthusiast |