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 all 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
55 changes: 44 additions & 11 deletions lib/codegen/fromcto/jsonschema/jsonschemavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ class JSONSchemaVisitor {

/**
* Get the validators for a field or a scalar definition in JSON schema form.
* @param {Object} field - the scalar declaration being visited
* @param {String} type - the field type
* @param {bool} [isScalarUUID] - flag to indicate given field type is scalar uuid
* @param {bool} [validator] - the field validator
* @return {Object} the result of visiting or null
* @private
*/
getFieldOrScalarDeclarationValidatorsForSchema(field, isScalarUUID = false) {
const validator = field.getValidator();
getFieldOrScalarDeclarationValidatorsForSchema(type, isScalarUUID = false, validator) {
let jsonSchema = {};

switch (field.getType()) {
switch (type) {
jonathan-casey marked this conversation as resolved.
Show resolved Hide resolved
case 'String':
jsonSchema.type = 'string';
if (isScalarUUID) {
Expand Down Expand Up @@ -117,6 +117,8 @@ class JSONSchemaVisitor {
case 'Boolean':
jsonSchema.type = 'boolean';
break;
default:
jsonSchema.type = type;
}

return jsonSchema;
Expand Down Expand Up @@ -354,7 +356,7 @@ class JSONSchemaVisitor {
debug('entering visitScalarDeclaration', scalarDeclaration.getName());
return {
$id: scalarDeclaration.getFullyQualifiedName(),
schema: this.getFieldOrScalarDeclarationValidatorsForSchema(scalarDeclaration)
schema: this.getFieldOrScalarDeclarationValidatorsForSchema(scalarDeclaration.getType(), false, scalarDeclaration.getValidator())
};
}

Expand All @@ -381,11 +383,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 @@ -398,7 +395,7 @@ class JSONSchemaVisitor {

jsonSchema = {
...jsonSchema,
...this.getFieldOrScalarDeclarationValidatorsForSchema(field, isScalarUUID)
...this.getFieldOrScalarDeclarationValidatorsForSchema(field.getType(), isScalarUUID, field.getValidator())
};

// If this field has a default value, add it.
Expand All @@ -411,6 +408,42 @@ 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 mapKey = mapDeclaration.getModelFile().getType(mapDeclaration.getKey().getType());
let mapValue = mapDeclaration.getModelFile().getType(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.getFieldOrScalarDeclarationValidatorsForSchema(mapDeclaration.getValue().getType()).type
}
}
};

if (mapKey.isScalarDeclaration?.() && mapKey.getValidator()?.getRegex()) {
jsonSchema.schema.propertyNames.pattern = String(mapKey.getValidator().getRegex());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add support for regex validators for scalars we should also support it for string properties.

Copy link
Member Author

@jonathan-casey jonathan-casey Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wasn't part of the original spec unfortunately. Supporting regex on Map elements of type string is a fundamental change at the parser level, and on through validation, etc. We should definitely add this as an enhancement at a later stage.

fyi @mttrbrts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be a misunderstanding here?

We support regexs where they are defined in the Scalar type, we don't support them inline in the Map definition (for either Strings or Scalar types).


}

if (mapValue.isScalarDeclaration?.() && mapValue.getValidator()?.getRegex()) {
jsonSchema.schema.additionalProperties.pattern = String(mapValue.getValidator().getRegex());
}

// if its a ClassDeclaration, add reference to its schema.
if (mapValue?.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
Loading
Loading