chore: Add objectParser for ObjectTypeAnnotation in Parse Server options (#9912)

This commit is contained in:
Mattia Faraci
2025-12-03 16:10:29 +01:00
committed by GitHub
parent 339b10c39b
commit f6ccef1d53
3 changed files with 171 additions and 9 deletions

View File

@@ -155,6 +155,8 @@ function mapperFor(elt, t) {
return wrap(t.identifier('objectParser'));
} else if (t.isBooleanTypeAnnotation(elt)) {
return wrap(t.identifier('booleanParser'));
} else if (t.isObjectTypeAnnotation(elt)) {
return wrap(t.identifier('objectParser'));
} else if (t.isGenericTypeAnnotation(elt)) {
const type = elt.typeAnnotation.id.name;
if (type == 'Adapter') {
@@ -372,6 +374,8 @@ This code has been generated by resources/buildConfigDefinitions.js
Do not edit manually, but update Options/index.js
`;
// Only run the transformation when executed directly, not when imported by tests
if (require.main === module) {
const babel = require('@babel/core');
const res = babel.transformFileSync('./src/Options/index.js', {
plugins: [plugin, '@babel/transform-flow-strip-types'],
@@ -381,3 +385,7 @@ const res = babel.transformFileSync('./src/Options/index.js', {
});
require('fs').writeFileSync('./src/Options/Definitions.js', res.code + '\n');
require('fs').writeFileSync('./src/Options/docs.js', docs);
}
// Export mapperFor for testing
module.exports = { mapperFor };

View File

@@ -0,0 +1,153 @@
const t = require('@babel/types');
const { mapperFor } = require('../resources/buildConfigDefinitions');
describe('buildConfigDefinitions', () => {
describe('mapperFor', () => {
it('should return objectParser for ObjectTypeAnnotation', () => {
const mockElement = {
type: 'ObjectTypeAnnotation',
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('objectParser');
});
it('should return objectParser for AnyTypeAnnotation', () => {
const mockElement = {
type: 'AnyTypeAnnotation',
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('objectParser');
});
it('should return arrayParser for ArrayTypeAnnotation', () => {
const mockElement = {
type: 'ArrayTypeAnnotation',
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('arrayParser');
});
it('should return booleanParser for BooleanTypeAnnotation', () => {
const mockElement = {
type: 'BooleanTypeAnnotation',
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('booleanParser');
});
it('should return numberParser call expression for NumberTypeAnnotation', () => {
const mockElement = {
type: 'NumberTypeAnnotation',
name: 'testNumber',
};
const result = mapperFor(mockElement, t);
expect(t.isCallExpression(result)).toBe(true);
expect(result.callee.property.name).toBe('numberParser');
expect(result.arguments[0].value).toBe('testNumber');
});
it('should return moduleOrObjectParser for Adapter GenericTypeAnnotation', () => {
const mockElement = {
type: 'GenericTypeAnnotation',
typeAnnotation: {
id: {
name: 'Adapter',
},
},
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('moduleOrObjectParser');
});
it('should return numberOrBooleanParser for NumberOrBoolean GenericTypeAnnotation', () => {
const mockElement = {
type: 'GenericTypeAnnotation',
typeAnnotation: {
id: {
name: 'NumberOrBoolean',
},
},
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('numberOrBooleanParser');
});
it('should return numberOrStringParser call expression for NumberOrString GenericTypeAnnotation', () => {
const mockElement = {
type: 'GenericTypeAnnotation',
name: 'testString',
typeAnnotation: {
id: {
name: 'NumberOrString',
},
},
};
const result = mapperFor(mockElement, t);
expect(t.isCallExpression(result)).toBe(true);
expect(result.callee.property.name).toBe('numberOrStringParser');
expect(result.arguments[0].value).toBe('testString');
});
it('should return arrayParser for StringOrStringArray GenericTypeAnnotation', () => {
const mockElement = {
type: 'GenericTypeAnnotation',
typeAnnotation: {
id: {
name: 'StringOrStringArray',
},
},
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('arrayParser');
});
it('should return objectParser for unknown GenericTypeAnnotation', () => {
const mockElement = {
type: 'GenericTypeAnnotation',
typeAnnotation: {
id: {
name: 'UnknownType',
},
},
};
const result = mapperFor(mockElement, t);
expect(t.isMemberExpression(result)).toBe(true);
expect(result.object.name).toBe('parsers');
expect(result.property.name).toBe('objectParser');
});
});
});

View File

@@ -111,6 +111,7 @@ module.exports.ParseServerOptions = {
env: 'PARSE_SERVER_AUTH_PROVIDERS',
help:
'Configuration for your authentication providers, as stringified JSON. See http://docs.parseplatform.org/parse-server/guide/#oauth-and-3rd-party-authentication',
action: parsers.objectParser,
},
cacheAdapter: {
env: 'PARSE_SERVER_CACHE_ADAPTER',