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

Servers: Variables do not support non-enums #1272

Closed
atombender opened this issue Jun 29, 2024 · 0 comments · Fixed by #1273
Closed

Servers: Variables do not support non-enums #1272

atombender opened this issue Jun 29, 2024 · 0 comments · Fixed by #1273
Labels
bug Something isn't working

Comments

@atombender
Copy link
Contributor

Given a server list like this:

servers:
- url: https://{hostPort}/v{version}
  x-ogen-server-name: development
  variables:
    version:
      description: API version
      default: UNKNOWN  # Ogen requires a dummy default here
    hostPort:
      description: Host and port
      default: UNKNOWN  # Ogen requires a dummy default here

This will generate the following validation code:

func (s DevelopmentServer) Build() (string, error) {
	zeroOr := func(s string, def string) string {
		if s == "" {
			return def
		}
		return s
	}
	s.HostPort = zeroOr(s.HostPort, "UNKNOWN")
	// Validate "hostPort"
	switch s.HostPort {
	default:
		return "", errors.Errorf("param %q: unexpected value %q", "hostPort", s.HostPort)
	}
	...

This means it's not possible to create structs with arbitrary data:

srv := DevelopmentServer{
	HostPort: "localhost:8080",
	Version: "2024-06-29",
}

as this will fail with:

param "hostPort": unexpected value "localhsot:8080":

This is in violation of the OpenAPI spec, which says the enum is optional (emphasis mine):

An enumeration of string values to be used if the substitution options are from a limited set

The validation template should just skip non-set values, e.g.:

	{{- if $p.Spec.Enum }}
	// Validate {{ quote $p.Spec.Name }}
	switch s.{{ $p.Name }} {
	{{- range $e := $p.Spec.Enum }}
	case {{ quote $e }}:
	{{- end }}
	default:
		return "", errors.Errorf("param %q: unexpected value %q", {{ quote $p.Spec.Name }}, s.{{ $p.Name }})
	}
	{{- end }}
@atombender atombender added the bug Something isn't working label Jun 29, 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

Successfully merging a pull request may close this issue.

1 participant