Skip to content

Commit

Permalink
#68 editor support improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
miho committed Jan 18, 2025
1 parent 166c715 commit b6168ba
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ private void addDefaultValueAndDescriptionAndConstraintIfAvailable(Property prop
addDefaultValue(property, propertySchema);
addDescription(property, propertySchema);
addConstraint(property, propertySchema);
addFormat(property, propertySchema);
addUniqueItems(property, propertySchema);
addInjections(property, propertySchema);
}

private void addDefaultValue(Property property, Map<String, Object> propertySchema) {
Expand Down Expand Up @@ -517,4 +520,41 @@ private void addConstraint(Property property, Map<String, Object> propertySchema
// ignore, not possible to get default value
}
}

private void addUniqueItems(Property property, Map<String, Object> propertySchema) {
try {
var uniqueItems = property.annotationByKey("vmf:jackson:schema:uniqueItems").get().getValue();
if (uniqueItems != null) {
propertySchema.put("uniqueItems", Boolean.parseBoolean(uniqueItems));
}
} catch (Exception e) {
// ignore, not possible to get default value
}
}

private void addInjections(Property property, Map<String, Object> propertySchema) {
try {
var injections = property.annotationByKey("vmf:jackson:schema:inject").get().getValue();
if (injections != null) {
System.out.println("!!!injections: " + injections);
// inject json into schema by parsing it and adding it to the schema
var injectionsMap = new ObjectMapper().readValue("{"+injections+"}", Map.class);
propertySchema.putAll(injectionsMap);
}
} catch (Exception e) {
// ignore, not possible to get default value
e.printStackTrace();
}
}

private void addFormat(Property property, Map<String, Object> propertySchema) {
try {
var format = property.annotationByKey("vmf:jackson:schema:format").get().getValue();
if (format != null) {
propertySchema.put("format", format);
}
} catch (Exception e) {
// ignore, not possible to get default value
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eu.mihosoft.vmf.jackson.test.type_formats01;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import eu.mihosoft.vmf.jackson.VMFJacksonModule;
import eu.mihosoft.vmf.jackson.VMFJsonSchemaGenerator;
import eu.mihosoft.vmf.jackson.test.simple.Address;
import eu.mihosoft.vmf.jackson.test.simple.Employee;
import eu.mihosoft.vmf.jackson.test.simple.MyModel;
import eu.mihosoft.vmf.jackson.test.simple.Person;
import org.junit.jupiter.api.Assertions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class FormatModelTest {
@org.junit.jupiter.api.Test
void testModel() {
ObjectMapper mapper = new ObjectMapper();

mapper.registerModule(VMFJacksonModule.newInstance(VMFJacksonModule.RUNTIME_TYPE.EXPERIMENTAL)
.withTypeAlias("pet", Pet.class.getName())
);

var writer = mapper.writerWithDefaultPrettyPrinter();

// generate schema
VMFJsonSchemaGenerator schemaGenerator =
VMFJsonSchemaGenerator.newInstance(VMFJacksonModule.RUNTIME_TYPE.EXPERIMENTAL)
.withTypeAlias("pet", Pet.class.getName());

var schema = schemaGenerator.generateSchemaAsString(FormatModel01.class);

System.out.println(schema);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package eu.mihosoft.vmf.jackson.test.type_formats01;

public enum PetType {
DOG, CAT, BIRD, FISH, UNKNOWN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package eu.mihosoft.vmf.jackson.test.type_formats01.vmfmodel;

import eu.mihosoft.vmf.core.*;

@ExternalType(pkgName = "eu.mihosoft.vmf.jackson.test.type_formats01")
interface PetType {
}

interface FormatModel01 {
@Contains(opposite = "model")
@Annotation(key = "vmf:jackson:schema:format", value = "table")
@Annotation(key = "vmf:jackson:schema:uniqueItems", value = "true")
Pet[] getPets();
}

interface Pet {
@DefaultValue("eu.mihosoft.vmf.jackson.test.type_formats01.PetType.UNKNOWN")
PetType getType();
String getName();
int getAge();
@Container(opposite = "pets")
FormatModel01 getModel();

@Annotation(key = "vmf:jackson:schema:format", value = "color")
@DefaultValue("\"#ffa500\"")
String getColor();

@Annotation(key = "vmf:jackson:schema:inject", value = "\"title\": \"My Title\"")
String getOtherProperty();
}


Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private void handleSchemaUpdate(String schema) {

webView.getEngine().executeScript("updateSchema('" + escapeJavaScript(schema) + "')");

if (value != null && !value.isEmpty() && !"\"\"".equals(value)) {
if (value != null && !value.isEmpty() && !"\"\"".equals(value) && !"\"set a schema\"".equals(value)) {
attemptValueMigration(value);
}
}
Expand Down

0 comments on commit b6168ba

Please sign in to comment.