gnostic icon indicating copy to clipboard operation
gnostic copied to clipboard

OpenAPI 3 enums

Open nmnellis opened this issue 5 years ago • 1 comments

How does gnostic represent openapi enums so that a plugin (Ex. gnostic-generate-go) could leverage it to generate the correct go code? Looking at the surface.Model I dont see how we can determine that the object is infact an enum?

nmnellis avatar Jul 26 '19 13:07 nmnellis

How does gnostic represent openapi enums so that a plugin (Ex. gnostic-generate-go) could leverage it to generate the correct go code?

Given an OpenAPI schema like:

schema:
  enum:
    - 1337
    - abc

Inside of gnostic that is represented with the type: OpenAPIv3.Schema. So let's say you have a variable mySchema which represents the schema above, you can retrieve the enum values like that:

mySchema.Enum[0].Yaml // returns "1337" (type string)
mySchema.Enum[1].Yaml // returns "abc" (type string)

Looking at the surface.Model I dont see how we can determine that the object is infact an enum?

Yes, you are right. The surface model currently does not support enum values. For that to work we would need to update the Field message in surface.proto to something similar to this:

message Field {
  string name = 1;
  ...
  bool serialize = 9;
  repeated string enumValues = 10; // New value here
}

Then inside of the model_openapiv3.go we update the typeForSchema method: we would need to check whether schema.Enum is not nil and if so return the values and then set field.EnumValues accordingly.

Disclaimer: I am not a main contributor to gnostic but I have been working with gnostic extensively over last couple of weeks.

LorenzHW avatar Jul 26 '19 20:07 LorenzHW