Skip to content

Commit

Permalink
Merge pull request #220 from RealityAnomaly/fix/use-define-for-class-…
Browse files Browse the repository at this point in the history
…fields

Set model-defined props outside constructor to ensure useDefineForClassFields compatibility
  • Loading branch information
tommy351 authored Dec 8, 2024
2 parents 565b3de + de414dd commit e0ff682
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
56 changes: 56 additions & 0 deletions .changeset/mighty-pets-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
"@kubernetes-models/base": patch
"@kubernetes-models/crd-generate": patch
"@kubernetes-models/openapi-generate": patch
"@kubernetes-models/apimachinery": patch
"kubernetes-models": patch
"@kubernetes-models/argo-cd": patch
"@kubernetes-models/argo-rollouts": patch
"@kubernetes-models/argo-workflows": patch
"@kubernetes-models/autoscaler": patch
"@kubernetes-models/cert-manager": patch
"@kubernetes-models/cilium": patch
"@kubernetes-models/cloudnative-pg": patch
"@kubernetes-models/contour": patch
"@kubernetes-models/elastic-cloud": patch
"@kubernetes-models/envoy-gateway": patch
"@kubernetes-models/external-secrets": patch
"@kubernetes-models/fission": patch
"@kubernetes-models/flagger": patch
"@kubernetes-models/flux-cd": patch
"@kubernetes-models/gateway-api": patch
"@kubernetes-models/gke": patch
"@kubernetes-models/grafana-agent-operator": patch
"@kubernetes-models/grafana-operator": patch
"@kubernetes-models/hierarchical-namespaces": patch
"@kubernetes-models/istio": patch
"@kubernetes-models/jaeger-operator": patch
"@kubernetes-models/k8ssandra-operator": patch
"@kubernetes-models/kapp-controller": patch
"@kubernetes-models/karpenter": patch
"@kubernetes-models/keda": patch
"@kubernetes-models/knative": patch
"@kubernetes-models/kubedb": patch
"@kubernetes-models/kyverno": patch
"@kubernetes-models/linkerd": patch
"@kubernetes-models/longhorn": patch
"@kubernetes-models/nats": patch
"@kubernetes-models/pipelines-as-code": patch
"@kubernetes-models/postgres-operator": patch
"@kubernetes-models/prometheus-operator": patch
"@kubernetes-models/rabbitmq-cluster-operator": patch
"@kubernetes-models/rabbitmq-messaging-topology-operator": patch
"@kubernetes-models/redis-operator": patch
"@kubernetes-models/sealed-secrets": patch
"@kubernetes-models/seldon-core-operator": patch
"@kubernetes-models/shipwright": patch
"@kubernetes-models/smi": patch
"@kubernetes-models/spicedb": patch
"@kubernetes-models/spiffe": patch
"@kubernetes-models/thanos-operator": patch
"@kubernetes-models/tidb-operator": patch
"@kubernetes-models/traefik": patch
"@kubernetes-models/victoria-metrics-operator": patch
---

Set model-defined props outside constructor to ensure `useDefineForClassFields` compatibility.
6 changes: 6 additions & 0 deletions core/base/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export class Model<T> {
}
}

protected setDefinedProps(data?: ModelData<T>): any {
if (data) {
setDefinedProps(data, this);
}
}

public toJSON(): any {
const result = {};

Expand Down
8 changes: 0 additions & 8 deletions first-party/kubernetes-models/__tests__/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ describe("Deployment apps/v1", () => {
expect(deployment.metadata).toEqual({ name: "test" });
});

it("should not set sepc", () => {
expect(deployment).not.toHaveProperty("spec");
});

it("toJSON", () => {
expect(deployment.toJSON()).toEqual({
apiVersion: "apps/v1",
Expand Down Expand Up @@ -67,10 +63,6 @@ describe("Deployment apps/v1beta1", () => {
expect(deployment.metadata).toEqual({ name: "test" });
});

it("should not set sepc", () => {
expect(deployment).not.toHaveProperty("spec");
});

it("toJSON", () => {
expect(deployment.toJSON()).toEqual({
apiVersion: "extensions/v1beta1",
Expand Down
4 changes: 0 additions & 4 deletions third-party/gke/__tests__/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ describe("BackendConfig", () => {
expect(config.metadata).toEqual({ name: "test" });
});

it("should not set sepc", () => {
expect(config).not.toHaveProperty("spec");
});

it("toJSON", () => {
expect(config.toJSON()).toEqual({
apiVersion: "cloud.google.com/v1beta1",
Expand Down
4 changes: 3 additions & 1 deletion utils/crd-generate/src/generators/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ static kind: ${interfaceName}["kind"] = ${JSON.stringify(gvk.kind)};
static is = createTypeMetaGuard<${interfaceName}>(${className});
constructor(data?: ModelData<${interfaceName}>) {
super({
super();
this.setDefinedProps({
apiVersion: ${className}.apiVersion,
kind: ${className}.kind,
...data
Expand Down
22 changes: 16 additions & 6 deletions utils/openapi-generate/src/generators/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ export default function ({
}
});

if (gvk) {
imports.push({
name: "ModelData",
path: "@kubernetes-models/base"
});
imports.push({
name: "ModelData",
path: "@kubernetes-models/base"
});

if (gvk) {
imports.push({
name: "TypeMeta",
path: "@kubernetes-models/base"
Expand All @@ -141,12 +141,22 @@ static kind: ${shortInterfaceName}["kind"] = "${gvk.kind}";
static is = createTypeMetaGuard<${shortInterfaceName}>(${shortClassName});
constructor(data?: ModelData<${shortInterfaceName}>) {
super({
super();
this.setDefinedProps({
apiVersion: ${shortClassName}.apiVersion,
kind: ${shortClassName}.kind,
...data
} as ${shortInterfaceName});
}
}`;
} else {
classContent = `${trimSuffix(classContent, "}")}
constructor(data?: ModelData<${shortInterfaceName}>) {
super();
this.setDefinedProps(data);
}
}`;
}

Expand Down

0 comments on commit e0ff682

Please sign in to comment.