Skip to content

Commit 309a9bc

Browse files
caimoQIURC
andauthored
fix bug JsonPropertyIntrospector处理record类型里的枚举值时,会报空指针异常 #4983 (#5012)
Co-authored-by: QIURC <qiurongcai@huawei.com>
1 parent 9b7f8e6 commit 309a9bc

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/extend/introspector/JsonPropertyIntrospector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import io.swagger.v3.core.jackson.SwaggerAnnotationIntrospector;
2828

29+
import java.util.Objects;
30+
2931

3032
public class JsonPropertyIntrospector extends SwaggerAnnotationIntrospector {
3133
private static final long serialVersionUID = 4157263023893695762L;
@@ -47,10 +49,10 @@ public String findEnumValue(Enum<?> value) {
4749
@Override
4850
public String findPropertyDescription(Annotated annotated) {
4951
Class<?> enumClass = annotated.getRawType();
50-
if (enumClass.isEnum()) {
52+
if (enumClass.isEnum() && Objects.nonNull(annotated.getAnnotated())) {
5153
return SwaggerEnum.JDK.findPropertyDescription(enumClass, annotated.getAnnotated().getAnnotations());
5254
}
53-
if (EnumUtils.isDynamicEnum(enumClass)) {
55+
if (EnumUtils.isDynamicEnum(enumClass) && Objects.nonNull(annotated.getAnnotated())) {
5456
return SwaggerEnum.DYNAMIC.findPropertyDescription(enumClass, annotated.getAnnotated().getAnnotations());
5557
}
5658

swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.logging.Logger;
3132

3233
import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
3334
import org.apache.servicecomb.swagger.SwaggerUtils;
3435
import org.apache.servicecomb.swagger.generator.SwaggerConst;
3536
import org.apache.servicecomb.swagger.generator.SwaggerGeneratorUtils;
37+
import org.apache.servicecomb.swagger.generator.core.pojo.TestTypeClass;
38+
import org.apache.servicecomb.swagger.generator.core.pojo.TestTypeRecord;
3639
import org.apache.servicecomb.swagger.generator.core.pojo.TestType1;
3740
import org.apache.servicecomb.swagger.generator.core.pojo.TestType2;
3841
import org.apache.servicecomb.swagger.generator.core.schema.RepeatOperation;
@@ -50,6 +53,7 @@
5053

5154
@SuppressWarnings("rawtypes")
5255
public class TestSwaggerUtils {
56+
Logger LOGGER = Logger.getLogger(TestSwaggerUtils.class.getName());
5357

5458
@Test
5559
public void testSchemaMethod() {
@@ -117,12 +121,24 @@ private static class AllTypeTest2 {
117121
Map<String, TestType2> t3;
118122

119123
TestType2[] t4;
124+
125+
}
126+
127+
private static class AllTypeTest3{
128+
TestTypeRecord t5;
129+
}
130+
private static class AllTypeTest4{
131+
TestTypeClass t5;
120132
}
121133

122134
@Test
123135
public void testAddDefinitions() {
124-
Field[] fields1 = AllTypeTest1.class.getDeclaredFields();
125-
Field[] fields2 = AllTypeTest2.class.getDeclaredFields();
136+
testAllType(AllTypeTest1.class, AllTypeTest2.class);
137+
}
138+
139+
private void testAllType(Class clazz1, Class clazz2) {
140+
Field[] fields1 = clazz1.getDeclaredFields();
141+
Field[] fields2 = clazz2.getDeclaredFields();
126142
for (Field value : fields1) {
127143
for (Field field : fields2) {
128144
if (value.isSynthetic() || field.isSynthetic()) {
@@ -132,12 +148,18 @@ public void testAddDefinitions() {
132148
testExcep(value.getGenericType(), field.getGenericType());
133149
fail("IllegalArgumentException expected");
134150
} catch (IllegalArgumentException e) {
151+
LOGGER.warning(value.getGenericType() + " " + field.getGenericType() + " " + e.getMessage());
135152
MatcherAssert.assertThat(e.getMessage(), containsString("duplicate param model:"));
136153
}
137154
}
138155
}
139156
}
140157

158+
@Test
159+
public void testAddDefinitionsWithRecord() {
160+
testAllType(AllTypeTest3.class, AllTypeTest4.class);
161+
}
162+
141163
private void testExcep(Type f1, Type f2) {
142164
OpenAPI swagger = new OpenAPI();
143165
SwaggerUtils.resolveTypeSchemas(swagger, f1);
@@ -161,5 +183,13 @@ public void test_resolve_type_schemas_correct() {
161183
// should be ObjectSchema but swagger is not.
162184
// <pre> Assertions.assertTrue(schema instanceof ObjectSchema) </pre>
163185
Assertions.assertEquals("object", schema.getType());
186+
187+
openAPI = new OpenAPI();
188+
schema = SwaggerUtils.getSchema(openAPI, SwaggerUtils.resolveTypeSchemas(openAPI, TestTypeClass.class)); // resolve reference
189+
Assertions.assertEquals("object", schema.getType());
190+
191+
openAPI = new OpenAPI();
192+
schema = SwaggerUtils.getSchema(openAPI, SwaggerUtils.resolveTypeSchemas(openAPI, TestTypeRecord.class)); // resolve reference
193+
Assertions.assertEquals("object", schema.getType());
164194
}
165195
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.servicecomb.swagger.generator.core.pojo;
19+
20+
import io.swagger.v3.oas.annotations.media.Schema;
21+
22+
@Schema(name = "YYY")
23+
public class TestTypeClass {
24+
@Schema(description = "language")
25+
private TestTypeEnumLang testTypeEnumLang;
26+
27+
public TestTypeEnumLang getTestTypeEnumLang() {
28+
return testTypeEnumLang;
29+
}
30+
31+
public void setTestTypeEnumLang(TestTypeEnumLang testTypeEnumLang) {
32+
this.testTypeEnumLang = testTypeEnumLang;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.servicecomb.swagger.generator.core.pojo;
19+
20+
public enum TestTypeEnumLang {
21+
JAVA, CHINESE, UNKNOWN;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.servicecomb.swagger.generator.core.pojo;
19+
20+
import io.swagger.v3.oas.annotations.media.Schema;
21+
22+
@Schema(name = "YYY")
23+
public record TestTypeRecord(
24+
@Schema(description = "language") TestTypeEnumLang testTypeEnumLang) {
25+
}

0 commit comments

Comments
 (0)