Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenApi enum with multiple values

I 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?

like image 644
fbnrbn Avatar asked Jul 03 '26 16:07

fbnrbn


2 Answers

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):

  1. provide your own templates (see: https://openapi-generator.tech/docs/templating) or
  2. make usage of the ignore file https://openapi-generator.tech/docs/customization/#ignore-file-format and define/code your enum manually
like image 108
limazh Avatar answered Jul 05 '26 05:07

limazh


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()
like image 22
Java-Enthusiast Avatar answered Jul 05 '26 04:07

Java-Enthusiast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!