Skip to content

fix: report an error when the fetched openapi schema is not JSON - #6263

Open
PragalvaXFREZ wants to merge 1 commit into
kubernetes-sigs:masterfrom
PragalvaXFREZ:fix-openapi-fetch-invalid-json
Open

fix: report an error when the fetched openapi schema is not JSON#6263
PragalvaXFREZ wants to merge 1 commit into
kubernetes-sigs:masterfrom
PragalvaXFREZ:fix-openapi-fetch-invalid-json

Conversation

@PragalvaXFREZ

Copy link
Copy Markdown

printSchema runs kubectl get --raw /openapi/v2 and unmarshals stdout. The json.Unmarshal error is discarded, not even assigned to _:

var jsonSchema map[string]interface{}
output := stdout.Bytes()
json.Unmarshal(output, &jsonSchema)
output, _ = json.MarshalIndent(jsonSchema, "", "  ")

When kubectl succeeds but the response is not JSON, for example an auth portal or corporate proxy returning HTML, jsonSchema is left nil, MarshalIndent renders it as null, and the command prints that and exits 0:

$ kustomize openapi fetch
null
$ echo $?
0

The function already guards the two neighbouring failures, kubectl exiting non-zero and empty stdout, and both reuse the errMsg advice. Only "kubectl succeeded but returned something that is not a schema" was unguarded, so a scripted fetch writes null to a file and nothing notices.

After the fix:

$ kustomize openapi fetch
Error: unable to parse the schema returned by kubectl: invalid character '<' looking for beginning of value
Error fetching schema from cluster.
Please make sure kubectl is installed, its context is set correctly, and your cluster is up.
Installation and setup instructions: https://kubernetes.io/docs/tasks/tools/install-kubectl/
$ echo $?
1

A valid schema still round-trips unchanged in both --format=json and --format=yaml. Reproduced with a stub kubectl on PATH.

@kubernetes-prow
kubernetes-prow Bot requested review from koba1t and sarab97 September 9, 2026 05:47
@kubernetes-prow kubernetes-prow Bot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 9, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

Details 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

@kubernetes-prow kubernetes-prow Bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 9, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @PragalvaXFREZ. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 9, 2026
printSchema runs kubectl get --raw /openapi/v2 and unmarshals stdout.
The json.Unmarshal error is discarded, not even assigned to _:

	var jsonSchema map[string]interface{}
	output := stdout.Bytes()
	json.Unmarshal(output, &jsonSchema)
	output, _ = json.MarshalIndent(jsonSchema, "", "  ")

When kubectl succeeds but the response is not JSON, for example an auth
portal or corporate proxy returning HTML, jsonSchema is left nil,
MarshalIndent renders it as null, and the command prints that and exits
0:

	$ kustomize openapi fetch
	null
	$ echo $?
	0

The function already guards the two neighbouring failures, kubectl
exiting non-zero and empty stdout, and both reuse the errMsg advice.
Only "kubectl succeeded but returned something that is not a schema"
was unguarded, so a scripted fetch writes null to a file and nothing
notices.

After the fix the same input reports the parse error alongside the
existing advice and exits 1. A valid schema still round-trips unchanged
in both --format=json and --format=yaml.

Signed-off-by: Pragalva Sapkota <sapkotapragalva@gmail.com>
@PragalvaXFREZ
PragalvaXFREZ force-pushed the fix-openapi-fetch-invalid-json branch from 0a9722c to 8433994 Compare September 9, 2026 05:47
@kubernetes-prow kubernetes-prow Bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 9, 2026
@koba1t

koba1t commented Sep 9, 2026

Copy link
Copy Markdown
Member

/ok-to-test

@kubernetes-prow kubernetes-prow Bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 9, 2026
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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants