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

typescript-angular2: Make the api suffix configurable #8281

Open tomvangreen opened 6 years ago

tomvangreen commented 6 years ago

I'd be happy to have a look if I'm able to provide a pull request for this feature request, if nobody is already working on this. I had a look in the issue tracker but there was only a request for changing it from Api to Service and another unrelated request for adding a possiblity to define a suffix in the JAXRs generator.

Description

In earlier versions the generated service classes were suffixed with "Api". Recently this has changed to be "Service". While this adheres to angular standards it sometimes can cause collisions with existing services. For me it would make sense to use the api suffix because of multiple reasons.

  1. We avoid collisions. If I have a UserService in my client and somebody introduces a UserApi, suddenly I need to alias my imports in order to use both services in one place.

  2. Having the .api file extensions and the Api suffix in the class name, it makes it more clear to the developer, that this is not a normal service. If you work in our project then you know that apis are always generated.

Suggest a fix/enhancement

Introduce a configuration for typescript-angular2 which lets you specify a different suffix.

argarner commented 3 years ago

For anyone coming across these old issues (that may never be resolved), I created the following script to:

  1. pre-process the swagger file tags to add an "api" suffix, to prevent generated service names conflicting with UI Angular service best practices (so OrderService becomes OrderApiService)
  2. post-process the output files, to fix this old bug with suffix duplication, by correcting the output model import duplication, so all generated models use an 'Api' prefix (OrderModel becomes ApiOrderModel)

NOTE: The script uses docker and is written for MacOS (which has some sed quirks that will have to be amended for other OS's)

#!/bin/bash
TEMP_SWAGGER_LOCATION=./order-api/
TEMP_SWAGGER_NAME=swagger-temp.yaml
TEMP_SWAGGER_FILE=$TEMP_SWAGGER_LOCATION$TEMP_SWAGGER_NAME
TEMP_SWAGGER_SUFFIX=api

echo "-----------------------------------------------"
echo "Clean up source folder..."
rm -r ./order-api/src/
echo "   ...done"
echo ""
echo "-----------------------------------------------"
echo "pre-processing swagger to add an 'Api' suffix to all service names for code generation"
cp ./order-api/swagger.yaml "$TEMP_SWAGGER_FILE"
echo "searching and replacing all 'tags:' in "$TEMP_SWAGGER_NAME""
TAGS=$(grep -e '- name:' "$TEMP_SWAGGER_FILE" | sed 's/^.*: //')
# replace "tags:" declaration names with -api suffix
sed -i '' "s/- name: .*/&-$TEMP_SWAGGER_SUFFIX/g" "$TEMP_SWAGGER_FILE"

# loop over tags and add '-api' suffix to each
for TAG in ${TAGS}; do
  echo "$TAG"
  sed -i '' "s/- $TAG$/&-$TEMP_SWAGGER_SUFFIX/g" "$TEMP_SWAGGER_FILE"
done

echo "-----------------------------------------------"
echo "Generating code..."
docker run --rm --net=host -u="$(id -u)" -v ${PWD}/order-api:/local \
    swaggerapi/swagger-codegen-cli generate \
    -i /local/$TEMP_SWAGGER_NAME \
    -l typescript-angular \
    -o /local/src \
    --additional-properties ngVersion=9.0.1 \
    --model-name-prefix=$TEMP_SWAGGER_SUFFIX

find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's/rxjs\//rxjs/' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's/formParams = formParams/formParams/' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's/ || formParams//' {} \;

# fix longstanding model prefix generation issue
# https://github.com/swagger-api/swagger-codegen/issues/7866 and https://github.com/swagger-api/swagger-codegen/issues/8281
find ./order-api/src/model/*.ts -type f -exec sed -i '' -e 's|/apiApi|/api|' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's|/apiApi|/api|' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's|ApiApi|Api|' {} \;

echo "   ...done"
echo ""

echo "-----------------------------------------------"
echo "Building Library"
(cd ./order-api; ng build order-api --prod)
echo "   ...done"
echo ""
echo "Compressing"
tar -zcvf order-api.tar.gz ./dist/order-api

echo "Cleaning up temp swagger file..."
rm -f "$TEMP_SWAGGER_FILE"
echo "generate.sh library generation complete"