forked from loafle/openapi-generator-original
Properly handle mappings and import mappings after processOpts (#16216)
* make Java imports overridable * fix for other dateLibraries * complete core fix * adapt test - step 1/2 * add unit test * adapt test - step 2/2 * remove accidentally added file * final small clean-up
This commit is contained in:
parent
35f5852cc0
commit
20692aa891
@ -6,3 +6,8 @@ templateDir: modules/openapi-generator/src/main/resources/Java
|
||||
additionalProperties:
|
||||
artifactId: echo-api-native
|
||||
hideGenerationTimestamp: "true"
|
||||
|
||||
typeMappings:
|
||||
OffsetDateTime: "Instant"
|
||||
importMappings:
|
||||
OffsetDateTime: "java.time.Instant"
|
||||
|
@ -22,11 +22,13 @@ import io.swagger.v3.parser.core.models.AuthorizationValue;
|
||||
|
||||
import org.openapitools.codegen.api.TemplateDefinition;
|
||||
import org.openapitools.codegen.auth.AuthParser;
|
||||
import org.openapitools.codegen.config.GeneratorSettings;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ClientOptInput {
|
||||
private CodegenConfig config;
|
||||
private GeneratorSettings generatorSettings;
|
||||
private OpenAPI openAPI;
|
||||
private List<AuthorizationValue> auths;
|
||||
private List<TemplateDefinition> userDefinedTemplates;
|
||||
@ -36,6 +38,11 @@ public class ClientOptInput {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientOptInput generatorSettings(GeneratorSettings generatorSettings) {
|
||||
this.setGeneratorSettings(generatorSettings);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientOptInput config(CodegenConfig codegenConfig) {
|
||||
this.setConfig(codegenConfig);
|
||||
return this;
|
||||
@ -92,6 +99,20 @@ public class ClientOptInput {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public GeneratorSettings getGeneratorSettings() {
|
||||
return generatorSettings;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private void setGeneratorSettings(GeneratorSettings generatorSettings) {
|
||||
this.generatorSettings = generatorSettings;
|
||||
// TODO: ClientOptInputs needs to be retired
|
||||
if (this.openAPI != null) {
|
||||
this.config.setOpenAPI(this.openAPI);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public OpenAPI getOpenAPI() {
|
||||
return openAPI;
|
||||
|
@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.openapitools.codegen.api.TemplateDefinition;
|
||||
import org.openapitools.codegen.api.TemplatePathLocator;
|
||||
import org.openapitools.codegen.api.TemplateProcessor;
|
||||
import org.openapitools.codegen.config.GeneratorSettings;
|
||||
import org.openapitools.codegen.config.GlobalSettings;
|
||||
import org.openapitools.codegen.api.TemplatingEngineAdapter;
|
||||
import org.openapitools.codegen.api.TemplateFileType;
|
||||
@ -253,6 +254,10 @@ public class DefaultGenerator implements Generator {
|
||||
}
|
||||
|
||||
config.processOpts();
|
||||
if (opts != null && opts.getGeneratorSettings() != null) {
|
||||
config.typeMapping().putAll(opts.getGeneratorSettings().getTypeMappings());
|
||||
config.importMapping().putAll(opts.getGeneratorSettings().getImportMappings());
|
||||
}
|
||||
|
||||
// normalize the spec
|
||||
try {
|
||||
|
@ -753,6 +753,7 @@ public class CodegenConfigurator {
|
||||
|
||||
ClientOptInput input = new ClientOptInput()
|
||||
.config(config)
|
||||
.generatorSettings(generatorSettings)
|
||||
.userDefinedTemplates(userDefinedTemplates);
|
||||
|
||||
return input.openAPI((OpenAPI)context.getSpecDocument());
|
||||
|
@ -244,6 +244,26 @@ public class JavaClientCodegenTest {
|
||||
TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/auth/HttpSignatureAuth.java");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportMappingResult() throws IOException {
|
||||
File output = Files.createTempDirectory("test").toFile();
|
||||
|
||||
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||
.addTypeMapping("OffsetDateTime", "Instant")
|
||||
.addImportMapping("OffsetDateTime", "java.time.Instant")
|
||||
.setGeneratorName("java")
|
||||
.setInputSpec("src/test/resources/3_0/echo_api.yaml")
|
||||
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
|
||||
|
||||
final ClientOptInput clientOptInput = configurator.toClientOptInput();
|
||||
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
|
||||
List<File> files = generator.opts(clientOptInput).generate();
|
||||
|
||||
TestUtils.assertFileContains(Paths.get(output + "/src/main/java/org/openapitools/client/api/QueryApi.java"), "import java.time.Instant;");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSupportedSecuritySchemesJersey() throws Exception {
|
||||
final JavaClientCodegen codegen = new JavaClientCodegen();
|
||||
|
@ -9,7 +9,7 @@
|
||||
|------------ | ------------- | ------------- | -------------|
|
||||
|**suffix** | **String** | test suffix | [optional] |
|
||||
|**text** | **String** | Some text containing white spaces | [optional] |
|
||||
|**date** | **OffsetDateTime** | A date | [optional] |
|
||||
|**date** | **Instant** | A date | [optional] |
|
||||
|
||||
|
||||
|
||||
|
@ -181,7 +181,7 @@ public class Example {
|
||||
defaultClient.setBasePath("http://localhost:3000");
|
||||
|
||||
QueryApi apiInstance = new QueryApi(defaultClient);
|
||||
OffsetDateTime datetimeQuery = OffsetDateTime.now(); // OffsetDateTime |
|
||||
Instant datetimeQuery = new Instant(); // Instant |
|
||||
LocalDate dateQuery = LocalDate.now(); // LocalDate |
|
||||
String stringQuery = "stringQuery_example"; // String |
|
||||
try {
|
||||
@ -203,7 +203,7 @@ public class Example {
|
||||
|
||||
| Name | Type | Description | Notes |
|
||||
|------------- | ------------- | ------------- | -------------|
|
||||
| **datetimeQuery** | **OffsetDateTime**| | [optional] |
|
||||
| **datetimeQuery** | **Instant**| | [optional] |
|
||||
| **dateQuery** | **LocalDate**| | [optional] |
|
||||
| **stringQuery** | **String**| | [optional] |
|
||||
|
||||
@ -251,7 +251,7 @@ public class Example {
|
||||
defaultClient.setBasePath("http://localhost:3000");
|
||||
|
||||
QueryApi apiInstance = new QueryApi(defaultClient);
|
||||
OffsetDateTime datetimeQuery = OffsetDateTime.now(); // OffsetDateTime |
|
||||
Instant datetimeQuery = new Instant(); // Instant |
|
||||
LocalDate dateQuery = LocalDate.now(); // LocalDate |
|
||||
String stringQuery = "stringQuery_example"; // String |
|
||||
try {
|
||||
@ -275,7 +275,7 @@ public class Example {
|
||||
|
||||
| Name | Type | Description | Notes |
|
||||
|------------- | ------------- | ------------- | -------------|
|
||||
| **datetimeQuery** | **OffsetDateTime**| | [optional] |
|
||||
| **datetimeQuery** | **Instant**| | [optional] |
|
||||
| **dateQuery** | **LocalDate**| | [optional] |
|
||||
| **stringQuery** | **String**| | [optional] |
|
||||
|
||||
|
@ -19,7 +19,7 @@ import org.openapitools.client.Pair;
|
||||
|
||||
import org.openapitools.client.model.DataQuery;
|
||||
import java.time.LocalDate;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.Instant;
|
||||
import org.openapitools.client.model.Pet;
|
||||
import org.openapitools.client.model.StringEnumRef;
|
||||
import org.openapitools.client.model.TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter;
|
||||
@ -192,7 +192,7 @@ public class QueryApi {
|
||||
* @return String
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public String testQueryDatetimeDateString(OffsetDateTime datetimeQuery, LocalDate dateQuery, String stringQuery) throws ApiException {
|
||||
public String testQueryDatetimeDateString(Instant datetimeQuery, LocalDate dateQuery, String stringQuery) throws ApiException {
|
||||
ApiResponse<String> localVarResponse = testQueryDatetimeDateStringWithHttpInfo(datetimeQuery, dateQuery, stringQuery);
|
||||
return localVarResponse.getData();
|
||||
}
|
||||
@ -206,7 +206,7 @@ public class QueryApi {
|
||||
* @return ApiResponse<String>
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public ApiResponse<String> testQueryDatetimeDateStringWithHttpInfo(OffsetDateTime datetimeQuery, LocalDate dateQuery, String stringQuery) throws ApiException {
|
||||
public ApiResponse<String> testQueryDatetimeDateStringWithHttpInfo(Instant datetimeQuery, LocalDate dateQuery, String stringQuery) throws ApiException {
|
||||
HttpRequest.Builder localVarRequestBuilder = testQueryDatetimeDateStringRequestBuilder(datetimeQuery, dateQuery, stringQuery);
|
||||
try {
|
||||
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
|
||||
@ -243,7 +243,7 @@ public class QueryApi {
|
||||
}
|
||||
}
|
||||
|
||||
private HttpRequest.Builder testQueryDatetimeDateStringRequestBuilder(OffsetDateTime datetimeQuery, LocalDate dateQuery, String stringQuery) throws ApiException {
|
||||
private HttpRequest.Builder testQueryDatetimeDateStringRequestBuilder(Instant datetimeQuery, LocalDate dateQuery, String stringQuery) throws ApiException {
|
||||
|
||||
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
|
||||
|
||||
|
@ -24,7 +24,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -49,7 +49,7 @@ public class DataQuery extends Query {
|
||||
private String text;
|
||||
|
||||
public static final String JSON_PROPERTY_DATE = "date";
|
||||
private OffsetDateTime date;
|
||||
private Instant date;
|
||||
|
||||
public DataQuery() {
|
||||
}
|
||||
@ -104,7 +104,7 @@ public class DataQuery extends Query {
|
||||
}
|
||||
|
||||
|
||||
public DataQuery date(OffsetDateTime date) {
|
||||
public DataQuery date(Instant date) {
|
||||
this.date = date;
|
||||
return this;
|
||||
}
|
||||
@ -117,14 +117,14 @@ public class DataQuery extends Query {
|
||||
@JsonProperty(JSON_PROPERTY_DATE)
|
||||
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
|
||||
|
||||
public OffsetDateTime getDate() {
|
||||
public Instant getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
|
||||
@JsonProperty(JSON_PROPERTY_DATE)
|
||||
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
|
||||
public void setDate(OffsetDateTime date) {
|
||||
public void setDate(Instant date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user