Skip to content

[REQ] [Go] Set Default Values for Required Variables where only one value is possible. #19107

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

Closed
prmedipa opened this issue Jul 9, 2024 · 0 comments · Fixed by #19232
Closed

Comments

@prmedipa
Copy link

prmedipa commented Jul 9, 2024

Is your feature request related to a problem? Please describe.

Go does not support adding default values for required variables when only one value is possible.

Describe the solution you'd like

The proposed solution is to add a configuration variable: initRequiredVars.

When initRequiredVars is set to true, and the response JSON lacks values for the required variables or the user hasn't provided values for the required variables, the system should automatically assign default values to these variables if only a single value is possible. If multiple values are possible for the required variables, default values should not be assigned.

For Example, consider the following snippet from the specification file:

schemas:
    Dog:
        - type: object
        -  required:
            - className
          properties:
            className:
              enum:
                - 'Dog'
              x-enum-as-string: true
              default: 'Dog'
              type: string
            breed:
              type: string
            legs:
               $ref: '#/components/schemas/Legs'
    Legs:
        type: object
        required:
        - legs
        properties:
            legs:
              enum:
              - '2'
              - '4'
              default: '4'
              x-enum-as-string: true
            name:
               type: string

        run:
           type: boolean
           default: True
           readOnly: true

As we can see in the above case Dog class contain required property className. Now if we will not specify the className in Dog class as payload and when the payload will be validated will throw an error.
Error:

missing 1 required positional argument: 'class_name'

Code:

package main

import (
	"encoding/json"
	"fmt"
)

// Define the Legs struct
type Legs struct {
	Legs string `json:"legs"`
	Name string `json:"name,omitempty"`
}

// Define the Dog struct
type Dog struct {
	ClassName string `json:"className"`
	Breed     string `json:"breed"`
	Legs      Legs   `json:"legs"`
	Color     string `json:"color"`
	Run       bool   `json:"run"`
}

func main() {
	legs := Legs{
		Legs: "4",
	}

	dogInstance := Dog{
		Breed:     "Bulldog",
		Legs:      legs,
		Color:     "Black",
		Run:       true,
	}

	// Print the dog instance directly
	dogJson, _ := json.Marshal(dogInstance)
	fmt.Println(string(dogJson))
}

Describe alternatives you've considered

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment