Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aravindhp opened this issue Sep 18, 2024 · 0 comments
Open

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

aravindhp opened this issue Sep 18, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@aravindhp
Copy link

aravindhp commented Sep 18, 2024

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 aravindhp added the bug Something isn't working label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant