OpenApi enum with multiple values
Asked Answered
U

3

8

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?

Underpart answered 14/4, 2022 at 13:53 Comment(4)
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?Noisemaker
as you are using type: string you will only get one string to useNoisemaker
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.Underpart
can you that code? I want to know how the instance of enum is getting passed.Noisemaker
H
3

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
Hoffert answered 21/4, 2022 at 7:17 Comment(0)
I
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()
Ilarrold answered 24/4, 2022 at 22:9 Comment(0)
S
-2

parameters:

  • name: status in: query schema: type: string enum: [active, inactive, deleted]
Snowplow answered 14/4, 2022 at 14:22 Comment(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.Stricklin
Thanks for your comment! Unfortunately I don't fully understand your answer. Could you please give an example?Underpart

© 2022 - 2024 — McMap. All rights reserved.