forked from loafle/openapi-generator-original
[html2] Fix import statements for most languages (#4243)
* [html2] Clean up namespace issues in code samples * pull c# and php package namespace from --additional-properties arg phpInvokerPackage arg now sets the PHP namespace and packageName sets the CSharp namespace. invokerPackage still works for Java and Android namespace.
This commit is contained in:
parent
6ade001663
commit
bd81f3264d
@ -16,6 +16,9 @@ public class CodegenConstants {
|
||||
public static final String INVOKER_PACKAGE = "invokerPackage";
|
||||
public static final String INVOKER_PACKAGE_DESC = "root package for generated code";
|
||||
|
||||
public static final String PHP_INVOKER_PACKAGE = "phpInvokerPackage";
|
||||
public static final String PHP_INVOKER_PACKAGE_DESC = "root package for generated php code";
|
||||
|
||||
public static final String GROUP_ID = "groupId";
|
||||
public static final String GROUP_ID_DESC = "groupId in generated pom.xml";
|
||||
|
||||
|
@ -7,17 +7,23 @@ import io.swagger.models.Swagger;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.Property;
|
||||
import io.swagger.models.Info;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class StaticHtml2Generator extends DefaultCodegen implements CodegenConfig {
|
||||
protected String invokerPackage = "io.swagger.client";
|
||||
protected String invokerPackage = "io.swagger.client"; // default for Java and Android
|
||||
protected String phpInvokerPackage = "Swagger\\Client"; // default for PHP
|
||||
protected String packageName = "IO.Swagger"; // default for C#
|
||||
protected String groupId = "io.swagger";
|
||||
protected String artifactId = "swagger-client";
|
||||
protected String artifactVersion = "1.0.0";
|
||||
protected String jsProjectName;
|
||||
protected String jsModuleName;
|
||||
|
||||
public StaticHtml2Generator() {
|
||||
super();
|
||||
@ -33,6 +39,8 @@ public class StaticHtml2Generator extends DefaultCodegen implements CodegenConfi
|
||||
cliOptions.add(new CliOption("licenseInfo", "a short description of the license"));
|
||||
cliOptions.add(new CliOption("licenseUrl", "a URL pointing to the full license"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PHP_INVOKER_PACKAGE, CodegenConstants.PHP_INVOKER_PACKAGE_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "C# package name"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.GROUP_ID, CodegenConstants.GROUP_ID_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_ID, CodegenConstants.ARTIFACT_ID_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, CodegenConstants.ARTIFACT_VERSION_DESC));
|
||||
@ -44,6 +52,8 @@ public class StaticHtml2Generator extends DefaultCodegen implements CodegenConfi
|
||||
additionalProperties.put("licenseInfo", "All rights reserved");
|
||||
additionalProperties.put("licenseUrl", "http://apache.org/licenses/LICENSE-2.0.html");
|
||||
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
|
||||
additionalProperties.put(CodegenConstants.PHP_INVOKER_PACKAGE, phpInvokerPackage);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
|
||||
additionalProperties.put(CodegenConstants.GROUP_ID, groupId);
|
||||
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
|
||||
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
|
||||
@ -101,6 +111,29 @@ public class StaticHtml2Generator extends DefaultCodegen implements CodegenConfi
|
||||
return objs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preprocessSwagger(Swagger swagger) {
|
||||
super.preprocessSwagger(swagger);
|
||||
|
||||
if (swagger.getInfo() != null) {
|
||||
Info info = swagger.getInfo();
|
||||
if (StringUtils.isBlank(jsProjectName) && info.getTitle() != null) {
|
||||
// when jsProjectName is not specified, generate it from info.title
|
||||
jsProjectName = sanitizeName(dashize(info.getTitle()));
|
||||
}
|
||||
}
|
||||
|
||||
// default values
|
||||
if (StringUtils.isBlank(jsProjectName)) {
|
||||
jsProjectName = "swagger-js-client";
|
||||
}
|
||||
if (StringUtils.isBlank(jsModuleName)) {
|
||||
jsModuleName = camelize(underscore(jsProjectName));
|
||||
}
|
||||
|
||||
additionalProperties.put("jsProjectName", jsProjectName);
|
||||
additionalProperties.put("jsModuleName", jsModuleName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
|
||||
|
@ -222,50 +222,36 @@
|
||||
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane active" id="examples-{{baseName}}-{{nickname}}-0-curl">
|
||||
<pre class="prettyprint"><code class="language-bsh">
|
||||
curl -X <span style="text-transform: uppercase;">{{httpMethod}}</span>{{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}} -H "{{keyParamName}}: [[apiKey]]" {{/isKeyInHeader}}{{/isApiKey}}{{#isBasic}} -H "Authorization: Basic [[basicHash]]" {{/isBasic}}{{/authMethods}} "{{basePath}}{{path}}{{#hasQueryParams}}?{{#queryParams}}{{^-first}}&{{/-first}}{{paramName}}={{vendorExtensions.x-eg}}{{/queryParams}}{{/hasQueryParams}}"
|
||||
</code></pre>
|
||||
<pre class="prettyprint"><code class="language-bsh">curl -X <span style="text-transform: uppercase;">{{httpMethod}}</span>{{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}} -H "{{keyParamName}}: [[apiKey]]" {{/isKeyInHeader}}{{/isApiKey}}{{#isBasic}} -H "Authorization: Basic [[basicHash]]" {{/isBasic}}{{/authMethods}} "{{basePath}}{{path}}{{#hasQueryParams}}?{{#queryParams}}{{^-first}}&{{/-first}}{{paramName}}={{vendorExtensions.x-eg}}{{/queryParams}}{{/hasQueryParams}}"</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-java">
|
||||
<pre class="prettyprint"><code class="language-java">
|
||||
{{>sample_java}}
|
||||
</code></pre>
|
||||
<pre class="prettyprint"><code class="language-java">{{>sample_java}}</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-android">
|
||||
<pre class="prettyprint"><code class="language-java">
|
||||
{{>sample_android}}
|
||||
</code></pre>
|
||||
<pre class="prettyprint"><code class="language-java">{{>sample_android}}</code></pre>
|
||||
</div>
|
||||
<!--
|
||||
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-groovy">
|
||||
<pre class="prettyprint language-json prettyprinted" data-type="json"><code>Coming Soon!</code></pre>
|
||||
</div> -->
|
||||
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-objc">
|
||||
<pre class="prettyprint"><code class="language-cpp">
|
||||
{{>sample_objc}}
|
||||
</code></pre>
|
||||
<pre class="prettyprint"><code class="language-cpp">{{>sample_objc}}</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-javascript">
|
||||
<pre class="prettyprint"><code class="language-js">
|
||||
{{>sample_js}}
|
||||
</code></pre>
|
||||
<pre class="prettyprint"><code class="language-js">{{>sample_js}}</code></pre>
|
||||
</div>
|
||||
|
||||
<!--<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-angular">
|
||||
<pre class="prettyprint language-json prettyprinted" data-type="json"><code>Coming Soon!</code></pre>
|
||||
</div>-->
|
||||
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-csharp">
|
||||
<pre class="prettyprint"><code class="language-cs">
|
||||
{{>sample_csharp}}
|
||||
</code></pre>
|
||||
<pre class="prettyprint"><code class="language-cs">{{>sample_csharp}}</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-0-php">
|
||||
<pre class="prettyprint"><code class="language-php">
|
||||
{{>sample_php}}
|
||||
</code></pre>
|
||||
<pre class="prettyprint"><code class="language-php">{{>sample_php}}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {{{package}}}.{{{classname}}};
|
||||
import {{{invokerPackage}}}.api.{{{classname}}};
|
||||
|
||||
public class {{{classname}}}Example {
|
||||
|
||||
@ -15,4 +15,4 @@ public class {{{classname}}}Example {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ using System;
|
||||
using System.Diagnostics;
|
||||
using {{packageName}}.Api;
|
||||
using {{packageName}}.Client;
|
||||
{{#modelPackage}}
|
||||
using {{{.}}};
|
||||
{{/modelPackage}}
|
||||
using {{packageName}}.Model;
|
||||
|
||||
namespace Example
|
||||
{
|
||||
@ -48,4 +46,4 @@ namespace Example
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import {{{invokerPackage}}}.*;
|
||||
import {{{invokerPackage}}}.auth.*;
|
||||
import {{{invokerPackage}}}.model.*;
|
||||
import {{{package}}}.{{{classname}}};
|
||||
import {{{invokerPackage}}}.api.{{{classname}}};
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
@ -38,4 +38,4 @@ public class {{{classname}}}Example {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
var {{{moduleName}}} = require('{{{projectName}}}');
|
||||
var {{{jsModuleName}}} = require('{{{jsProjectName}}}');
|
||||
{{#hasAuthMethods}}
|
||||
var defaultClient = {{{moduleName}}}.ApiClient.instance;
|
||||
var defaultClient = {{{jsModuleName}}}.ApiClient.instance;
|
||||
{{#authMethods}}{{#isBasic}}
|
||||
// Configure HTTP basic authorization: {{{name}}}
|
||||
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
|
||||
@ -17,7 +17,7 @@ var {{{name}}} = defaultClient.authentications['{{{name}}}'];
|
||||
{{/authMethods}}
|
||||
{{/hasAuthMethods}}
|
||||
|
||||
var api = new {{{moduleName}}}.{{{classname}}}(){{#hasParams}}
|
||||
var api = new {{{jsModuleName}}}.{{{classname}}}(){{#hasParams}}
|
||||
{{#vendorExtensions.x-codegen-hasRequiredParams}}{{#allParams}}{{#required}}
|
||||
var {{{paramName}}} = {{{example}}}; // {{=< >=}}{<&dataType>}<={{ }}=> {{{description}}}
|
||||
{{/required}}{{/allParams}}{{/vendorExtensions.x-codegen-hasRequiredParams}}{{#hasOptionalParams}}
|
||||
|
@ -13,7 +13,6 @@
|
||||
[apiConfig setAccessToken:@"YOUR_ACCESS_TOKEN"];
|
||||
{{/isOAuth}}{{/authMethods}}
|
||||
{{/hasAuthMethods}}
|
||||
|
||||
{{#allParams}}{{{dataType}}} *{{paramName}} = {{{example}}}; // {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
|
||||
{{/allParams}}
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
|
||||
<?php
|
||||
require_once(__DIR__ . '/vendor/autoload.php');
|
||||
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
|
||||
// Configure HTTP basic authorization: {{{name}}}
|
||||
{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setUsername('YOUR_USERNAME');
|
||||
{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setPassword('YOUR_PASSWORD');{{/isBasic}}{{#isApiKey}}
|
||||
{{phpInvokerPackage}}\Configuration::getDefaultConfiguration()->setUsername('YOUR_USERNAME');
|
||||
{{phpInvokerPackage}}\Configuration::getDefaultConfiguration()->setPassword('YOUR_PASSWORD');{{/isBasic}}{{#isApiKey}}
|
||||
// Configure API key authorization: {{{name}}}
|
||||
{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKey('{{{keyParamName}}}', 'YOUR_API_KEY');
|
||||
{{phpInvokerPackage}}\Configuration::getDefaultConfiguration()->setApiKey('{{{keyParamName}}}', 'YOUR_API_KEY');
|
||||
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
|
||||
// {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKeyPrefix('{{{keyParamName}}}', 'Bearer');{{/isApiKey}}{{#isOAuth}}
|
||||
// {{phpInvokerPackage}}\Configuration::getDefaultConfiguration()->setApiKeyPrefix('{{{keyParamName}}}', 'Bearer');{{/isApiKey}}{{#isOAuth}}
|
||||
// Configure OAuth2 access token for authorization: {{{name}}}
|
||||
{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');{{/isOAuth}}{{/authMethods}}
|
||||
{{phpInvokerPackage}}\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');{{/isOAuth}}{{/authMethods}}
|
||||
{{/hasAuthMethods}}
|
||||
|
||||
$api_instance = new {{invokerPackage}}\Api\{{classname}}();
|
||||
$api_instance = new Swagger\Client\Api\{{classname}}();
|
||||
{{#allParams}}${{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}}
|
||||
{{/allParams}}
|
||||
|
||||
@ -22,3 +22,4 @@ try {
|
||||
} catch (Exception $e) {
|
||||
echo 'Exception when calling {{classname}}->{{operationId}}: ', $e->getMessage(), PHP_EOL;
|
||||
}
|
||||
?>
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user