Skip to content

panic on schema error #167

@SJrX

Description

@SJrX
2025/07/18 12:41:42 http: panic serving [::1]:42816: runtime error: invalid memory address or nil pointer dereference
goroutine 1153 [running]:
net/http.(*conn).serve.func1()
	/usr/lib/go/src/net/http/server.go:1947 +0x13f
panic({0xcf78a0?, 0x12e0fe0?})
	/usr/lib/go/src/runtime/panic.go:792 +0x136
github.com/santhosh-tekuri/jsonschema/v6.unevalFrom({0xd0d4a0, 0xc00012ad80}, 0x0, 0x0)
	.../go/pkg/mod/github.com/santhosh-tekuri/jsonschema/[email protected]/validator.go:922 +0x261
github.com/santhosh-tekuri/jsonschema/v6.(*Schema).validate(0x0, {0xd0d4a0, 0xc00012ad80}, 0x0, 0x0, 0x0, 0x0, 0x0)
        .../go/pkg/mod/github.com/santhosh-tekuri/jsonschema/[email protected]/validator.go:25 +0xc9
github.com/santhosh-tekuri/jsonschema/v6.(*Schema).Validate(0x0, {0xd0d4a0, 0xc00012ad80})
	.../go/pkg/mod/github.com/santhosh-tekuri/jsonschema/[email protected]/validator.go:16 +0x4a
github.com/pb33f/libopenapi-validator/responses.ValidateResponseSchema(0xc000d32780, 0xc000693170, 0xc000a56288, {0xc000ed6000, 0xaef6, 0xe000}, {0xc00042e000, 0x3ab6, 0x4000}, {0xc00138ddd8, ...})
	.../go/pkg/mod/github.com/pb33f/[email protected]/responses/validate_response.go:134 +0x1a38
github.com/pb33f/libopenapi-validator/responses.(*responseBodyValidator).checkResponseSchema(0xc000876108, 0xc000d32780, 0xc000693170, {0xc000b842f0, 0x10}, 0xc00084a060)
	.../go/pkg/mod/github.com/pb33f/[email protected]/responses/validate_body.go:189 +0xc77
github.com/pb33f/libopenapi-validator/responses.(*responseBodyValidator).ValidateResponseBodyWithPathItem(0xc000876108, 0xc000d32780, 0xc000693170, 0xc000134140, {0xc0002b93e0, 0x13})
	.../go/pkg/mod/github.com/pb33f/[email protected]/responses/validate_body.go:78 +0x551
github.com/pb33f/libopenapi-validator.(*validator).ValidateHttpResponse(0xc000502410, 0xc000d32780, 0xc000693170)
	.../go/pkg/mod/github.com/pb33f/[email protected]/validator.go:149 +0x289

While I think there is an issue in the underlying library (that I'll report separately) because the JSON Schema seems valid in other tools and the error that it reported didn't make sense to me.

However the underlying json schema library returned an error (here): https://github.com/pb33f/libopenapi-validator/blob/main/responses/validate_response.go#L134

// create a new jsonschema compiler and add in the rendered JSON schema.
	jsch, _ := helpers.NewCompiledSchema(helpers.ResponseBodyValidation, jsonSchema, options)

	// validate the object against the schema
	scErrs := jsch.Validate(decodedObj)

The NewCompiledSchema()

// NewCompiledSchema establishes a programmatic representation of a JSON Schema document that is used for validation.
func NewCompiledSchema(name string, jsonSchema []byte, o *config.ValidationOptions) (*jsonschema.Schema, error) {
	// Fake-Up a resource name for the schema
	resourceName := fmt.Sprintf("%s.json", name)

	// Establish a compiler with the desired configuration
	compiler := NewCompilerWithOptions(o)
	compiler.UseLoader(NewCompilerLoader())

	// Decode the JSON Schema into a JSON blob.
	decodedSchema, err := jsonschema.UnmarshalJSON(bytes.NewReader(jsonSchema))
	if err != nil {
		return nil, fmt.Errorf("failed to unmarshal JSON schema: %w", err)
	}

	// Give our schema to the compiler.
	if err = compiler.AddResource(resourceName, decodedSchema); err != nil {
		return nil, fmt.Errorf("failed to add resource to schema compiler: %w", err)
	}

	// Try to compile it.
	jsch, err := compiler.Compile(resourceName)
	if err != nil {
		return nil, fmt.Errorf("failed to compile JSON schema: %w", err)
	}

	// Done.
	return jsch, nil
}

In my case we exited in the third block, with an error, then got the panic later because it wasn't checked.

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