Skip to content

Commit

Permalink
feat(map): Codegen for JsonSchema Target (#63)
Browse files Browse the repository at this point in the history
* feat(map): add map support for jsonschemavisitor

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): test cases

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): update snapshot

Signed-off-by: Jonathan Casey <[email protected]>

* feat(map): add type defs

Signed-off-by: Jonathan Casey <[email protected]>

* feat(map): remove redundant property from additionalProperties

Signed-off-by: Jonathan Casey <[email protected]>

* feat(map): use getFieldOrScalarDeclarationValidatorsForSchema to get the type

Signed-off-by: Jonathan Casey <[email protected]>

* feat(map): Codegen for GraphQL Target (#66)

* feat(map): add graphqlvisitor

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): update snapshot

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): adds graphqlvisitor

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): restores sandbox

Signed-off-by: Jonathan Casey <[email protected]>

---------

Signed-off-by: Jonathan Casey <[email protected]>

* feat!(codegen): remove loopback src (#59)

feat(codegen): remove loopback src

Signed-off-by: Jonathan Casey <[email protected]>

* chore(deps-dev): bump @babel/traverse from 7.22.11 to 7.23.2 (#67)

Bumps [@babel/traverse](https://github.com/babel/babel/tree/HEAD/packages/babel-traverse) from 7.22.11 to 7.23.2.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.23.2/packages/babel-traverse)

---
updated-dependencies:
- dependency-name: "@babel/traverse"
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* test(codegen) update HR model to use multiple namespaces (#68)

Signed-off-by: Dan Selman <[email protected]>

* test(*): snapshot update

Signed-off-by: Jonathan Casey <[email protected]>

* feat(*): gets type from model file

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): snapshot update

Signed-off-by: Jonathan Casey <[email protected]>

* feat(*): use scalar validator regex as pattern value

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): add regex to property

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): update snapshot

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): update jsdoc and typedef

Signed-off-by: Jonathan Casey <[email protected]>

---------

Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Dan Selman <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Dan Selman <[email protected]>
  • Loading branch information
3 people authored Nov 16, 2023
1 parent 01bb536 commit ee9117c
Show file tree
Hide file tree
Showing 5 changed files with 680 additions and 17 deletions.
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) {
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());

}

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

0 comments on commit ee9117c

Please sign in to comment.