Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{#pattern}}{{^isByteArray}} @Pattern(regexp="{{{pattern}}}"){{/isByteArray}}{{/pattern}}{{!
{{#isEmail}} @Email{{/isEmail}}{{!
}}{{#pattern}}{{^isByteArray}} @Pattern(regexp="{{{pattern}}}"){{/isByteArray}}{{/pattern}}{{!
minLength && maxLength set
}}{{#minLength}}{{#maxLength}} @Size(min={{minLength}},max={{maxLength}}){{/maxLength}}{{/minLength}}{{!
minLength set, maxLength not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,64 @@ public void testJdkHttpClientWithUseBeanValidationEnabled() {
.contains("@Pattern", "import jakarta.validation.constraints.*");
}

@Test
public void testUseBeanValidationGeneratesEmailAnnotation() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.setLibrary(JavaClientCodegen.NATIVE)
.addAdditionalProperty(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model")
.addAdditionalProperty(JavaClientCodegen.USE_BEANVALIDATION, true)
.addAdditionalProperty(JavaClientCodegen.USE_JAKARTA_EE, true)
.setInputSpec("src/test/resources/3_1/issue-17485.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();

validateJavaSourceFiles(files);
// format: email is validated with @Email (required, so @NotNull too); the constraint
// package comes from the wildcard "import jakarta.validation.constraints.*"
assertThat(output.resolve("src/main/java/xyz/abcdef/model/User.java")).content()
.contains("@Email", "@NotNull", "import jakarta.validation.constraints.*");
}

@Test
public void testUseBeanValidationGeneratesEmailAnnotationWithJavax() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.setLibrary(JavaClientCodegen.NATIVE)
.addAdditionalProperty(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model")
.addAdditionalProperty(JavaClientCodegen.USE_BEANVALIDATION, true)
.addAdditionalProperty(JavaClientCodegen.USE_JAKARTA_EE, false)
.setInputSpec("src/test/resources/3_1/issue-17485.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();

validateJavaSourceFiles(files);
assertThat(output.resolve("src/main/java/xyz/abcdef/model/User.java")).content()
.contains("@Email", "import javax.validation.constraints.*");
}

@Test
public void testWithoutBeanValidationDoesNotGenerateEmailAnnotation() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.setLibrary(JavaClientCodegen.NATIVE)
.addAdditionalProperty(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model")
.addAdditionalProperty(JavaClientCodegen.USE_BEANVALIDATION, false)
.setInputSpec("src/test/resources/3_1/issue-17485.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();

validateJavaSourceFiles(files);
assertThat(output.resolve("src/main/java/xyz/abcdef/model/User.java")).content()
.doesNotContain("@Email");
}

@Test
public void testJdkHttpClientWithAndWithoutDiscriminator() {
final Path output = newTempFolder();
Expand Down
17 changes: 17 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/issue-17485.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@ paths:
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/User"
components:
schemas:
User:
type: object
required:
- email
properties:
username:
type: string
email:
type: string
format: email
description: The user's contact email address
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ README.md
api/openapi.yaml
build.gradle
build.sbt
docs/User.md
docs/UserApi.md
git_push.sh
gradle.properties
Expand All @@ -28,3 +29,4 @@ src/main/java/org/openapitools/client/auth/ApiKeyAuth.java
src/main/java/org/openapitools/client/auth/Authentication.java
src/main/java/org/openapitools/client/auth/HttpBasicAuth.java
src/main/java/org/openapitools/client/auth/HttpBearerAuth.java
src/main/java/org/openapitools/client/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public class UserApiExample {
UserApi apiInstance = new UserApi(defaultClient);
List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> username = Arrays.asList(); // List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> | The name of the user
try {
apiInstance.userGet(username);
User result = apiInstance.userGet(username);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling UserApi#userGet");
System.err.println("Status code: " + e.getCode());
Expand All @@ -119,6 +120,7 @@ Class | Method | HTTP request | Description

## Documentation for Models

- [User](docs/User.md)


<a id="documentation-for-authorization"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,28 @@ paths:
style: simple
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/User"
description: OK
tags:
- user
x-accepts:
- application/json
components:
schemas: {}
schemas:
User:
example:
username: username
email: email
properties:
username:
type: string
email:
description: The user's contact email address
format: email
type: string
required:
- email

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


# User


## Properties

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**username** | **String** | | [optional] |
|**email** | **String** | The user&#39;s contact email address | |



Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ All URIs are relative to *http://api.example.xyz/v1*

## userGet

> userGet(username)
> User userGet(username)



Expand All @@ -32,7 +32,8 @@ public class Example {
UserApi apiInstance = new UserApi(defaultClient);
List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> username = Arrays.asList(); // List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> | The name of the user
try {
apiInstance.userGet(username);
User result = apiInstance.userGet(username);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling UserApi#userGet");
System.err.println("Status code: " + e.getCode());
Expand All @@ -53,7 +54,7 @@ public class Example {

### Return type

null (empty response body)
[**User**](User.md)

### Authorization

Expand All @@ -62,7 +63,7 @@ No authorization required
### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: Not defined
- **Accept**: application/json


### HTTP response details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.openapitools.client.ApiClient;
import org.openapitools.client.BaseApi;

import org.openapitools.client.model.User;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -44,21 +45,22 @@ public UserApi(ApiClient apiClient) {
*
* <p><b>200</b> - OK
* @param username The name of the user (required)
* @return User
* @throws RestClientException if an error occurs while attempting to invoke the API
*/
public void userGet(List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> username) throws RestClientException {
userGetWithHttpInfo(username);
public User userGet(List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> username) throws RestClientException {
return userGetWithHttpInfo(username).getBody();
}

/**
*
*
* <p><b>200</b> - OK
* @param username The name of the user (required)
* @return ResponseEntity&lt;Void&gt;
* @return ResponseEntity&lt;User&gt;
* @throws RestClientException if an error occurs while attempting to invoke the API
*/
public ResponseEntity<Void> userGetWithHttpInfo(List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> username) throws RestClientException {
public ResponseEntity<User> userGetWithHttpInfo(List<@Pattern(regexp = "^[a-zA-Z0-9]$")String> username) throws RestClientException {
Object localVarPostBody = null;

// verify the required parameter 'username' is set
Expand All @@ -75,14 +77,16 @@ public ResponseEntity<Void> userGetWithHttpInfo(List<@Pattern(regexp = "^[a-zA-Z
final MultiValueMap<String, String> localVarCookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap<String, Object> localVarFormParams = new LinkedMultiValueMap<String, Object>();

final String[] localVarAccepts = { };
final String[] localVarAccepts = {
"application/json"
};
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = { };
final MediaType localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);

String[] localVarAuthNames = new String[] { };

ParameterizedTypeReference<Void> localReturnType = new ParameterizedTypeReference<Void>() {};
ParameterizedTypeReference<User> localReturnType = new ParameterizedTypeReference<User>() {};
return apiClient.invokeAPI("/user", HttpMethod.GET, uriVariables, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, localReturnType);
}

Expand All @@ -97,7 +101,9 @@ public <T> ResponseEntity<T> invokeAPI(String url, HttpMethod method, Object req
final MultiValueMap<String, String> localVarCookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap<String, Object> localVarFormParams = new LinkedMultiValueMap<String, Object>();

final String[] localVarAccepts = { };
final String[] localVarAccepts = {
"application/json"
};
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = { };
final MediaType localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
Expand Down
Loading
Loading