Skip to content

Commit 1880b94

Browse files
authored
Merge pull request #148 from spdx/issue144
Update OWL schema to include deprecated
2 parents 41da8a9 + 665d377 commit 1880b94

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

src/main/java/org/spdx/tools/schema/OwlToJsonSchema.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class OwlToJsonSchema extends AbstractOwlRdfConverter {
6565
private static final String JSON_RESTRICTION_MIN_ITEMS = "minItems";
6666
private static final String JSON_RESTRICTION_MAXITEMS = "maxItems";
6767

68-
private static final String SCHEMA_VERSION_URI = "http://json-schema.org/draft-07/schema#";
68+
private static final String SCHEMA_VERSION_URI = "https://json-schema.org/draft/2019-09/schema#";
6969
private static final String RELATIONSHIP_TYPE = SpdxConstants.SPDX_NAMESPACE + SpdxConstants.CLASS_RELATIONSHIP;
7070
static ObjectMapper jsonMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
7171
private static final Set<String> USES_SPDXIDS;
@@ -91,10 +91,11 @@ public ObjectNode convertToJsonSchema() {
9191
ObjectNode root = jsonMapper.createObjectNode();
9292
root.put("$schema", SCHEMA_VERSION_URI);
9393
ExtendedIterator<Ontology> ontologyIter = model.listOntologies();
94+
String version = null;
9495
if (ontologyIter.hasNext()) {
9596
Ontology ont = ontologyIter.next();
9697
if (ont.isURIResource()) {
97-
String version = ont.getVersionInfo();
98+
version = ont.getVersionInfo();
9899
String ontologyUri = version == null ? ont.getURI() : ont.getURI() + "/" + version;
99100
if (Objects.nonNull(ontologyUri)) {
100101
root.put("$id", ontologyUri);
@@ -108,15 +109,28 @@ public ObjectNode convertToJsonSchema() {
108109
root.put(JSON_RESTRICTION_TYPE,JSON_TYPE_OBJECT);
109110
ObjectNode properties = jsonMapper.createObjectNode();
110111
ArrayNode required = jsonMapper.createArrayNode();
112+
ObjectNode schemaProp = jsonMapper.createObjectNode();
113+
schemaProp.put(JSON_RESTRICTION_TYPE, JSON_TYPE_STRING);
114+
String schemaRefDescription = "Reference the SPDX ";
115+
if (Objects.nonNull(version)) {
116+
schemaRefDescription = schemaRefDescription + "version " + version + " ";
117+
}
118+
schemaRefDescription = schemaRefDescription + "JSON Schema.";
119+
schemaProp.put("description", schemaRefDescription);
120+
properties.set("$schema", schemaProp);
121+
111122
OntClass docClass = model.getOntClass(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.CLASS_SPDX_DOCUMENT);
112123
Objects.requireNonNull(docClass, "Missing SpdxDocument class in OWL document");
113124
addClassProperties(docClass, properties, required);
114125
// Add in the extra properties
115126
properties.set(SpdxConstants.PROP_DOCUMENT_NAMESPACE, createSimpleTypeSchema(JSON_TYPE_STRING,
116127
"The URI provides an unambiguous mechanism for other SPDX documents to reference SPDX elements within this SPDX document."));
117128
required.add(SpdxConstants.PROP_DOCUMENT_NAMESPACE);
118-
properties.set(SpdxConstants.PROP_DOCUMENT_DESCRIBES, toArraySchema(createSimpleTypeSchema(JSON_TYPE_STRING, "SPDX ID for each Package, File, or Snippet."),
119-
"Packages, files and/or Snippets described by this SPDX document", 0));
129+
ObjectNode describesProperty = toArraySchema(createSimpleTypeSchema(JSON_TYPE_STRING, "SPDX ID for each Package, File, or Snippet."),
130+
"DEPRECATED: use relationships instead of this field. Packages, files and/or Snippets described by this SPDX document", 0);
131+
describesProperty.put("deprecated", true);
132+
describesProperty.put("$comment", "This field has been deprecated as it is a duplicate of using the SPDXRef-DOCUMENT DESCRIBES relationship");
133+
properties.set(SpdxConstants.PROP_DOCUMENT_DESCRIBES, describesProperty);
120134

121135
OntClass packageClass = model.getOntClass(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.CLASS_SPDX_PACKAGE);
122136
Objects.requireNonNull(packageClass, "Missing SPDX Package class in OWL document");
@@ -153,7 +167,7 @@ private JsonNode toArrayPropertySchema(OntClass ontClass, int min) {
153167
* @param min Minimum number of elements for the array
154168
* @return JSON Schema of an array of item types
155169
*/
156-
private JsonNode toArraySchema(ObjectNode itemSchema, String description, int min) {
170+
private ObjectNode toArraySchema(ObjectNode itemSchema, String description, int min) {
157171
ObjectNode property = jsonMapper.createObjectNode();
158172
property.put("description", description);
159173
property.put(JSON_RESTRICTION_TYPE, JSON_TYPE_ARRAY);
@@ -223,21 +237,22 @@ private void addClassProperties(OntClass spdxClass, ObjectNode jsonSchemaPropert
223237
}
224238
Collection<OntProperty> ontProperties = propertiesFromClassRestrictions(spdxClass);
225239
for (OntProperty property:ontProperties) {
240+
String propName = property.getLocalName();
226241
if (SKIPPED_PROPERTIES.contains(property.getURI())) {
227242
continue;
228243
}
229244
PropertyRestrictions restrictions = getPropertyRestrictions(spdxClass, property);
230-
Objects.requireNonNull(restrictions.getTypeUri(), "Missing type for property "+property.getLocalName());
245+
Objects.requireNonNull(restrictions.getTypeUri(), "Missing type for property "+propName);
231246
if (restrictions.getTypeUri().equals(RELATIONSHIP_TYPE)) {
232247
continue;
233248
}
234249
if (restrictions.isListProperty()) {
235250
jsonSchemaProperties.set(MultiFormatStore.propertyNameToCollectionPropertyName(
236-
checkConvertRenamedPropertyName(property.getLocalName())),
251+
checkConvertRenamedPropertyName(propName)),
237252
deriveListPropertySchema(property, restrictions));
238253
if (!restrictions.isOptional() || restrictions.getMinCardinality() > 0) {
239254
required.add(MultiFormatStore.propertyNameToCollectionPropertyName(
240-
checkConvertRenamedPropertyName(property.getLocalName())));
255+
checkConvertRenamedPropertyName(propName)));
241256
}
242257
} else {
243258
jsonSchemaProperties.set(checkConvertRenamedPropertyName(property.getLocalName()), derivePropertySchema(property, restrictions));
@@ -264,6 +279,16 @@ private ObjectNode deriveListPropertySchema(OntProperty property, PropertyRestri
264279
addCardinalityRestrictions(propertySchema, restrictions);
265280
propertySchema.put(JSON_RESTRICTION_TYPE, JSON_TYPE_ARRAY);
266281
propertySchema.set(JSON_RESTRICTION_ITEMS, derivePropertySchema(property, restrictions));
282+
// Check for deprecated properties
283+
String propName = property.getLocalName();
284+
if ("hasFile".equals(propName)) {
285+
// Add deprecated information
286+
propertySchema.put("description", "DEPRECATED: use relationships instead of this field. Indicates that a particular file belongs to a package.");
287+
propertySchema.put("deprecated", true);
288+
propertySchema.put("$comment", "This field has been deprecated as it is a duplicate of using SPDXRef-<package-id> CONTAINS SPDXRef-<file-id> relationships");
289+
} else if ("reviewed".equals(propName) || "fileDependency".equals(propName)) {
290+
propertySchema.put("deprecated", true);
291+
}
267292
return propertySchema;
268293
}
269294

0 commit comments

Comments
 (0)