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

About validating default: {} for type: object #673

Open
Huang-Wei opened this issue Nov 18, 2022 · 1 comment
Open

About validating default: {} for type: object #673

Huang-Wei opened this issue Nov 18, 2022 · 1 comment

Comments

@Huang-Wei
Copy link

Huang-Wei commented Nov 18, 2022

I noticed with the introduction of #610, the following schema will fail (but it was not before #610):

openapi: 3.0.0
info:
  title: "Hello World REST APIs"
  version: "1.0"
paths: {}
components:
  schemas:
    SomeSchema:
      type: object
      default: {}
      required:
      - field
      properties:
        field:
          type: string
          default: `foo`

According to my limited knowledge on OpenAPI v3, this yaml is valid, so could you help confirm if it's a regression?

BTW: removing default: {} can make kin validator happy, but I don't think that's mandatory in OpenAPI spec's POV.

@Huang-Wei
Copy link
Author

Unit test to reproduce this issue using the latest master code:

func TestIssue673(t *testing.T) {
	specf := func(dflt string) string {
		return `
openapi: 3.0.0
info:
  title: "Hello World REST APIs"
  version: "1.0"
paths: {}
components:
  schemas:
    SomeSchema:
      type: object
      default: {}
      required:
      - field
      properties:
        field:
          type: string
          default: ` + dflt + `
`
	}

	for _, testcase := range []struct {
		dflt, err string
	}{
		{
			dflt: `"foo"`,
			err:  "",
		},
		{
			dflt: `""`,
			err:  "",
		},
		{
			dflt: `1`,
			err:  "invalid components: invalid schema default: Field must be set to string or not be present",
		},
	} {
		t.Run(testcase.dflt, func(t *testing.T) {
			spec := specf(testcase.dflt)

			sl := NewLoader()

			doc, err := sl.LoadFromData([]byte(spec))
			require.NoError(t, err)

			err = doc.Validate(sl.Context)
			if testcase.err == "" {
				require.NoError(t, err)
			} else {
				require.Error(t, err, testcase.err)
			}
		})
	}
}

Output:

--- FAIL: TestIssue673 (0.00s)
    --- FAIL: TestIssue673/"foo" (0.00s)
        issue136_test.go:107: 
            	Error Trace:	/Users/weih/go/src/github.com/Huang-Wei/kin-openapi/openapi3/issue136_test.go:107
            	Error:      	Received unexpected error:
            	            	invalid components: schema "SomeSchema": invalid default: Error at "/field": property "field" is missing
            	            	Schema:
            	            	  {
            	            	    "default": {},
            	            	    "properties": {
            	            	      "field": {
            	            	        "default": "foo",
            	            	        "type": "string"
            	            	      }
            	            	    },
            	            	    "required": [
            	            	      "field"
            	            	    ],
            	            	    "type": "object"
            	            	  }
            	            	
            	            	Value:
            	            	  {}
            	Test:       	TestIssue673/"foo"
    --- FAIL: TestIssue673/"" (0.00s)
        issue136_test.go:107: 
            	Error Trace:	/Users/weih/go/src/github.com/Huang-Wei/kin-openapi/openapi3/issue136_test.go:107
            	Error:      	Received unexpected error:
            	            	invalid components: schema "SomeSchema": invalid default: Error at "/field": property "field" is missing
            	            	Schema:
            	            	  {
            	            	    "default": {},
            	            	    "properties": {
            	            	      "field": {
            	            	        "default": "",
            	            	        "type": "string"
            	            	      }
            	            	    },
            	            	    "required": [
            	            	      "field"
            	            	    ],
            	            	    "type": "object"
            	            	  }
            	            	
            	            	Value:
            	            	  {}
            	Test:       	TestIssue673/""
FAIL
FAIL	github.com/getkin/kin-openapi/openapi3	0.125s
FAIL

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

No branches or pull requests

1 participant