Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
65 changes: 34 additions & 31 deletions lib/codegen/fromcto/jsonschema/jsonschemavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ class JSONSchemaVisitor {
return jsonSchema;
}

/**
* Get the schema for the key or value type of a map declaration, the same way as for a
* field of that type: a scalar contributes its base type and validators, and a class
* declaration is referenced by its fully qualified name, from the namespace it is declared in.
* @param {Object} [declaration] - the declaration of the type, if it is not a primitive
* @param {String} typeName - the type name used in the map declaration
* @param {Object} parameters - the parameters
* @return {Object} the JSON schema for the map key or value
* @private
*/
getMapEntrySchema(declaration, typeName, parameters) {
if (declaration?.isClassDeclaration?.()) {
const refRoot = parameters.refRoot ? parameters.refRoot : '#/definitions';
return { $ref: `${refRoot}/${declaration.getFullyQualifiedName()}` };
}
if (declaration?.isScalarDeclaration?.()) {
const isScalarUUID = ModelUtil.removeNamespaceVersionFromFullyQualifiedName(declaration.getFullyQualifiedName()) === 'concerto.scalar.UUID';
return this.getFieldOrScalarDeclarationValidatorsForSchema(declaration.getType(), isScalarUUID, declaration.getValidator());
}
return this.getFieldOrScalarDeclarationValidatorsForSchema(typeName);
}

/**
* Returns true if the class declaration contains recursive references.
*
Expand Down Expand Up @@ -441,50 +463,31 @@ class JSONSchemaVisitor {
} 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 keyType = mapDeclaration.getKey().getType();
const valueType = mapDeclaration.getValue().getType();

// Do not set $id on inlined map schemas: inherited fields (e.g. Person.nextOfKin
// on Employee, Contractor, Manager) would share the same $id and break Ajv.
const jsonSchema = {
schema: {
title: mapDeclaration.getName(),
description : `An instance of ${field.getFullyQualifiedTypeName()}`,
type: 'object',
propertyNames: {
type: 'string'
},
additionalProperties: {
type: this.getFieldOrScalarDeclarationValidatorsForSchema(mapDeclaration.getValue().getType()).type
}
}
// The schema is returned as the property's schema itself, like every other field type.
const mapSchema = {
title: mapDeclaration.getName(),
description : `An instance of ${field.getFullyQualifiedTypeName()}`,
type: 'object',
propertyNames: this.getMapEntrySchema(mapDeclaration.getModelFile().getType(keyType), keyType, parameters),
additionalProperties: this.getMapEntrySchema(mapDeclaration.getModelFile().getType(valueType), valueType, parameters)
};

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

}

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()}`)}`};
}

const sizeValidator = field.getSizeValidator?.();
if (sizeValidator) {
if (sizeValidator.getMinSize() !== null) {
jsonSchema.schema.minProperties = sizeValidator.getMinSize();
mapSchema.minProperties = sizeValidator.getMinSize();
}
if (sizeValidator.getMaxSize() !== null) {
jsonSchema.schema.maxProperties = sizeValidator.getMaxSize();
mapSchema.maxProperties = sizeValidator.getMaxSize();
}
}

return jsonSchema;
return mapSchema;
// Not primitive, so must be a class or enumeration!
} else {
// Look up the type of the property.
Expand Down
Loading
Loading