Skip to content

[feature]: Allow setting defaults for properties in the schema #1200

Closed

Description

@jmezach

Is your feature request related to a problem? Please describe.

As far as I can tell there is currently no way to set defaults for properties in the generated OpenAPI schema. This would be extremely useful to have. In our scenario we wanted to create an operator that is compatible with kstatus. For that to work well with brand new Kubernetes resources (ie. initial deploy) you need to make sure that there is an initial status object with at least observedGeneration: 0. If the resource has no status object anything that waits on that resource will immediately report that it is done because the operator itself will never have a chance to write the initial status object. Setting a default resolves this conoundrum.

Describe the solution you would like

Ideally I'd like to be able to set an [DefaultValue(<string>)] attribute on any field that is part of the schema so that this would be outputted to the generated CRD.

Additional Context

I can work around this for now by manually modifying the CRD YAML, but this causes it to drift from the initially generated version so another generate would effectively remove the workaround. This also doesn't work well with the Aspire Hosting integration since that applies the CRD definition on the fly, but only what is generated and there is no opportunity to modify it.

Activity

  1. added
    enhancement
    on Jul 15, 2026
  2. kimpenhaus commented on Jul 15, 2026

    @kimpenhaus
    Collaborator

    @jmezach yes that's absolutely true - I came across this just recently, too

    I've started a branch already - addressing this shortcome

    see this in particular 😃

  3. jmezach commented on Jul 15, 2026

    @jmezach
    ContributorAuthor

    @kimpenhaus Nice, good to hear that this is already being worked on. One thing to note is that while I was working on this I need to set the default on the whole status subresource. Setting only a default for status.observedGeneration to zero didn't fix it for me. But setting it on the status subresource would require setting a whole object. Not sure if that would be supported now.

  4. kimpenhaus commented on Jul 15, 2026

    @kimpenhaus
    Collaborator

    @jmezach I've opened pr #1201 - your requirement could be solved by something like:

      [KubernetesEntity(Group = "example.com", ApiVersion = "v1", Kind = "Example")]
      public partial class V1Example :
          CustomKubernetesEntity<V1Example.EntitySpec, V1Example.EntityStatus>
      {
          public sealed class EntitySpec;
    
          [DefaultValue("{}", Json = true)]
          public sealed class EntityStatus
          {
              [DefaultValue(0)]
              public long ObservedGeneration { get; set; }
          }
      }

    that should generate this:

      status:
        type: object
        default: {}
        properties:
          observedGeneration:
            type: integer
            format: int64
            default: 0

    if that fits your needs I can complete it and maybe you could test it with the create pre-version.

  5. added a commit that references this issue on Jul 16, 2026
    67aa3fd
  6. jmezach commented on Jul 17, 2026

    @jmezach
    ContributorAuthor

    @kimpenhaus I just tried the prerelease version. One thing that confused me a bit is that previously I used the System.ComponentModel.DataAnnotations set of attributes. That doesn't have a DefaultValue attribute so I had to add a using for KubeOps.Abstractions.Entities.Attributes but that namespace also includes a RequiredAttribute (for example) which is now an ambigious type. Simply removing the using System.ComponentModel.DataAnnotations line fixed that and it looks like the output hasn't really changed so I guess that worked. But I can imagine this being confusing for people.

  7. kimpenhaus commented on Jul 17, 2026

    @kimpenhaus
    Collaborator

    that's kinda funny - I wasn't even aware that DataAnnotations.Required would work 😆 guess I will add an info box to the docs.

    thanks for testing, feedback and pointing to this 🫶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions