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

oneof schema error improvment #289

Closed
shaj13 opened this issue Jan 28, 2021 · 1 comment · Fixed by #292
Closed

oneof schema error improvment #289

shaj13 opened this issue Jan 28, 2021 · 1 comment · Fixed by #292

Comments

@shaj13
Copy link

shaj13 commented Jan 28, 2021

Context:
currently, the library when validating an input against a schema and when an error occurred it returns schema error contains scheme filed where the error happens or input is invalid, under some circumstance, this is still not a piece of useful information to exactly know what happened behind the scene.

Pain:
Inappropriate error message returned when input matches more than "oneof" scheme.

Goal:
return a different error to distinguish input match more than oneof schemas (conflict) from input does not match oneof schemas.

Got:
Error at "/address": Doesn't match schema "oneOf"

Expected:
Error at "/address": Conflict input match all oneof schemas"

Reproduce:

package main

import (
	"fmt"

	"github.com/getkin/kin-openapi/openapi3"
)

func main() {
	data := map[string]interface{}{
		"name":      "kin-openapi",
		"address": "127.0.0.1",
	}
	s, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData([]byte(api))
	if err != nil {
		panic(err)
	}
	err = s.Components.Schemas["Server"].Value.VisitJSON(data)
	fmt.Println(err)

	//
}

var api = 
`components: 
  schemas: 
    Server: 
      properties: 
        address: 
          oneOf: 
            - $ref: "#/components/schemas/ip-address"
            - $ref: "#/components/schemas/domain-name"
        name: 
          type: string
      type: object
    domain-name: 
      maxLength: 10
      minLength: 5
      pattern: "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)|\\."
      type: string
    ip-address: 
      pattern: "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"
      type: string
openapi: "3.0.1"
`

Note:

Suggested Solution:

var ErrOneOfConflict = errors.new("input match all oneof schemas")  

//  https://github.com/getkin/kin-openapi/blob/master/openapi3/schema.go#L850.
		if ok != 1 {
			if settings.failfast {
				return errSchema
			}
			 err := &SchemaError{
				Value:       value,
				Schema:      schema,
				SchemaField: "oneOf",
			}
                        if ok > 1 {
                           err.Origin = ErrOneOfConflict
                         }
                        return err 
		}
@shaj13
Copy link
Author

shaj13 commented Jan 28, 2021

PTAL @fenollp

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

Successfully merging a pull request may close this issue.

1 participant