Skip to content
Merged
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
1 change: 1 addition & 0 deletions bin/configs/java-jersey2-8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ generatorName: java
outputDir: samples/openapi3/client/petstore/java/jersey2-java8
library: jersey2
inputSpec: modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
artifactId: petstore-openapi3-jersey2-java8
hideGenerationTimestamp: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1486,20 +1486,20 @@ public DefaultCodegen() {

// option to change how we process + set the data in the 'additionalProperties' keyword.
CliOption disallowAdditionalPropertiesIfNotPresentOpt = CliOption.newBoolean(
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT,
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString());
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT,
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString());
Map<String, String> disallowAdditionalPropertiesIfNotPresentOpts = new HashMap<>();
disallowAdditionalPropertiesIfNotPresentOpts.put("false",
"The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.");
"The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.");
disallowAdditionalPropertiesIfNotPresentOpts.put("true",
"when the 'additionalProperties' keyword is not present in a schema, " +
"the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " +
"Note: this mode is not compliant with the JSON schema specification. " +
"This is the original openapi-generator behavior.");
"when the 'additionalProperties' keyword is not present in a schema, " +
"the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " +
"Note: this mode is not compliant with the JSON schema specification. " +
"This is the original openapi-generator behavior.");
disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts);
cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt);
this.setDisallowAdditionalPropertiesIfNotPresent(true);

// initialize special character mapping
initalizeSpecialCharacterMapping();

