Skip to content

Conversation

@jmuk
Copy link
Contributor

@jmuk jmuk commented Dec 15, 2016

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

This PR introduces a new language "nodejs-google-cloud-functions." This is quite similar to nodejs server but the index.js is formed to work within Google Cloud Functions.

writeOptional(outputFolder, new SupportingFile("index.mustache", "", "index.js"));
writeOptional(outputFolder, new SupportingFile("package.mustache", "", "package.json"));
writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md"));
if (System.getProperty("noservice") == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmuk thanks for the PR. Instead of using noservice system property, what about using java -Dmodels -DsupportingFiles instead? https://github.com/swagger-api/swagger-codegen#selective-generation

You may also consider using .swagger-codegen-ignore to skip certain files when regenerating the code: https://github.com/swagger-api/swagger-codegen#ignore-file-format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: .swagger-codegen-ignore: removed

re: noservice: well, this is what currently nodejs-server is doing. I think it's better to do things like that in another PR.

import java.util.Map;
import java.util.Map.Entry;

public abstract class AbstractNodeJSCodegen extends DefaultCodegen implements CodegenConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmuk thanks for refactoring NodeJS generator into a common abstract class 👍

@wing328
Copy link
Contributor

wing328 commented Dec 16, 2016

@jmuk thanks for the contribution.

This PR introduces a new language "nodejs-google-cloud-functions." This is quite similar to nodejs server but the index.js is formed to work within Google Cloud Functions.

If only index.js is different, what about adding a CLI option to use different index.js mustache template instead of introducing another generator.

@jmuk
Copy link
Contributor Author

jmuk commented Dec 16, 2016

To be more precise, not only index.js but also package.json (because it doesn't have to depend on some) and README.md are slightly different from nodejs-server -- but yeah, the contents are mostly common. Let me check how I can converge them into a single generator.

@wing328
Copy link
Contributor

wing328 commented Dec 17, 2016

@jmuk One way is to pass --additional-properties=enableGoogleCloudFunction=true via command line argument and then you can use the following in the mustache template

{{#enableGoogleCloudFunction}}
... code for enabling Google cloud function here ...
{{/enableGoogleCloudFunction}}

This does not add a new language, but adding some client options
to support Google Cloud Functions (GCF).
@jmuk
Copy link
Contributor Author

jmuk commented Dec 20, 2016

Updated. Now the single NodeJSServerCodegen.java does the codegen for GCF upon googleCloudFunctions=true additional property.

writeOptional(outputFolder, new SupportingFile("index.mustache", "", "index.js"));
}
writeOptional(outputFolder, new SupportingFile("package.mustache", "", "package.json"));
writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using noservice, what about using selective generation as mentioned in https://github.com/swagger-api/swagger-codegen#selective-generation? Would that meet your need?

I prefer not to introduce another system property if the same output can already be achieved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this comes from the original NodeJSCodegen.java, I just moved the code location. I'm not adding new.

Also, the noservice is for a different purpose from the selective generation.

The nodejs-server codegen generates two files per API. For the example of petstore, it generates files like controllers/Pet.js and controllers/PetService.js (see https://github.com/swagger-api/swagger-codegen/tree/master/samples/server/petstore/nodejs/controllers).

When this noservice is specified, it omits the generation of PetService.js file or any other -Service.js files. Only Pet.js or those files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmuk thanks for the clarification. I'll review and let you know if I've further question.

}
cliOptions.add(CliOption.newBoolean(GOOGLE_CLOUD_FUNCTIONS,
"When specified, it will generate the code which runs within Google Cloud Functions "
+ "instead of standalone Node.JS server."));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: A URL to tutorial on how to deploy the auto-generated code to Google Cloud Functions would be helpful for developers who are new to Google Cloud Functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea. Done. Also added a section in README.mustache template, so people can refer to the URL after the code is generated.

Adds the client options help message and the README.md file.
@wing328 wing328 merged commit 27f1b6e into swagger-api:master Dec 22, 2016
@wing328
Copy link
Contributor

wing328 commented Dec 22, 2016

@jmuk PR merged into master. Thanks for your contribution.

@wing328
Copy link
Contributor

wing328 commented Dec 22, 2016

FYI. I merged #4411 into master to improve the NodeJS code quality a bit.

@@ -0,0 +1,44 @@
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmuk we'll move this file to nodejs/google-cloud-functions/index.mustache (and update the code accordingly). so that all nodejs (server) related files are under the folder.

Are you OK with the proposed change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I don't care the exact file location as long as the code works. But thank you for telling me!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that this file is the same as index-gcf.mustache so I simply removed it via #4808. I did some tests and the generator is still working properly with or without the option for Google cloud function.

@wing328 wing328 changed the title Introduce NodeJS codegen for Google Cloud Functions. [NodeJS] Introduce NodeJS codegen for Google Cloud Functions Feb 21, 2017
davidgri pushed a commit to davidgri/swagger-codegen that referenced this pull request May 11, 2017
* Another approach: extending NodeJS server to support GCF.

This does not add a new language, but adding some client options
to support Google Cloud Functions (GCF).

* Add URLs for how to deploy the generated code.

Adds the client options help message and the README.md file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants