ogen icon indicating copy to clipboard operation
ogen copied to clipboard

Enum strings `ON` `OFF` being translated to `true` `false` in generate code

Open aravindhp opened this issue 5 months ago • 0 comments

What version of ogen are you using?

$ go list -m github.com/ogen-go/ogen
github.com/ogen-go/ogen v1.4.1

Can this issue be reproduced with the latest version?

Yes

What did you do?

I have an OpenAPI schema as follows:

openapi: 3.0.3
info:
  title: Test
  description: Test
  version: 1.0.0
paths:
  /api/test/:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    status:
                      type: string
                      enum:
                        - ON
                        - OFF
                  required:
                    - status
      operationId: getTest
      description: Get test
      summary: Get test

My ogen config is as follows:

generator:
  ignore_not_implemented: ["all"]
  features:
    disable: 
    - 'paths/server'
    - 'webhooks/server'
    - 'ogen/otel'

When I run ogen to generate the code the following oas_schemas_gen.go gets generated:

// Code generated by ogen, DO NOT EDIT.

package test

import (
	"github.com/go-faster/errors"
)

type GetTestOKItem struct {
	Status GetTestOKItemStatus `json:"status"`
}

// GetStatus returns the value of Status.
func (s *GetTestOKItem) GetStatus() GetTestOKItemStatus {
	return s.Status
}

// SetStatus sets the value of Status.
func (s *GetTestOKItem) SetStatus(val GetTestOKItemStatus) {
	s.Status = val
}

type GetTestOKItemStatus string

const (
	GetTestOKItemStatusTrue  GetTestOKItemStatus = true
	GetTestOKItemStatusFalse GetTestOKItemStatus = false
)

// AllValues returns all GetTestOKItemStatus values.
func (GetTestOKItemStatus) AllValues() []GetTestOKItemStatus {
	return []GetTestOKItemStatus{
		GetTestOKItemStatusTrue,
		GetTestOKItemStatusFalse,
	}
}

// MarshalText implements encoding.TextMarshaler.
func (s GetTestOKItemStatus) MarshalText() ([]byte, error) {
	switch s {
	case GetTestOKItemStatusTrue:
		return []byte(s), nil
	case GetTestOKItemStatusFalse:
		return []byte(s), nil
	default:
		return nil, errors.Errorf("invalid value: %q", s)
	}
}

// UnmarshalText implements encoding.TextUnmarshaler.
func (s *GetTestOKItemStatus) UnmarshalText(data []byte) error {
	switch GetTestOKItemStatus(data) {
	case GetTestOKItemStatusTrue:
		*s = GetTestOKItemStatusTrue
		return nil
	case GetTestOKItemStatusFalse:
		*s = GetTestOKItemStatusFalse
		return nil
	default:
		return errors.Errorf("invalid value: %q", data)
	}
}

What did you expect to see?

const (
	GetTestOKItemStatusON  GetTestOKItemStatus = "ON"
	GetTestOKItemStatusOFF GetTestOKItemStatus = "OFF"
)

What did you see instead?

const (
	GetTestOKItemStatusTrue  GetTestOKItemStatus = true
	GetTestOKItemStatusFalse GetTestOKItemStatus = false
)

Note that the resulting code will not compile.

I suspect this has to do with YAML interpretation of ON and OFF to true and false. However the OpenAPI spec says:

type: boolean represents two values: true and false. Note that truthy and falsy values such as "true", "", 0 or null are not considered boolean values

aravindhp avatar Sep 18 '24 23:09 aravindhp