Skip to content

fix: preserve CRD schema min* validation constraints#18

Draft
jtligon wants to merge 1 commit into
calfonso:mainfrom
jtligon:fix/crd-openapi-preserve-required-fields
Draft

fix: preserve CRD schema min* validation constraints#18
jtligon wants to merge 1 commit into
calfonso:mainfrom
jtligon:fix/crd-openapi-preserve-required-fields

Conversation

@jtligon
Copy link
Copy Markdown
Contributor

@jtligon jtligon commented Apr 29, 2026

Problem

CRD OpenAPI schemas were incorrectly stripping minimum validation constraints (, , , ) when set to 0.

This caused conformance test failures in the "CRD OpenAPI publishing" category where CRD schema definitions were missing validation fields in the endpoint after CRD creation or update.

Root Cause

In JSON Schema/OpenAPI, minLength: 0 has semantic meaning - it explicitly allows empty values. This is different from omitting minLength entirely.

The code was treating all zero-value constraints as "unset" and stripping them per Go's omitempty behavior. However, for minimum constraints, 0 is a valid and meaningful value.

Fix

  • Preserve minLength: 0, minItems: 0, minProperties: 0, minimum: 0 as they are semantically meaningful
  • Only strip obvious sentinel values (negative or very large numbers > 1000000)
  • Keep maximum constraints stripped when 0 (less common to explicitly set maxLength: 0)

Testing

  • API server compiles cleanly
  • Conformance tests should show improvement in CRD OpenAPI category

Kubernetes Reference

  • k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/validation
  • JSONSchemaProps allows minLength: 0 as a valid constraint
  • Go's omitempty applies to pointer types, but 0 is still a valid dereferenced value

Related Category

Part of fixing CRD OpenAPI publishing failures (~9 tests in Round 147).


🤖 Generated with Claude Code

CRD OpenAPI schemas were incorrectly stripping minimum validation
constraints (minLength, minItems, minProperties, minimum) when set to 0.

In JSON Schema, minLength: 0 has semantic meaning - it explicitly allows
empty values. This is different from omitting minLength entirely, which
means 'no constraint'.

This fix:
- Preserves minLength: 0, minItems: 0, etc. as they are meaningful
- Only strips obvious sentinel values (negative or very large numbers)
- Keeps maximum constraints stripped when 0 (less common use case)

This should fix CRD conformance tests that validate schema constraints
are properly reflected in /openapi/v2 endpoint.

Kubernetes reference:
- k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/validation
- JSONSchemaProps allows minLength: 0 as valid constraint

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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 this pull request may close these issues.

1 participant