Skip to content

Commit 8c61b5c

Browse files
Merge pull request #228 from littleli/swagger-apimodelproperty-datatype
Use @ApiModelProperty attribute dataType to override target type
2 parents dca079a + ac798c9 commit 8c61b5c

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/PropertyModel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public PropertyModel withComments(List<String> comments) {
6666
return new PropertyModel(name, type, optional, originalMember, pullProperties, comments);
6767
}
6868

69+
public PropertyModel withType(Type type) {
70+
return new PropertyModel(name, type, optional, originalMember, pullProperties, comments);
71+
}
72+
6973
@Override
7074
public String toString() {
7175
return "PropertyModel{" + "name=" + name + ", type=" + type + "}";

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/Swagger.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
package cz.habarta.typescript.generator.parser;
33

44
import cz.habarta.typescript.generator.util.Utils;
5+
56
import java.lang.annotation.Annotation;
67
import java.lang.reflect.AnnotatedElement;
78
import java.lang.reflect.Method;
9+
import java.lang.reflect.Type;
810
import java.util.*;
911

1012

@@ -93,7 +95,17 @@ private static PropertyModel enrichProperty(PropertyModel property) {
9395
final AnnotatedElement annotatedElement = (AnnotatedElement) property.getOriginalMember();
9496
final String comment = Utils.getAnnotationElementValue(annotatedElement, "io.swagger.annotations.ApiModelProperty", "value", String.class);
9597
final List<String> comments = comment != null && !comment.isEmpty() ? Arrays.asList(comment) : null;
96-
return property.withComments(Utils.concat(comments, property.getComments()));
98+
final PropertyModel propertyModel = property.withComments(Utils.concat(comments, property.getComments()));
99+
final String dataTypeString = Utils.getAnnotationElementValue(annotatedElement, "io.swagger.annotations.ApiModelProperty", "dataType", String.class);
100+
if (dataTypeString == null || dataTypeString.isEmpty()) {
101+
return propertyModel;
102+
}
103+
try {
104+
final Type type = Class.forName(dataTypeString);
105+
return propertyModel.withType(type);
106+
} catch (ClassNotFoundException | ClassCastException e) {
107+
return propertyModel;
108+
}
97109
} else {
98110
return property;
99111
}

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/SwaggerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public void testDocumentation() {
4444
Assert.assertTrue(output.contains("Not Found"));
4545
Assert.assertTrue(output.contains("Documentation for the bean."));
4646
Assert.assertTrue(output.contains("Documentation for property 1."));
47+
Assert.assertTrue(output.contains("property1: string"));
48+
}
49+
50+
@Test
51+
public void testDocumentationAndDataType() {
52+
final Settings settings = TestUtils.settings();
53+
settings.generateJaxrsApplicationInterface = true;
54+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(DocumentedApplication.class));
55+
Assert.assertTrue(output.contains("Documentation for operation 1."));
56+
Assert.assertTrue(output.contains("Bad Request"));
57+
Assert.assertTrue(output.contains("Not Found"));
58+
Assert.assertTrue(output.contains("Documentation for the bean."));
59+
Assert.assertTrue(output.contains("Documentation for property 2."));
60+
Assert.assertTrue(output.contains("property2: string")); // see if type was overridden from number to string
4761
}
4862

4963
private static class TestApplication extends Application {
@@ -128,6 +142,13 @@ private static class DocumentedBean {
128142

129143
@ApiModelProperty("Documentation for property 1.")
130144
public String property1;
145+
146+
/**
147+
* Sometimes custom serializers are used. In such cases target type is overridden explicitly
148+
* with dataType annotation attribute.
149+
*/
150+
@ApiModelProperty(value = "Documentation for property 2.", dataType = "java.lang.String")
151+
public Long property2;
131152
}
132153

133154
}

0 commit comments

Comments
 (0)