swagger-api / swagger-api/swagger-codegen

In client code generation for typed objects, handling one response model for success and one for errors

Open
#5,664 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Scala General: Question
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

(There are similar issues but nothing exactly the same as this I believe? eg #3371 and #4398 - apologies if I missed an open item that this duplicates)

I am generating clients in java and scala from a Swagger JSON object (itself generated from swagger-spray). Pretty much all my endpoints have a single model for 2xx responses, and a single model for 4xx/5xx responses, eg:

        "responses": {
          "202": {
            "description": "The shutdown command was issued successfully, use the \"GET\" command on the /{cluster_id} resource to monitor progress",
            "schema": {
              "$ref": "#/definitions/EmptyResponse"
            }
          },
          "404": {
            "description": "The cluster specified by {cluster_id} cannot be found (code: 'clusters.cluster_not_found')",
            "schema": {
              "$ref": "#/definitions/BasicFailedReply"
            }
          },

when generating a client eg in scala, the endpoint returns Option[Any] (and in java Object instead of Any) pointing to a Map instead of (eg in the above example) EmptyResponse. If I only add a single response class then it correctly assigns the class to the endpoint return value.

So it seems that it treats >1 response class differently (reasonably!) but doesn't take into account that all 4xx/5xx responses are captured as ApiExceptions, so can be ignored as far as the return type of the endpoint is concerned?

Glancing over the code (and in the short term thinking selfishly about how i can quickly generate a better typed client for my own purposes!), could it be as simple as discarding responses associated with errors here?

Swagger-codegen version

v2.2.2

Swagger declaration file content or url

(see JSON fragment above, can gist a standalone JSON file or our entire API if useful)

Command line used for generation

swagger-codegen generate -i api-coverage-tests/src/main/resources/apidocs.json -l scala -o api-coverage-tests/generated --additional-properties modelPropertyNaming=original --api-package org.elastic.cloud.apiclient --model-package org.elastic.cloud.apiclient.model

Steps to reproduce

(can provide a minimal reconstruction later if helpful)

Related issues
  • #3371
  • #4398
Suggest a Fix

Nothing concrete yet, happy to PR if there is a desirable solution

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

Start at DefaultCodegen.java around the linked response-handling code, using the JSON responses fragment and the Scala generation command as the reproduction context. Compare the generated Scala and Java return types when 2xx and 4xx/5xx models are both present, then verify that successful responses use EmptyResponse while error responses remain handled as ApiExceptions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, scala
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.