Skip to content

Commit

Permalink
Fix to include item types in array properties; support entries for pr…
Browse files Browse the repository at this point in the history
…operties; reorganize table output to new format
  • Loading branch information
jimmarino committed Dec 13, 2024
1 parent dfe4ee2 commit 613d60b
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.eclipse.dsp.generation.jsom.ElementDefinition.Type.CONSTANT;
import static org.eclipse.dsp.generation.jsom.ElementDefinition.Type.REFERENCE;
import static org.eclipse.dsp.generation.jsom.JsonSchemaKeywords.ALL_OF;
import static org.eclipse.dsp.generation.jsom.JsonSchemaKeywords.COMMENT;
import static org.eclipse.dsp.generation.jsom.JsonSchemaKeywords.CONST;
import static org.eclipse.dsp.generation.jsom.JsonSchemaKeywords.CONTAINS;
import static org.eclipse.dsp.generation.jsom.JsonSchemaKeywords.DEFINITIONS;
Expand Down Expand Up @@ -211,25 +212,26 @@ private void parseRequired(Map<String, Object> definition, SchemaType schemaType
@SuppressWarnings("unchecked")
private SchemaProperty parseProperty(String name, Map<String, Object> value) {
var type = value.get(TYPE);
var comment = value.containsKey(COMMENT) ? value.get(COMMENT).toString() : "";
if (type == null) {
var ref = value.get(REF);
if (ref != null) {
return SchemaProperty.Builder.newInstance()
.name(name)
.types(Set.of((String) ref))
.description("")
.description(comment)
.build();
}
return SchemaProperty.Builder.newInstance()
.name(name)
.types(Set.of(ANY.getName()))
.description("")
.description(comment)
.build();
} else if (ARRAY.getBaseType().equals(type)) {
var property = SchemaProperty.Builder.newInstance()
.name(name)
.types(Set.of((String) type))
.description("");
.description(comment);
var items = value.get(ITEMS);
if (items instanceof Map) {
property.itemTypes(parseElementDefinition((Map<String, Object>) items));
Expand All @@ -239,7 +241,7 @@ private SchemaProperty parseProperty(String name, Map<String, Object> value) {
var builder = SchemaProperty.Builder.newInstance()
.name(name)
.types(Set.of((String) type))
.description("");
.description(comment);
var constantValue = value.get(CONST);
if (constantValue != null) {
builder.constantValue(constantValue.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface JsonSchemaKeywords {
String REF = "$ref";
String ALL_OF = "allOf";
String ANY_OF = "anyOf";
String COMMENT = "$comment";
String CONST = "const";
String CONTAINS = "contains";
String DEFINITIONS = "definitions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public class SchemaProperty implements Comparable<SchemaProperty> {
private final Set<SchemaType> resolvedTypes = new TreeSet<>();
private final Set<ElementDefinition> itemTypes = new TreeSet<>();

private SchemaProperty() {
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -81,6 +78,9 @@ public String toString() {
return b.toString();
}

private SchemaProperty() {
}

public static final class Builder {
private final SchemaProperty property;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class SchemaType implements Comparable<SchemaType> {
* Ctor for base Json types.
*/
public SchemaType(String name) {
this(name, "any", JSON_BASE_URI);
this(name, name, JSON_BASE_URI);
this.jsonBaseType = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ public class HtmlTableTransformer implements SchemaTypeTransformer<String> {
@NotNull
public String transform(SchemaType schemaType) {
var builder = new StringBuilder(CSS).append("<table class=\"message-table\">");
builder.append(format("<tr><td class=\"message-class\" colspan=\"3\">%s</td></tr>", schemaType.getName()));
transformProperties("Required", schemaType.getTransitiveRequiredProperties(), builder);
transformProperties("Optional", schemaType.getTransitiveOptionalProperties(), builder);
builder.append(format("<tr><td class=\"message-class\" colspan=\"4\">%s</td></tr>", schemaType.getName()));
transformProperties(schemaType.getTransitiveRequiredProperties(), true, builder);
transformProperties(schemaType.getTransitiveOptionalProperties(), false, builder);
return builder.append("</table>").toString();
}

private void transformProperties(String title, Set<SchemaPropertyReference> references, StringBuilder builder) {
private void transformProperties(Set<SchemaPropertyReference> references, boolean required, StringBuilder builder) {
if (!references.isEmpty()) {
builder.append(format("<tr><td class=\"message-properties-heading\" colspan=\"3\">%s properties</td></tr>", title));
references.forEach(propertyReference -> transformProperty(propertyReference, builder));
references.forEach(propertyReference -> transformProperty(propertyReference, required, builder));
}
}

private void transformProperty(SchemaPropertyReference propertyReference, StringBuilder builder) {
private void transformProperty(SchemaPropertyReference propertyReference, boolean required, StringBuilder builder) {
builder.append("<tr>");
builder.append(format("<td class=\"code\">%s</td>", propertyReference.getName()));
builder.append("<td>").append(required ? "required" : "optional").append("</td>");
var resolvedProperty = propertyReference.getResolvedProperty();
if (resolvedProperty != null) {
String resolvedTypes = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void verifyParse() throws JsonProcessingException {
assertThat(agreement.getTransitiveRequiredProperties().size()).isEqualTo(5);
assertThat(agreement.getTransitiveOptionalProperties().size()).isEqualTo(4);

assertThat(agreement.getRequiredProperties())
.filteredOn(p -> p.getName().equals("assignee"))
.first()
.extracting(p -> p.getResolvedProperty().getDescription())
.isEqualTo("The dataset consumer");

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Metaform Systems, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Metaform Systems, Inc. - initial API and implementation
*
*/

package org.eclipse.dsp.generation.jsom;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class JsonTypesTest {

@Test
void verifyTypes() {
assertThat(JsonTypes.ANY.getBaseType()).isEqualTo("any");
assertThat(JsonTypes.ARRAY.getBaseType()).isEqualTo("array");
assertThat(JsonTypes.BOOLEAN.getBaseType()).isEqualTo("boolean");
assertThat(JsonTypes.INTEGER.getBaseType()).isEqualTo("integer");
assertThat(JsonTypes.NULL.getBaseType()).isEqualTo("null");
assertThat(JsonTypes.NUMBER.getBaseType()).isEqualTo("number");
assertThat(JsonTypes.OBJECT.getBaseType()).isEqualTo("object");
assertThat(JsonTypes.STRING.getBaseType()).isEqualTo("string");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ public interface TestSchema {
"type": "string"
},
"assigner": {
"$comment" :"The dataset provider",
"type": "string"
},
"assignee": {
"$comment" :"The dataset consumer",
"type": "string"
},
"timestamp": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class HtmlTableTransformerTest {

@Test
void verifyTransform() {
var barType = new SchemaType("Bar");
var fooType = new SchemaType("Foo");
var barType = new SchemaType("Bar", "object", "https://test.com/foo");
var fooType = new SchemaType("Foo", "object", "https://test.com/foo");

var prop1 = SchemaProperty.Builder.newInstance()
.name("prop1")
Expand Down Expand Up @@ -68,9 +68,7 @@ void verifyTransform() {

var result = transformer.transform(fooType);

assertThat(result.contains("<td class=\"message-class\" colspan=\"3\">Foo</td><")).isTrue(); // verify type name
assertThat(result.contains("Required properties</td>")).isTrue(); // verify required properties
assertThat(result.contains("Optional properties</td>")).isTrue(); // verify optional properties
assertThat(result.contains("<td class=\"message-class\" colspan=\"4\">Foo</td><")).isTrue(); // verify type name
assertThat(result.contains("<td>string</td>")).isTrue(); // verify property type names are included
assertThat(result.contains("array[Bar]")).isTrue(); // verify array type names are included
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
]
},
"callbackAddress": {
"type": "string"
"type": "string",
"$comment": "A URL indicating where messages to the consumer should be sent"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* SAP SE - extension to catalog request message
* SAP SE - extension to catalog request message
*
*/

Expand Down

0 comments on commit 613d60b

Please sign in to comment.