contiamo / openapi-generator-go

An opinionated OpenAPI v3 code generator for Go. Use this to generate API models and router scaffolding.
MIT License
107 stars 12 forks source link

Enum with type integer #97

Closed doublefint closed 1 year ago

doublefint commented 1 year ago

Hi, Contiamo! Input:

openapi: 3.0.0
info:
  title: 'Test'
  version: 0.0.1
paths: {}
components:
    schemas:
      Fibonacci:
        type: integer
        enum: [1,2,3,5,8,13]

Output:

// This file is auto-generated, DO NOT EDIT.
//
// Source:
//
//  Title: Test
//  Version: 0.0.1
package models

import (
    validation "github.com/go-ozzo/ozzo-validation/v4"
)

// Fibonacci is an enum.
type Fibonacci int32

// Validate implements basic validation for this model
func (m Fibonacci) Validate() error {
    return InKnownFibonacci.Validate(m)
}

var (
    Fibonacci1  Fibonacci = "1" //<- string for int
    Fibonacci13 Fibonacci = "13" //<-sorted as string
    Fibonacci2  Fibonacci = "2"
    Fibonacci3  Fibonacci = "3"
    Fibonacci5  Fibonacci = "5"
    Fibonacci8  Fibonacci = "8"

https://github.com/contiamo/openapi-generator-go/blob/master/pkg/generators/models/models.go#L774 https://github.com/contiamo/openapi-generator-go/blob/master/pkg/generators/models/models.go#L779

LucasRoesler commented 1 year ago

@doublefint perhaps you can provide a bit more context about what you want to have.

Obviously there is a bug here because of the type mismatch and we should fix thus, but i am wondering what the use case of integer enums is? If you have a list of 5 integers that are allowed, you probably can't use any of the natural properties of integers then.

For future posterity, can you help document why and make sure we don't forget the use case.

doublefint commented 1 year ago

@LucasRoesler Thank you very much for the quick response!

There are many areas of human activity in which well-known numbers are used. Right now I can give the following examples: In IT, powers of 2 are widely used - 1024, 2048, 4096. When estimating time costs for developing functionality, it is recommended to use the Fibonacci number sequence. In the production of building materials, you will have your own number series for sizes, depending on the parameters of your equipment - 1.5x, 2x, 3x. In trading goods, the quantity of goods in various packages.

Therefore, when developing software for one of these industries, you will need a way to limit the value for a numeric property by selecting from such a "well-known in this area" numerical series.

BTW, the bug is not only with the incorrect type, but also with the sorting of values - see comments in the generated code.

LucasRoesler commented 1 year ago

Sounds good.

I am on a break this week, so i can take a look and patch it next week (or this weekend)

LucasRoesler commented 1 year ago

@doublefint PR #98 should resolve this now

LucasRoesler commented 1 year ago

I didn't change anything with the sorting because it isn't clear why it would be an issue, the the order in which they are defined in the file doesn't change anything about how the types will work or how the validation will work. Please let me know if an actual Go error that can occur because they are sorted in a different order.