Skip to content

Unhelpful error when response codes are integers #236

@SJrX

Description

@SJrX

I just spent about an hour trying to figure out what was wrong with the a random spec, I got a cryptic error:
SchemaError: got null, want object (at )

Here is a test case that shows the issue:

package main

import (
	"fmt"
	"testing"

	"github.com/pb33f/libopenapi"
	validator "github.com/pb33f/libopenapi-validator"
)

const specWithIntegerResponseCode = `openapi: 3.1.0
info:
  title: Test
  version: 1.0.0
paths:
  /test:
    get:
      responses:
        200:
          description: OK
`

func TestIntegerResponseCodeProducesUnhelpfulError(t *testing.T) {
	doc, err := libopenapi.NewDocument([]byte(specWithIntegerResponseCode))
	if err != nil {
		t.Fatalf("Failed to parse document: %v", err)
	}

	v, errs := validator.NewValidator(doc)
	if len(errs) > 0 {
		t.Fatalf("Failed to create validator: %v", errs)
	}

	valid, validationErrs := v.ValidateDocument()

	fmt.Println("=== Integer response code (200:) ===")
	fmt.Printf("Valid: %v\n", valid)
	if !valid {
		fmt.Println("Validation errors:")
		for i, e := range validationErrs {
			fmt.Printf("  [%d] Message: %s\n", i, e.Message)
			fmt.Printf("      Reason: %s\n", e.Reason)
			fmt.Printf("      HowToFix: %s\n", e.HowToFix)
			if len(e.SchemaValidationErrors) > 0 {
				for _, sve := range e.SchemaValidationErrors {
					fmt.Printf("      SchemaError: %s (at %s)\n", sve.Reason, sve.Location)
				}
			}
		}
	}

	// The spec is technically invalid (integer instead of string), so validation
	// should fail. But the error message should be helpful.
	if valid {
		t.Error("Expected validation to fail for integer response code")
	}

	// Check that we get the unhelpful error
	if len(validationErrs) > 0 && len(validationErrs[0].SchemaValidationErrors) > 0 {
		reason := validationErrs[0].SchemaValidationErrors[0].Reason
		if reason == "got null, want object" {
			t.Fatalf("BUG CONFIRMED: Error message is 'got null, want object' instead of something helpful like 'response code must be a string'")
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions