[Angular2] Invalid npm package name in package.json
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:
- The name must be less than or equal to 214 characters. This includes the scope for scoped packages.
- The name can't start with a dot or an underscore.
- New packages must not have uppercase letters in the name.
- The name ends up being part of a URL, an argument on the command line, and a folder name. Therefore, the name can't contain any non-URL-safe characters.
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:
- convert letters to lowercase
- remove leading dot or underscore
- replace spaces by dashes
- truncate to 214 characters
Maybe it would be best to also strip out any non-URL-safe characters?
@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
@wing328 this works indeed and was a mistake on my side.
@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)