Skip to content

Commit bb0e963

Browse files
Read-only and write-only properties (Jackson2, JSDoc tags) (#579)
1 parent 4b7d70b commit bb0e963

File tree

17 files changed

+454
-69
lines changed

17 files changed

+454
-69
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class Settings {
9090
private Predicate<String> mapClassesAsClassesFilter = null;
9191
public boolean generateConstructors = false;
9292
public boolean disableTaggedUnions = false;
93+
public boolean generateReadonlyAndWriteonlyJSDocTags = false;
9394
public boolean ignoreSwaggerAnnotations = false;
9495
public boolean generateJaxrsApplicationInterface = false;
9596
public boolean generateJaxrsApplicationClient = false;

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import cz.habarta.typescript.generator.parser.MethodParameterModel;
4848
import cz.habarta.typescript.generator.parser.Model;
4949
import cz.habarta.typescript.generator.parser.PathTemplate;
50+
import cz.habarta.typescript.generator.parser.PropertyAccess;
5051
import cz.habarta.typescript.generator.parser.PropertyModel;
5152
import cz.habarta.typescript.generator.parser.RestApplicationModel;
5253
import cz.habarta.typescript.generator.parser.RestMethodModel;
@@ -215,7 +216,7 @@ private static TsModel applyExtensionTransformers(SymbolTable symbolTable, TsMod
215216

216217
public TsType javaToTypeScript(Type type) {
217218
final BeanModel beanModel = new BeanModel(Object.class, Object.class, null, null, null, Collections.<Type>emptyList(),
218-
Collections.singletonList(new PropertyModel("property", type, false, null, null, null, null)), null);
219+
Collections.singletonList(new PropertyModel("property", type, false, null, null, null, null, null)), null);
219220
final Model model = new Model(Collections.singletonList(beanModel), Collections.<EnumModel>emptyList(), null);
220221
final TsModel tsModel = javaToTypeScript(model);
221222
return tsModel.getBeans().get(0).getProperties().get(0).getTsType();
@@ -386,7 +387,18 @@ private TsPropertyModel processProperty(SymbolTable symbolTable, BeanModel bean,
386387
final TsType type = typeFromJava(symbolTable, property.getType(), property.getContext(), property.getName(), bean.getOrigin());
387388
final TsType tsType = property.isOptional() ? type.optional() : type;
388389
final TsModifierFlags modifiers = TsModifierFlags.None.setReadonly(settings.declarePropertiesAsReadOnly);
389-
return new TsPropertyModel(prefix + property.getName() + suffix, tsType, modifiers, /*ownProperty*/ false, property.getComments());
390+
final List<String> comments = settings.generateReadonlyAndWriteonlyJSDocTags
391+
? Utils.concat(property.getComments(), getPropertyAccessComments(property.getAccess()))
392+
: property.getComments();
393+
return new TsPropertyModel(prefix + property.getName() + suffix, tsType, modifiers, /*ownProperty*/ false, comments);
394+
}
395+
396+
private static List<String> getPropertyAccessComments(PropertyAccess access) {
397+
final String accessTag =
398+
access == PropertyAccess.ReadOnly ? "@readonly" :
399+
access == PropertyAccess.WriteOnly ? "@writeonly" :
400+
null;
401+
return accessTag != null ? Collections.singletonList(accessTag) : null;
390402
}
391403

392404
private TsEnumModel processEnum(SymbolTable symbolTable, EnumModel enumModel) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private BeanModel parseBean(SourceType<Class<?>> sourceClass) {
9292
if (serializedName != null) {
9393
name = serializedName.value();
9494
}
95-
properties.add(new PropertyModel(name, field.getGenericType(), false, field, null, null, null));
95+
properties.add(new PropertyModel(name, field.getGenericType(), false, null, field, null, null, null));
9696
addBeanToQueue(new SourceType<>(field.getGenericType(), sourceClass.type, name));
9797
}
9898
cls = cls.getSuperclass();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ private BeanModel parseBean(SourceType<Class<?>> sourceClass) {
8989
continue;
9090
}
9191
final boolean optional = isPropertyOptional(propertyMember);
92-
properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, null, optional, sourceClass.type, member, null, null));
92+
properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, null, optional, null, sourceClass.type, member, null, null));
9393
}
9494
}
9595

9696
final JsonTypeInfo jsonTypeInfo = sourceClass.type.getAnnotation(JsonTypeInfo.class);
9797
if (jsonTypeInfo != null && jsonTypeInfo.include() == JsonTypeInfo.As.PROPERTY) {
9898
if (!containsProperty(properties, jsonTypeInfo.property())) {
99-
properties.add(new PropertyModel(jsonTypeInfo.property(), String.class, false, null, null, null, null));
99+
properties.add(new PropertyModel(jsonTypeInfo.property(), String.class, false, null, null, null, null, null));
100100
}
101101
}
102102

0 commit comments

Comments
 (0)