Skip to content

Add raw openapi schema to typescript client #279

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

schrodit
Copy link

@schrodit schrodit commented Apr 23, 2025

ref: kubernetes-client/javascript#2384

This PR adds the original raw openapi schema to the generated typescript models. This is required to include the Kubernetes specific extension x-kubernetes-group-version-kind that includes the group version and kind of the resource.

Only adding that field is not possible because the modelJson variable is a raw string that I was unable to correctly parse with mustache (the templating engine used by the openapi generator).

In order to use a dedicated template, the template was added to a new subfolder of the structure openapi/templates/$CLIENT_TYPE. That subfolder is automtically mounted to the generator container at /template if it is defined.
Doing it like makes it easy for other language to add templates in the future if required.

The newly generated modals look like this example of a Secret resource:

// ...

/**
* Secret holds secret data of a certain type. The total bytes of the values in the Data field must be less than MaxSecretSize bytes.
*/
export class V1Secret {
    /**
    * APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
    */
    'apiVersion'?: string;
    /**
    * Data contains the secret data. Each key must consist of alphanumeric characters, '-', '_' or '.'. The serialized form of the secret data is a base64 encoded string, representing the arbitrary (possibly non-string) data value here. Described in https://tools.ietf.org/html/rfc4648#section-4
    */
    'data'?: { [key: string]: string; };
    /**
    * Immutable, if set to true, ensures that data stored in the Secret cannot be updated (only object metadata can be modified). If not set to true, the field can be modified at any time. Defaulted to nil.
    */
    'immutable'?: boolean;
    /**
    * Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
    */
    'kind'?: string;
    'metadata'?: V1ObjectMeta;
    /**
    * stringData allows specifying non-binary secret data in string form. It is provided as a write-only input field for convenience. All keys and values are merged into the data field on write, overwriting any existing values. The stringData field is never output when reading from the API.
    */
    'stringData'?: { [key: string]: string; };
    /**
    * Used to facilitate programmatic handling of secret data. More info: https://kubernetes.io/docs/concepts/configuration/secret/#secret-types
    */
    'type'?: string;

    // ...

    public static OPENAPI_SCHEMA = {
  "type" : "object",
  "properties" : {
    "apiVersion" : {
      "type" : "string",
      "description" : "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources"
    },
    "data" : {
      "type" : "object",
      "additionalProperties" : {
        "pattern" : "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$",
        "type" : "string",
        "format" : "byte"
      },
      "description" : "Data contains the secret data. Each key must consist of alphanumeric characters, '-', '_' or '.'. The serialized form of the secret data is a base64 encoded string, representing the arbitrary (possibly non-string) data value here. Described in https://tools.ietf.org/html/rfc4648#section-4"
    },
    "immutable" : {
      "type" : "boolean",
      "description" : "Immutable, if set to true, ensures that data stored in the Secret cannot be updated (only object metadata can be modified). If not set to true, the field can be modified at any time. Defaulted to nil."
    },
    "kind" : {
      "type" : "string",
      "description" : "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds"
    },
    "metadata" : {
      "$ref" : "#/components/schemas/v1.ObjectMeta"
    },
    "stringData" : {
      "type" : "object",
      "additionalProperties" : {
        "type" : "string"
      },
      "description" : "stringData allows specifying non-binary secret data in string form. It is provided as a write-only input field for convenience. All keys and values are merged into the data field on write, overwriting any existing values. The stringData field is never output when reading from the API."
    },
    "type" : {
      "type" : "string",
      "description" : "Used to facilitate programmatic handling of secret data. More info: https://kubernetes.io/docs/concepts/configuration/secret/#secret-types"
    }
  },
  "description" : "Secret holds secret data of a certain type. The total bytes of the values in the Data field must be less than MaxSecretSize bytes.",
  "x-kubernetes-group-version-kind" : [ {
    "group" : "",
    "kind" : "Secret",
    "version" : "v1"
  } ]
}
}

the formatting of the OPENAPI_SCHEMA is a bit broken because indentation is not possible via mustache.
An alternative would be to have the schema in a variable for readability. But not sure if this is worth adding on a anyway generated file.
e.g.

const schema = {
    foo: 1,
    bar: 2,
}

export class V1Secret {
    public static OPENAPI_SCHEMA = schema;
}

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: schrodit
Once this PR has been reviewed and has the lgtm label, please assign yliaog for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 23, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @schrodit!

It looks like this is your first PR to kubernetes-client/gen 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/gen has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 23, 2025
@schrodit
Copy link
Author

/assign brendandburns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants