swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.04k stars 6.03k forks source link

swagger spec with 2 tags generates 2 api files. #5371

Open dnome2 opened 7 years ago

dnome2 commented 7 years ago
Description

I've two tags defined in the swagger spec .yaml file for each api, so when i run swagger-codegen generate, there are 2 corresponding *_api.py in apis directory. And the only difference between those two file is the class name. Shouldn't it generate only one api file, may be pick just the first tag from the list of tags??

Or is there an option to control the resultant *_api.py file name through the cli or config.json ??

currently for below .yaml it generates asset_api.py AND assetsecond_api.py

Swagger-codegen version

2.0

Swagger declaration file content or url

swagger: "2.0"
info:
  description: "this is the aurora api"
  version: "1.0.0"
  title: "aurora api"
host: "aurora"
basePath: "/api/v1"
schemes:
- "https"
consumes:
- "application/json"
produces:
- "application/json"
paths:
  /asset/DeviceClaims:
    post:
      tags:
        - asset
        - assetsecond
      summary: Create or update a assetDeviceClaim
      parameters:
        - name: body
          in: body
          description: assetDeviceClaim to add
          required: true
          schema:
            $ref: '#/definitions/assetDeviceClaim'
      responses:
        201:
          description: "Null response"
        default:
          description: "unexpected error"
          schema:
            $ref: "#/definitions/Error"

-->

Command line used for generation

swagger-codegen generate -i restapi.yaml -DapiTests=false,apiDocs=false,modelTests=false,modelDocs=false -c config.json -l python -o py-client-new

{ "packageName" : "aurora", "packageVersion" : "1.0.0" }

Steps to reproduce

as mentioned above, let me know if missed anything.

appreciate your help.

wing328 commented 7 years ago

@dnome2 which version of Swagger Codegen are you using? We just release 2.2.2 a month ago: https://github.com/swagger-api/swagger-codegen/releases/tag/v2.2.2

The tags is used for grouping endpoints (operations) into classes and so far we've not seen a valid use case in which a single operation can belong to 2 classes.

dnome2 commented 7 years ago

@wing328 i did a brew install locally on my mac, how do i find what version do i have?

$ swagger-codegen -version Exception in thread "main" io.airlift.airline.ParseArgumentsUnexpectedException: Found unexpected parameters: [-version] at io.airlift.airline.Cli.validate(Cli.java:148) at io.airlift.airline.Cli.parse(Cli.java:116) at io.airlift.airline.Cli.parse(Cli.java:97) at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:41) $ swagger-codegen -help Exception in thread "main" io.airlift.airline.ParseArgumentsUnexpectedException: Found unexpected parameters: [-help] at io.airlift.airline.Cli.validate(Cli.java:148) at io.airlift.airline.Cli.parse(Cli.java:116) at io.airlift.airline.Cli.parse(Cli.java:97) at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:41)

wing328 commented 7 years ago

Please try the following

swagger-codegen|master⚡ ⇒ swagger-codegen help
usage: swagger-codegen-cli <command> [<args>]

The most commonly used swagger-codegen-cli commands are:
    config-help   Config help for chosen lang
    generate      Generate code with chosen lang
    help          Display help information
    langs         Shows available langs
    meta          MetaGenerator. Generator for creating a new template set and configuration for Codegen.  The output will be based on the language you specify, and includes default templates to include.
    version       Show version information

See 'swagger-codegen-cli help <command>' for more information on a specific
command.

swagger-codegen|master⚡ ⇒ swagger-codegen version
2.2.2
dnome2 commented 7 years ago

Ok this is what i have.

swagger-codegen version 2.2.2

@wing328 Seems like 'tags' is an array defined in swagger spec, i'm guessing its generating api for all the tags available in the array. Since the only use-case for tag is to group end point, it should avoid generating _api for rest of the tags in the list or provide any option to name the corresponding endpoint? Can you pls help us with providing a way to avoids generating multiple _api.py files. We certainly want to use the multiple tagging option to enable customers to retrieve the correct endpoint.

giuliopulina commented 7 years ago

is there any workaround to prevent this behavior? I'm experiencing also in swagger-codegen 2.2.3 with java language.

EDIT: I found one that's quite good: specify -Dapis=Api1,Api2 to restrict the list of the tags you specified in the swagger spec

wing328 commented 7 years ago

The only workaround I can think of is to update the spec to allow only single tag per endpoint.

What about adding an option to use only the first tag to generate files?

giuliopulina commented 7 years ago

the problem is that I'm using tags for different purposes, so it is unlikely I will be able to put only one per endpoint. I think that adding an option to use only the first tag could be a good idea, even if, in my opinion, the OpenAPI spec should support a dedicated field to categorize the endpoint.

sreekanth-krishnan commented 6 years ago

I too have this issue. Is there any option to specify which all tags to include while generating client? In my case I have large number of operations in the definition. The API is used by a web admin dashboard and a mobile app. While generating client sdk for Android app I want only a subset of endpoints in that and for some other client I may need all the endpoints. I was looking to do this using tags. But there seems no way to specify the tag while generating client. Is there any different approach to achieve what I need?

UPDATE Just now managed to do what I wanted using this command -Dapis=AnAPI -Dmodels -DsupportingFiles where 'default' is the tag that I wanted to export. I think the documentation doesn't specify these things in detail.