Skip to content

Commit

Permalink
Read-only and write-only properties (Jackson2, JSDoc tags) (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechhabarta authored Nov 17, 2020
1 parent 4b7d70b commit bb0e963
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public class Settings {
private Predicate<String> mapClassesAsClassesFilter = null;
public boolean generateConstructors = false;
public boolean disableTaggedUnions = false;
public boolean generateReadonlyAndWriteonlyJSDocTags = false;
public boolean ignoreSwaggerAnnotations = false;
public boolean generateJaxrsApplicationInterface = false;
public boolean generateJaxrsApplicationClient = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import cz.habarta.typescript.generator.parser.MethodParameterModel;
import cz.habarta.typescript.generator.parser.Model;
import cz.habarta.typescript.generator.parser.PathTemplate;
import cz.habarta.typescript.generator.parser.PropertyAccess;
import cz.habarta.typescript.generator.parser.PropertyModel;
import cz.habarta.typescript.generator.parser.RestApplicationModel;
import cz.habarta.typescript.generator.parser.RestMethodModel;
Expand Down Expand Up @@ -215,7 +216,7 @@ private static TsModel applyExtensionTransformers(SymbolTable symbolTable, TsMod

public TsType javaToTypeScript(Type type) {
final BeanModel beanModel = new BeanModel(Object.class, Object.class, null, null, null, Collections.<Type>emptyList(),
Collections.singletonList(new PropertyModel("property", type, false, null, null, null, null)), null);
Collections.singletonList(new PropertyModel("property", type, false, null, null, null, null, null)), null);
final Model model = new Model(Collections.singletonList(beanModel), Collections.<EnumModel>emptyList(), null);
final TsModel tsModel = javaToTypeScript(model);
return tsModel.getBeans().get(0).getProperties().get(0).getTsType();
Expand Down Expand Up @@ -386,7 +387,18 @@ private TsPropertyModel processProperty(SymbolTable symbolTable, BeanModel bean,
final TsType type = typeFromJava(symbolTable, property.getType(), property.getContext(), property.getName(), bean.getOrigin());
final TsType tsType = property.isOptional() ? type.optional() : type;
final TsModifierFlags modifiers = TsModifierFlags.None.setReadonly(settings.declarePropertiesAsReadOnly);
return new TsPropertyModel(prefix + property.getName() + suffix, tsType, modifiers, /*ownProperty*/ false, property.getComments());
final List<String> comments = settings.generateReadonlyAndWriteonlyJSDocTags
? Utils.concat(property.getComments(), getPropertyAccessComments(property.getAccess()))
: property.getComments();
return new TsPropertyModel(prefix + property.getName() + suffix, tsType, modifiers, /*ownProperty*/ false, comments);
}

private static List<String> getPropertyAccessComments(PropertyAccess access) {
final String accessTag =
access == PropertyAccess.ReadOnly ? "@readonly" :
access == PropertyAccess.WriteOnly ? "@writeonly" :
null;
return accessTag != null ? Collections.singletonList(accessTag) : null;
}

private TsEnumModel processEnum(SymbolTable symbolTable, EnumModel enumModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private BeanModel parseBean(SourceType<Class<?>> sourceClass) {
if (serializedName != null) {
name = serializedName.value();
}
properties.add(new PropertyModel(name, field.getGenericType(), false, field, null, null, null));
properties.add(new PropertyModel(name, field.getGenericType(), false, null, field, null, null, null));
addBeanToQueue(new SourceType<>(field.getGenericType(), sourceClass.type, name));
}
cls = cls.getSuperclass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ private BeanModel parseBean(SourceType<Class<?>> sourceClass) {
continue;
}
final boolean optional = isPropertyOptional(propertyMember);
properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, null, optional, sourceClass.type, member, null, null));
properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, null, optional, null, sourceClass.type, member, null, null));
}
}

final JsonTypeInfo jsonTypeInfo = sourceClass.type.getAnnotation(JsonTypeInfo.class);
if (jsonTypeInfo != null && jsonTypeInfo.include() == JsonTypeInfo.As.PROPERTY) {
if (!containsProperty(properties, jsonTypeInfo.property())) {
properties.add(new PropertyModel(jsonTypeInfo.property(), String.class, false, null, null, null, null));
properties.add(new PropertyModel(jsonTypeInfo.property(), String.class, false, null, null, null, null, null));
}
}

Expand Down
Loading

0 comments on commit bb0e963

Please sign in to comment.