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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

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

import java.util.Objects;


public class JsonPropertyIntrospector extends SwaggerAnnotationIntrospector {
private static final long serialVersionUID = 4157263023893695762L;
Expand All @@ -47,10 +49,10 @@ public String findEnumValue(Enum<?> value) {
@Override
public String findPropertyDescription(Annotated annotated) {
Class<?> enumClass = annotated.getRawType();
if (enumClass.isEnum()) {
if (enumClass.isEnum() && Objects.nonNull(annotated.getAnnotated())) {
return SwaggerEnum.JDK.findPropertyDescription(enumClass, annotated.getAnnotated().getAnnotations());
}
if (EnumUtils.isDynamicEnum(enumClass)) {
if (EnumUtils.isDynamicEnum(enumClass) && Objects.nonNull(annotated.getAnnotated())) {
return SwaggerEnum.DYNAMIC.findPropertyDescription(enumClass, annotated.getAnnotated().getAnnotations());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
import org.apache.servicecomb.swagger.SwaggerUtils;
import org.apache.servicecomb.swagger.generator.SwaggerConst;
import org.apache.servicecomb.swagger.generator.SwaggerGeneratorUtils;
import org.apache.servicecomb.swagger.generator.core.pojo.TestTypeClass;
import org.apache.servicecomb.swagger.generator.core.pojo.TestTypeRecord;
import org.apache.servicecomb.swagger.generator.core.pojo.TestType1;
import org.apache.servicecomb.swagger.generator.core.pojo.TestType2;
import org.apache.servicecomb.swagger.generator.core.schema.RepeatOperation;
Expand All @@ -50,6 +53,7 @@

@SuppressWarnings("rawtypes")
public class TestSwaggerUtils {
Logger LOGGER = Logger.getLogger(TestSwaggerUtils.class.getName());

@Test
public void testSchemaMethod() {
Expand Down Expand Up @@ -117,12 +121,24 @@ private static class AllTypeTest2 {
Map<String, TestType2> t3;

TestType2[] t4;

}

private static class AllTypeTest3{
TestTypeRecord t5;
}
private static class AllTypeTest4{
TestTypeClass t5;
}

@Test
public void testAddDefinitions() {
Field[] fields1 = AllTypeTest1.class.getDeclaredFields();
Field[] fields2 = AllTypeTest2.class.getDeclaredFields();
testAllType(AllTypeTest1.class, AllTypeTest2.class);
}

private void testAllType(Class clazz1, Class clazz2) {
Field[] fields1 = clazz1.getDeclaredFields();
Field[] fields2 = clazz2.getDeclaredFields();
for (Field value : fields1) {
for (Field field : fields2) {
if (value.isSynthetic() || field.isSynthetic()) {
Expand All @@ -132,12 +148,18 @@ public void testAddDefinitions() {
testExcep(value.getGenericType(), field.getGenericType());
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
LOGGER.warning(value.getGenericType() + " " + field.getGenericType() + " " + e.getMessage());
MatcherAssert.assertThat(e.getMessage(), containsString("duplicate param model:"));
}
}
}
}

@Test
public void testAddDefinitionsWithRecord() {
testAllType(AllTypeTest3.class, AllTypeTest4.class);
}

private void testExcep(Type f1, Type f2) {
OpenAPI swagger = new OpenAPI();
SwaggerUtils.resolveTypeSchemas(swagger, f1);
Expand All @@ -161,5 +183,13 @@ public void test_resolve_type_schemas_correct() {
// should be ObjectSchema but swagger is not.
// <pre> Assertions.assertTrue(schema instanceof ObjectSchema) </pre>
Assertions.assertEquals("object", schema.getType());

openAPI = new OpenAPI();
schema = SwaggerUtils.getSchema(openAPI, SwaggerUtils.resolveTypeSchemas(openAPI, TestTypeClass.class)); // resolve reference
Assertions.assertEquals("object", schema.getType());

openAPI = new OpenAPI();
schema = SwaggerUtils.getSchema(openAPI, SwaggerUtils.resolveTypeSchemas(openAPI, TestTypeRecord.class)); // resolve reference
Assertions.assertEquals("object", schema.getType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.swagger.generator.core.pojo;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(name = "YYY")
public class TestTypeClass {
@Schema(description = "language")
private TestTypeEnumLang testTypeEnumLang;

public TestTypeEnumLang getTestTypeEnumLang() {
return testTypeEnumLang;
}

public void setTestTypeEnumLang(TestTypeEnumLang testTypeEnumLang) {
this.testTypeEnumLang = testTypeEnumLang;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.swagger.generator.core.pojo;

public enum TestTypeEnumLang {
JAVA, CHINESE, UNKNOWN;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.swagger.generator.core.pojo;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(name = "YYY")
public record TestTypeRecord(
@Schema(description = "language") TestTypeEnumLang testTypeEnumLang) {
}
Loading