3

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?

fbnrbn
  • 381
  • 1
  • 4
  • 9
  • If I am understand it correct you are using enum with multiple arguments in API, Can you share how you are accepting this enum in your APIs? – Gaurav Apr 19 '22 at 05:54
  • as you are using `type: string` you will only get one string to use – Gaurav Apr 19 '22 at 05:56
  • It is accepted like a normal enum in the api. I just need the other values of the enum to call other apis with different forms of this value. So this enum makes it easier for me. It is used for salutations and some apis expect a short versions or an ID of the salutation. – fbnrbn Apr 19 '22 at 07:22
  • can you that code? I want to know how the instance of enum is getting passed. – Gaurav Apr 19 '22 at 12:00

3 Answers3

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

  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
limazh
  • 11
  • 3
1

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()
-1

parameters:

  • name: status in: query schema: type: string enum: [active, inactive, deleted]
CoreGyan
  • 1
  • 4
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 14 '22 at 21:06
  • Thanks for your comment! Unfortunately I don't fully understand your answer. Could you please give an example? – fbnrbn Apr 16 '22 at 10:12