swagger-api / swagger-api/swagger-codegen

Swagger Codegen will generate clients from invalid specs

Open
#6,972 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

swagger-codegen generate will generate a client on specs that swagger-codegen validate will raise an uncaught exception for. I noticed this on a version of the 1.6 Kubernetes spec used to generate the Python client, but as of Kubernetes 1.8 the unedited spec is not valid but can still be used to generate a client. Other tools that only operate on valid specs break on the Kubernetes 1.8 spec.

Swagger-codegen version

I tested this with 2.2.3.

Swagger declaration file content or url

The Kubernetes 1.8 spec (beware, it's large). A sample of the output looks like:

Validating spec file (openapi_kubernetes_1.8.json)
attribute definitions.io.k8s.kubernetes.pkg.api.v1.PersistentVolume.description is unexpected
attribute definitions.io.k8s.kubernetes.pkg.apis.settings.v1alpha1.PodPreset.description is unexpected
attribute definitions.io.k8s.kubernetes.pkg.apis.authentication.v1.TokenReview.description is unexpected
attribute definitions.io.k8s.kubernetes.pkg.api.v1.NodeSelectorRequirement.description is unexpected
attribute definitions.io.k8s.kubernetes.pkg.api.v1.VolumeMount.description is unexpected
attribute definitions.io.k8s.kubernetes.pkg.api.v1.StorageOSPersistentVolumeSource.description is unexpected
...
Exception in thread "main" io.swagger.codegen.cmd.ValidateException
	at io.swagger.codegen.cmd.Validate.run(Validate.java:34)
	at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)

The invalid definitions are found at the end of the spec file.

Command line used for generation
swagger-codegen validate -i openapi_kubernetes_1.8.json
swagger-codegen generate -i openapi_kubernetes_1.8.json -o test_generated_client_python -l java

2.2.3 installed with Homebrew on Mac OS X 10.12.6. It will generate clients for both Java and Python, I didn't check other languages.

Steps to reproduce

Download https://raw.githubusercontent.com/kubernetes/kubernetes/release-1.8/api/openapi-spec/swagger.json and run swagger-codegen with validate and generate.

Suggest a fix/enhancement

Running the validation code before generating a client would be sufficient to catch invalid specs.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with the Kubernetes 1.8 spec and the listed validate and generate commands. Start at io.swagger.codegen.cmd.Validate.run, then inspect SwaggerCodegen.main and the generation entry point to understand where validation is handled. Done means generation no longer proceeds for a spec that validation rejects, with behavior verified against this reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.