Skip to content

Commit

Permalink
Updated the proto2jsonschema tool to include the examples field (#111)
Browse files Browse the repository at this point in the history
* Updated the proto2jsonschema tool to include examples field in the generated JSON schema

* Minor fixes

* Minor fixes

* Minor fixes
  • Loading branch information
chenyumic authored Dec 7, 2020
1 parent 59a5dda commit f61283b
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jsonschema/google/events/cloud/audit/v1/LogEntryData.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"google.cloud.audit.log.v1.written"
],
"product": "Cloud Audit Logs",
"examples": [
"https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/audit/v1/LogEntryData-pubsubCreateTopic.json"
],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"logName": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"google.cloud.cloudbuild.build.v1.statusChanged"
],
"product": "Cloud Build",
"examples": [],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"google.cloud.firestore.document.v1.written"
],
"product": "Cloud Firestore",
"examples": [
"https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/firestore/v1/DocumentEventData-complex.json",
"https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/firestore/v1/DocumentEventData-simple.json"
],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"value": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"google.cloud.pubsub.topic.v1.messagePublished"
],
"product": "Cloud Pub/Sub",
"examples": [
"https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/pubsub/v1/MessagePublishedData-binary.json",
"https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/pubsub/v1/MessagePublishedData-text.json"
],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"message": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"google.cloud.scheduler.job.v1.executed"
],
"product": "Cloud Scheduler",
"examples": [],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"customData": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"google.cloud.storage.object.v1.metadataUpdated"
],
"product": "Cloud Storage",
"examples": [
"https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/storage/v1/StorageObjectData-simple.json"
],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"contentEncoding": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"google.firebase.analytics.log.v1.written"
],
"product": "Google Analytics for Firebase",
"examples": [],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"userDim": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"google.firebase.auth.user.v1.deleted"
],
"product": "Firebase Authentication",
"examples": [],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"uid": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"google.firebase.database.ref.v1.written"
],
"product": "Firebase Realtime Database",
"examples": [],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"data": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"google.firebase.remoteconfig.remoteConfig.v1.updated"
],
"product": "Firebase Remote Config",
"examples": [],
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"versionNumber": {
Expand Down
30 changes: 30 additions & 0 deletions tools/proto2jsonschema/postgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ const flatten = require('flat');
* This tool polishes the JSON schemas with a few modifications:
* - Adds "$id" – "." delimited ID – e.g. "google.events.cloud.audit.v1.LogEntryData"
* - Adds "name" – The name for the JSON schema – e.g. "LogEntryData"
* - Adds "examples" - A list of paths to the test event data associated with the schema
* - e.g. ["https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/audit/v1/LogEntryData-pubsubCreateTopic.json"]
* - Removes snake_case fields that are duplicative of camelCase fields that are seen in JSON payloads
* @todo Removing snake_case fields is a feature request for the proto2jsonschema generator.
* @see https://github.com/chrusty/protoc-gen-jsonschema/issues/38
*/
const ROOT = path.resolve(`${__dirname}/../../jsonschema`);
const TESTDATA = path.resolve(`${__dirname}/../../testdata`);
console.log(`Fixing paths in dir: ${ROOT}`);
(async () => {
const filePaths = await recursive(ROOT);
Expand All @@ -29,6 +32,7 @@ console.log(`Fixing paths in dir: ${ROOT}`);
// Add the $id and name first
$id: getId(filePath),
name: dataName,
examples: getExamples(filePath),
package: packageName,
datatype: `${packageName}.${dataName}`,
...getCloudEventProperties(packageName),
Expand Down Expand Up @@ -138,6 +142,32 @@ function getCloudEventPackage(filepath) {
return removeSuffix.replace(/\//g, '.');
}

/**
* Gets the paths to test event data associated with the schema.
* @param {string} filepath The input file path
* @example filepath: /Documents/github/googleapis/google-cloudevents/jsonschema/google/events/cloud/audit/v1/LogEntryData.json
* @example out: ["https://googleapis.github.io/google-cloudevents/testdata/google/events/cloud/audit/v1/LogEntryData-pubsubCreateTopic.json"]
* @returns {array[string]} An array of paths to the test event data.
*/
function getExamples(filepath) {
const removePrefix = filepath.split('jsonschema/')[1];
const removeSuffix = removePrefix.substring(0, removePrefix.lastIndexOf("/"));
const testDataPath = TESTDATA + '/' + removeSuffix;
var filesAndDirs;
try {
filesAndDirs = fs.readdirSync(testDataPath, { withFileTypes: true });
} catch (err) {
return [];
}

return filesAndDirs
.map((value) => {
const isJSONFile = value.isFile() && value.name.endsWith('.json');
return isJSONFile ? `https://googleapis.github.io/google-cloudevents/testdata/${removeSuffix}/${value.name}` : null;
})
.filter((value) => !value);
}

/**
* Gets the CloudEvent properties from the corresponding `events.proto` file.
* @param {string} packageName The package name of the CloudEvent.
Expand Down

0 comments on commit f61283b

Please sign in to comment.