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
16.91k stars 6.03k forks source link

[Angular2] Invalid npm package name in package.json #4650

Closed BastiOfBerlin closed 7 years ago

BastiOfBerlin commented 7 years ago
Description

The title of an API seems to be used as the npm module's name more or less directly. Unfortunately, the conventions for that name attribute are more restrictive than those for API title. Specifically, npm's docs states that:

This leads to invalid package.json files. As a consequence, it is not possible to include the module as a dependency using npm.

Swagger-codegen version

2.2.2-SNAPSHOT as of 2017-01-23

Swagger declaration file content
{
    "swagger": "2.0",
    "info": {
        "title": "LoreM IpsUm - API Gateway",
        "description": "lorem ipsum",
        "version": "1.0.0-SNAPSHOT"
    }
}
Produced package.json
{
  "name": "LoreM IpsUm API",
  "version": "1.0.0"
}
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i "http://localhost:8080/swaggerfile" -l typescript-angular2 -c "swagger-codegen-config.ts.json" --additional-properties npmVersion=1.0.0 -o ".\generated-api"

Suggest a Fix

I suggest converting the title in the following way(s) before using it as the module's name:

Maybe it would be best to also strip out any non-URL-safe characters?

wing328 commented 7 years ago

@BastiOfBerlin have you tried the npmName option to customize the package name?

    npmName
        The name under which you want to publish generated npm package

Ref: java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l typescript-angular2

BastiOfBerlin commented 7 years ago

@wing328 this works indeed and was a mistake on my side.

wing328 commented 7 years ago

@BastiOfBerlin np. Let us know if you've further question or feedback.

(we've just added Swagger Codegen to ProductHunt: https://www.producthunt.com/posts/swagger-codegen, please upvote if you find Swagger Codegen useful)