Skip to content

Commit df81534

Browse files
authored
fix NPE in body parameter due to incorrect parameter/consume (#563)
1 parent a6e15d7 commit df81534

11 files changed

Lines changed: 46 additions & 29 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter) {
11101110
* Return the example value of the parameter.
11111111
*
11121112
* @param codegenParameter Codegen parameter
1113-
* @param parameter Parameter
1113+
* @param parameter Parameter
11141114
*/
11151115
public void setParameterExampleValue(CodegenParameter codegenParameter, Parameter parameter) {
11161116
if (parameter.getExample() != null) {
@@ -1120,7 +1120,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete
11201120

11211121
if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) {
11221122
Example example = parameter.getExamples().values().iterator().next();
1123-
if(example.getValue() != null) {
1123+
if (example.getValue() != null) {
11241124
codegenParameter.example = example.getValue().toString();
11251125
return;
11261126
}
@@ -1139,7 +1139,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete
11391139
* Return the example value of the parameter.
11401140
*
11411141
* @param codegenParameter Codegen parameter
1142-
* @param requestBody Request body
1142+
* @param requestBody Request body
11431143
*/
11441144
public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) {
11451145
Content content = requestBody.getContent();
@@ -1157,7 +1157,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, RequestB
11571157

11581158
if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) {
11591159
Example example = mediaType.getExamples().values().iterator().next();
1160-
if(example.getValue() != null) {
1160+
if (example.getValue() != null) {
11611161
codegenParameter.example = example.getValue().toString();
11621162
return;
11631163
}
@@ -1614,7 +1614,7 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
16141614
m.isInteger = Boolean.TRUE;
16151615
}
16161616
}
1617-
if (ModelUtils.isStringSchema(schema)){
1617+
if (ModelUtils.isStringSchema(schema)) {
16181618
m.isString = Boolean.TRUE;
16191619
}
16201620

@@ -4373,15 +4373,32 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
43734373
if (schema.getAdditionalProperties() != null) {// http body is map
43744374
LOGGER.error("Map should be supported. Please report to openapi-generator github repo about the issue.");
43754375
} else if (codegenProperty != null) {
4376+
String codegenModelName, codegenModelDescription;
4377+
4378+
if (codegenModel != null) {
4379+
codegenModelName = codegenModel.classname;
4380+
codegenModelDescription = codegenModel.description;
4381+
} else {
4382+
LOGGER.warn("The following schema has undefined (null) baseType. " +
4383+
"It could be due to form parameter defined in OpenAPI v2 spec with incorrect consumes. " +
4384+
"A correct 'consumes' for form parameters should be " +
4385+
"'application/x-www-form-urlencoded' or 'multipart/form-data'");
4386+
LOGGER.warn("schema: " + schema);
4387+
LOGGER.warn("codegenModel is null. Default to UNKNOWN_BASE_TYPE");
4388+
codegenModelName = "UNKNOWN_BASE_TYPE";
4389+
codegenModelDescription = "UNKNOWN_DESCRIPTION";
4390+
}
4391+
43764392
if (StringUtils.isEmpty(bodyParameterName)) {
4377-
codegenParameter.baseName = codegenModel.classname;
4393+
codegenParameter.baseName = codegenModelName;
43784394
} else {
43794395
codegenParameter.baseName = bodyParameterName;
43804396
}
4397+
43814398
codegenParameter.paramName = toParamName(codegenParameter.baseName);
4382-
codegenParameter.baseType = codegenModel.classname;
4383-
codegenParameter.dataType = getTypeDeclaration(codegenModel.classname);
4384-
codegenParameter.description = codegenModel.description;
4399+
codegenParameter.baseType = codegenModelName;
4400+
codegenParameter.dataType = getTypeDeclaration(codegenModelName);
4401+
codegenParameter.description = codegenModelDescription;
43854402
imports.add(codegenParameter.baseType);
43864403

43874404
if (codegenProperty.complexType != null) {
@@ -4465,6 +4482,6 @@ public void generateYAMLSpecFile(Map<String, Object> objs) {
44654482
}
44664483

44674484
public boolean isDataTypeString(String dataType) {
4468-
return "String".equals(dataType);
4485+
return "String".equals(dataType);
44694486
}
44704487
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.0-SNAPSHOT
1+
3.1.1-SNAPSHOT

samples/client/petstore-security-test/php/OpenAPIClient-php/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ $apiInstance = new OpenAPI\Client\Api\FakeApi(
6161
// This is optional, `GuzzleHttp\Client` will be used as default.
6262
new GuzzleHttp\Client()
6363
);
64-
$unknown_base_type = new \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE(); // object |
64+
$unknown_base_type = new \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE(); // \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE |
6565

6666
try {
6767
$apiInstance->testCodeInjectEndRnNR($unknown_base_type);
@@ -74,7 +74,7 @@ try {
7474

7575
## Documentation for API Endpoints
7676

77-
All URIs are relative to *petstore.swagger.io *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r/v2 *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r*
77+
All URIs are relative to *http://petstore.swagger.io *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r/v2 *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r*
7878

7979
Class | Method | HTTP request | Description
8080
------------ | ------------- | ------------- | -------------

samples/client/petstore-security-test/php/OpenAPIClient-php/docs/Api/FakeApi.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenAPI\Client\FakeApi
22

3-
All URIs are relative to *petstore.swagger.io *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r/v2 *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r*
3+
All URIs are relative to *http://petstore.swagger.io *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r/v2 *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
@@ -22,7 +22,7 @@ $apiInstance = new OpenAPI\Client\Api\FakeApi(
2222
// This is optional, `GuzzleHttp\Client` will be used as default.
2323
new GuzzleHttp\Client()
2424
);
25-
$unknown_base_type = new \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE(); // object |
25+
$unknown_base_type = new \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE(); // \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE |
2626

2727
try {
2828
$apiInstance->testCodeInjectEndRnNR($unknown_base_type);
@@ -36,7 +36,7 @@ try {
3636

3737
Name | Type | Description | Notes
3838
------------- | ------------- | ------------- | -------------
39-
**unknown_base_type** | [**object**](../Model/UNKNOWN_BASE_TYPE.md)| | [optional]
39+
**unknown_base_type** | [**\OpenAPI\Client\Model\UNKNOWN_BASE_TYPE**](../Model/UNKNOWN_BASE_TYPE.md)| | [optional]
4040

4141
### Return type
4242

samples/client/petstore-security-test/php/OpenAPIClient-php/lib/Api/FakeApi.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://openapi-generator.tech
20-
* OpenAPI Generator version: 3.1.0-SNAPSHOT
20+
* OpenAPI Generator version: 3.1.1-SNAPSHOT
2121
*/
2222

2323
/**
@@ -92,7 +92,7 @@ public function getConfig()
9292
*
9393
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
9494
*
95-
* @param object $unknown_base_type unknown_base_type (optional)
95+
* @param \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE $unknown_base_type unknown_base_type (optional)
9696
*
9797
* @throws \OpenAPI\Client\ApiException on non-2xx response
9898
* @throws \InvalidArgumentException
@@ -108,7 +108,7 @@ public function testCodeInjectEndRnNR($unknown_base_type = null)
108108
*
109109
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
110110
*
111-
* @param object $unknown_base_type (optional)
111+
* @param \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE $unknown_base_type (optional)
112112
*
113113
* @throws \OpenAPI\Client\ApiException on non-2xx response
114114
* @throws \InvalidArgumentException
@@ -160,7 +160,7 @@ public function testCodeInjectEndRnNRWithHttpInfo($unknown_base_type = null)
160160
*
161161
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
162162
*
163-
* @param object $unknown_base_type (optional)
163+
* @param \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE $unknown_base_type (optional)
164164
*
165165
* @throws \InvalidArgumentException
166166
* @return \GuzzleHttp\Promise\PromiseInterface
@@ -180,7 +180,7 @@ function ($response) {
180180
*
181181
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
182182
*
183-
* @param object $unknown_base_type (optional)
183+
* @param \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE $unknown_base_type (optional)
184184
*
185185
* @throws \InvalidArgumentException
186186
* @return \GuzzleHttp\Promise\PromiseInterface
@@ -216,7 +216,7 @@ function ($exception) {
216216
/**
217217
* Create request for operation 'testCodeInjectEndRnNR'
218218
*
219-
* @param object $unknown_base_type (optional)
219+
* @param \OpenAPI\Client\Model\UNKNOWN_BASE_TYPE $unknown_base_type (optional)
220220
*
221221
* @throws \InvalidArgumentException
222222
* @return \GuzzleHttp\Psr7\Request

samples/client/petstore-security-test/php/OpenAPIClient-php/lib/ApiException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://openapi-generator.tech
20-
* OpenAPI Generator version: 3.1.0-SNAPSHOT
20+
* OpenAPI Generator version: 3.1.1-SNAPSHOT
2121
*/
2222

2323
/**

samples/client/petstore-security-test/php/OpenAPIClient-php/lib/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://openapi-generator.tech
20-
* OpenAPI Generator version: 3.1.0-SNAPSHOT
20+
* OpenAPI Generator version: 3.1.1-SNAPSHOT
2121
*/
2222

2323
/**
@@ -81,7 +81,7 @@ class Configuration
8181
*
8282
* @var string
8383
*/
84-
protected $host = 'petstore.swagger.io *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r/v2 *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r';
84+
protected $host = 'http://petstore.swagger.io *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r/v2 *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r';
8585

8686
/**
8787
* User agent of the HTTP request, set to "OpenAPI-Generator/{version}/PHP" by default

samples/client/petstore-security-test/php/OpenAPIClient-php/lib/HeaderSelector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://openapi-generator.tech
20-
* OpenAPI Generator version: 3.1.0-SNAPSHOT
20+
* OpenAPI Generator version: 3.1.1-SNAPSHOT
2121
*/
2222

2323
/**

samples/client/petstore-security-test/php/OpenAPIClient-php/lib/Model/ModelInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
2020
* Generated by: https://openapi-generator.tech
21-
* OpenAPI Generator version: 3.1.0-SNAPSHOT
21+
* OpenAPI Generator version: 3.1.1-SNAPSHOT
2222
*/
2323

2424
/**

samples/client/petstore-security-test/php/OpenAPIClient-php/lib/Model/ModelReturn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
2020
* Generated by: https://openapi-generator.tech
21-
* OpenAPI Generator version: 3.1.0-SNAPSHOT
21+
* OpenAPI Generator version: 3.1.1-SNAPSHOT
2222
*/
2323

2424
/**

0 commit comments

Comments
 (0)