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

[BUG] [Go-Server] Required validation too strict #20496

Open
5 of 6 tasks
Fatcat560 opened this issue Jan 17, 2025 · 0 comments
Open
5 of 6 tasks

[BUG] [Go-Server] Required validation too strict #20496

Fatcat560 opened this issue Jan 17, 2025 · 0 comments

Comments

@Fatcat560
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Hello! I'm currently trying to use the go-server generator, however I am running into an issue with the required field validation.
If a schema is present which has an integer field marked as required, it will not pass validation if the field is present but has a value of 0.

This causes a problem when a value of 0 is considered to be a valid input for schemas, as the generated code will reject such values.

openapi-generator version

Tested both with 7.10.0 and 7.11.0-SNAPSHOT.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Test - OpenAPI 3.0
  version: 1.0.0
servers:
  - url: https://localhost:8080/api/v1
paths:
  /config:
    put:
      summary: Update something
      operationId: update
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TestObject'
        required: true
      responses:
        '204':
          description: Successful operation
components:
  schemas:
   TestObject:
    type: object
    properties:
      n:
        type: integer
        minimum: 0
    required:
      - n
Generation Details
Steps to reproduce
  • Get the latest release (in my case version 7.10.0)
  • Run java -jar openapi-generator-cli.jar generate -g go-server -i openapi.yaml -o out --additional-properties=router=chi
  • cd ./out
  • go mod tidy && go mod run ./main.go

Expected results:
Sending a HTTP PUT request to http://localhost:8080/api/v1/config with a body of {"n" : 1} or a body of {"n" : 0} both cause a 501 Not Implemented response

Actual results:
Sending the request with n=1 results in the expected 501 status code, however the n=0 case causes a 422 Unprocessable Entity. The error message is: "required field 'n' is zero value."

Related issues/PRs
Suggest a fix

I believe the issue is in the generated code for the model:

// In model_test_object.go

// AssertTestObjectRequired checks if the required fields are not zero-ed
func AssertTestObjectRequired(obj TestObject) error {
	elements := map[string]interface{}{
		"n": obj.N,
	}
	for name, el := range elements {
		if isZero := IsZeroValue(el); isZero {
			return &RequiredError{Field: name}
		}
	}

	return nil
}

//In helper.go

// IsZeroValue checks if the val is the zero-ed value.
func IsZeroValue(val interface{}) bool {
	return val == nil || reflect.DeepEqual(val, reflect.Zero(reflect.TypeOf(val)).Interface())
}

I think the required validator should not check if the value is some zero of its type, but only check the existence of the key in the property.

As a workaround, the AssertTestObjectRequired function can be changed to only return nil, however this change must then be made "permanent" by adding the model to the ignore list, otherwise it will get overwritten again.

If someone knows the specifics on how to approach this issue I'd be happy to try and help :)

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

No branches or pull requests

1 participant