Expand Down Expand Up @@ -2640,7 +2640,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
Integer hasDiscriminatorCnt = 0;
Integer hasNullTypeCnt = 0;
Set<String> discriminatorsPropNames = new HashSet<>();
for (Schema oneOf: composedSchema.getOneOf()) {
for (Schema oneOf : composedSchema.getOneOf()) {
if (ModelUtils.isNullType(oneOf)) {
// The null type does not have a discriminator. Skip.
hasNullTypeCnt++;
Expand All @@ -2654,7 +2654,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
}
if (discriminatorsPropNames.size() > 1) {
throw new RuntimeException("The oneOf schemas have conflicting discriminator property names. " +
"oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
"oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
}
if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) {
disc.setPropertyName(foundDisc.getPropertyName());
Expand All @@ -2669,7 +2669,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
Integer hasDiscriminatorCnt = 0;
Integer hasNullTypeCnt = 0;
Set<String> discriminatorsPropNames = new HashSet<>();
for (Schema anyOf: composedSchema.getAnyOf()) {
for (Schema anyOf : composedSchema.getAnyOf()) {
if (ModelUtils.isNullType(anyOf)) {
// The null type does not have a discriminator. Skip.
hasNullTypeCnt++;
Expand Down Expand Up @@ -2720,7 +2720,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
if (schemaList == null) {
continue;
}
for (Schema sc: schemaList) {
for (Schema sc : schemaList) {
if (ModelUtils.isNullType(sc)) {
continue;
}
Expand Down Expand Up @@ -2875,7 +2875,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
* Handle the model for the 'additionalProperties' keyword in the OAS schema.
*
* @param codegenModel The codegen representation of the schema.
* @param schema the input OAS schema.
* @param schema The input OAS schema.
*/
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
addParentContainer(codegenModel, codegenModel.name, schema);
Expand Down Expand Up @@ -3503,7 +3503,7 @@ protected void handleMethodResponse(Operation operation,
op.returnType = cm.dataType;
op.returnFormat = cm.dataFormat;
op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType);

// lookup discriminator
Schema schema = schemas.get(op.returnBaseType);
if (schema != null) {
Expand Down Expand Up @@ -3965,8 +3965,8 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
r.containerType = cp.containerType;
r.isMapContainer = "map".equals(cp.containerType);
r.isListContainer = "list".equalsIgnoreCase(cp.containerType) ||
"array".equalsIgnoreCase(cp.containerType) ||
"set".equalsIgnoreCase(cp.containerType);
"array".equalsIgnoreCase(cp.containerType) ||
"set".equalsIgnoreCase(cp.containerType);
} else {
r.simpleType = true;
}
Expand Down Expand Up @@ -4317,7 +4317,7 @@ public boolean isDataTypeBinary(String dataType) {
return false;
}
}

// TODO revise below as it should be replaced by ModelUtils.isFileSchema(parameterSchema)
public boolean isDataTypeFile(String dataType) {
if (dataType != null) {
Expand Down Expand Up @@ -5795,6 +5795,7 @@ private void addBodyModelSchema(CodegenParameter codegenParameter, String name,
codegenParameter.baseType = codegenModel.classname;
codegenParameter.dataType = getTypeDeclaration(codegenModel.classname);
codegenParameter.description = codegenModel.description;
codegenParameter.isNullable = codegenModel.isNullable;
imports.add(codegenParameter.baseType);
} else {
CodegenProperty codegenProperty = fromProperty("property", schema);
Expand All @@ -5808,6 +5809,7 @@ private void addBodyModelSchema(CodegenParameter codegenParameter, String name,
codegenParameter.baseType = codegenParameter.baseName;
codegenParameter.dataType = getTypeDeclaration(codegenModelName);
codegenParameter.description = codegenProperty.getDescription();
codegenParameter.isNullable = codegenProperty.isNullable;
} else {
if (ModelUtils.isMapSchema(schema)) {// http body is map
LOGGER.error("Map should be supported. Please report to openapi-generator github repo about the issue.");
Expand Down Expand Up @@ -5916,6 +5918,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
codegenParameter.baseType = getSchemaType(inner);
codegenParameter.isContainer = Boolean.TRUE;
codegenParameter.isMapContainer = Boolean.TRUE;
codegenParameter.isNullable = codegenProperty.isNullable;

setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);

Expand Down Expand Up @@ -5958,6 +5961,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
codegenParameter.baseType = getSchemaType(inner);
codegenParameter.isContainer = Boolean.TRUE;
codegenParameter.isListContainer = Boolean.TRUE;
codegenParameter.isNullable = codegenProperty.isNullable;

setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
// set nullable
Expand All @@ -5981,6 +5985,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
codegenParameter.baseType = codegenProperty.baseType;
codegenParameter.dataType = codegenProperty.dataType;
codegenParameter.description = codegenProperty.description;
codegenParameter.isNullable = codegenProperty.isNullable;
codegenParameter.paramName = toParamName(codegenParameter.baseName);
}
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
Expand Down Expand Up @@ -6010,6 +6015,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
codegenParameter.minLength = codegenProperty.minLength;
codegenParameter.maxLength = codegenProperty.maxLength;
codegenParameter.pattern = codegenProperty.pattern;
codegenParameter.isNullable = codegenProperty.isNullable;

if (codegenProperty.complexType != null) {
imports.add(codegenProperty.complexType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ public class ApiClient {
* @return Entity
* @throws ApiException API exception
*/
public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType) throws ApiException {
public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType, boolean isBodyNullable) throws ApiException {
Entity<?> entity;
if (contentType.startsWith("multipart/form-data")) {
MultiPart multiPart = new MultiPart();
Expand All @@ -863,7 +863,11 @@ public class ApiClient {
entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
} else {
// We let jersey handle the serialization
entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType);
if (isBodyNullable) { // payload is nullable
entity = Entity.entity(obj == null ? Entity.text("null") : obj, contentType);
} else {
entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType);
}
}
return entity;
}
Expand All @@ -874,10 +878,11 @@ public class ApiClient {
* @param obj Object
* @param formParams Form parameters
* @param contentType Context type
* @param isBodyNulalble True if the body is nullable
* @return String
* @throws ApiException API exception
*/
public String serializeToString(Object obj, Map<String, Object> formParams, String contentType) throws ApiException {
public String serializeToString(Object obj, Map<String, Object> formParams, String contentType, boolean isBodyNullable) throws ApiException {
try {
if (contentType.startsWith("multipart/form-data")) {
throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)");
Expand All @@ -893,7 +898,11 @@ public class ApiClient {
return formString.substring(0, formString.length() - 1);
}
} else {
return json.getMapper().writeValueAsString(obj);
if (isBodyNullable) {
return obj == null ? "null" : json.getMapper().writeValueAsString(obj);
} else {
return json.getMapper().writeValueAsString(obj);
}
}
} catch (Exception ex) {
throw new ApiException("Failed to perform serializeToString: " + ex.toString());
Expand Down Expand Up @@ -1007,6 +1016,7 @@ public class ApiClient {
* @param contentType The request's Content-Type header
* @param authNames The authentications to apply
* @param returnType The return type into which to deserialize the response
* @param isBodyNullable True if the body is nullable
* @return The response body in type of string
* @throws ApiException API exception
*/
Expand All @@ -1022,7 +1032,8 @@ public class ApiClient {
String accept,
String contentType,
String[] authNames,
GenericType<T> returnType)
GenericType<T> returnType,
boolean isBodyNullable)
throws ApiException {

// Not using `.target(targetURL).path(path)` below,
Expand Down Expand Up @@ -1069,7 +1080,7 @@ public class ApiClient {
}
}

Entity<?> entity = serialize(body, formParams, contentType);
Entity<?> entity = serialize(body, formParams, contentType, isBodyNullable);

// put all headers in one place
Map<String, String> allHeaderParams = new HashMap<>(defaultHeaderMap);
Expand All @@ -1081,7 +1092,7 @@ public class ApiClient {
queryParams,
allHeaderParams,
cookieParams,
serializeToString(body, formParams, contentType),
serializeToString(body, formParams, contentType, isBodyNullable),
method,
target.getUri());

Expand Down Expand Up @@ -1170,8 +1181,8 @@ public class ApiClient {
* @deprecated Add qualified name of the operation as a first parameter.
*/
@Deprecated
public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType) throws ApiException {
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType, boolean isBodyNullable) throws ApiException {
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public class {{classname}} {
{{/returnType}}
return apiClient.invokeAPI("{{classname}}.{{operationId}}", localVarPath, "{{httpMethod}}", localVarQueryParams, localVarPostBody,
localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType,
localVarAuthNames, {{#returnType}}localVarReturnType{{/returnType}}{{^returnType}}null{{/returnType}});
localVarAuthNames, {{#returnType}}localVarReturnType{{/returnType}}{{^returnType}}null{{/returnType}}, {{#bodyParam}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/bodyParam}}{{^bodyParam}}false{{/bodyParam}});
}
{{#vendorExtensions.x-group-parameters}}

Expand Down
Loading