Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(map): Codegen for JsonSchema Target #63

Merged
merged 19 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions lib/codegen/fromcto/jsonschema/jsonschemavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,6 @@ class JSONSchemaVisitor {
visitField(field, parameters, isScalarUUID = false) {
debug('entering visitField', field.getName());


if (ModelUtil.isMap?.(field)) {
return;
}

// Is this a primitive typed property?
let jsonSchema;
if (field.isPrimitive()) {
Expand All @@ -411,6 +406,34 @@ class JSONSchemaVisitor {
jsonSchema.description = 'The instance identifier for this type';
}

} else if (ModelUtil.isMap?.(field)) {
const mapDeclaration = field.getParent().getModelFile().getModelManager().getType(field.getFullyQualifiedTypeName());

let valueDeclaration = mapDeclaration.getModelFile()
jonathan-casey marked this conversation as resolved.
Show resolved Hide resolved
.getAllDeclarations()
.find(decl => decl.name === mapDeclaration.getValue().getType());

const jsonSchema = {
$id: field.getFullyQualifiedName(),
schema: {
title: mapDeclaration.getName(),
description : `An instance of ${field.getFullyQualifiedTypeName()}`,
type: 'object',
propertyNames: {
type: 'string'
},
additionalProperties: {
type: this.toJsonType(mapDeclaration.getValue().getType())
}
}
};

// if its a ClassDeclaration, add reference to its schema.
if (valueDeclaration?.isClassDeclaration()) {
jsonSchema.schema.additionalProperties.$ref = `#/definitions/${field.getParent().getModelFile().getNamespace().concat(`.${mapDeclaration.getValue().getType()}`)}`;
}

return jsonSchema;
// Not primitive, so must be a class or enumeration!
} else {
// Look up the type of the property.
Expand Down Expand Up @@ -546,6 +569,29 @@ class JSONSchemaVisitor {
// Return the schema.
return jsonSchema;
}

/**
* Converts a Concerto type to a JSON type. Primitive types are converted
* everything else is passed through unchanged.
* @param {string} type - the concerto type
* @return {string} the corresponding type in Json
* @private
*/
toJsonType(type) {
switch (type) {
case 'Boolean':
return 'boolean';
case 'String':
case 'DateTime':
return 'string';
case 'Double':
case 'Long':
case 'Integer':
return 'number';
default:
return type;
}
jonathan-casey marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = JSONSchemaVisitor;
Loading
Loading