Skip to content

Commit

Permalink
Fix embedded messages (#280)
Browse files Browse the repository at this point in the history
* Handle readonly and writeonly for all versions above draft-06

* protoc-gen-jsonschema: fix embedded messages with embedded messages

All schemas in definitions and definitions from embedded messages will now be written on the root schema.

* Fix naming of sub messages

* Fix failing test
  • Loading branch information
morphar authored Jan 6, 2022
1 parent 82ab4fb commit 7f9a651
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 41 deletions.
46 changes: 46 additions & 0 deletions apps/protoc-gen-jsonschema/examples/tests/embedded/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

syntax = "proto3";

package tests.embedded.message.v1;

import "google/api/annotations.proto";

option go_package = "github.com/google/gnostic/apps/protoc-gen-openapi/examples/tests/embedded/message/v1;message";

service Messaging {
rpc UpdateMessage(Message) returns (Message) {
option (google.api.http) = {
post : "/v1/messages/{message_id}"
body : "*"
};
}
}

message Message {
string message_id = 1;
message Comment {
string id = 1;
string content = 2;
message Reply {
string id = 1;
string comment_id = 2;
string content = 3;
}
repeated Reply replies = 3;
}
repeated Comment comments = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"title": "Message",
"$id": "http://example.com/schemas/Message.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"messageId": {
"title": "messageId",
"type": "string"
},
"comments": {
"title": "comments",
"type": "array",
"items": {
"$ref": "#/definitions/Message_Comment"
}
}
},
"definitions": {
"Message_Comment_Reply": {
"title": "Reply",
"type": "object",
"properties": {
"id": {
"title": "id",
"type": "string"
},
"commentId": {
"title": "commentId",
"type": "string"
},
"content": {
"title": "content",
"type": "string"
}
}
},
"Message_Comment": {
"title": "Comment",
"type": "object",
"properties": {
"id": {
"title": "id",
"type": "string"
},
"content": {
"title": "content",
"type": "string"
},
"replies": {
"title": "replies",
"type": "array",
"items": {
"$ref": "#/definitions/Message_Comment_Reply"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"title": "Message",
"$id": "http://example.com/schemas/Message.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"message_id": {
"title": "message_id",
"type": "string"
},
"comments": {
"title": "comments",
"type": "array",
"items": {
"$ref": "#/definitions/Message_Comment"
}
}
},
"definitions": {
"Message_Comment_Reply": {
"title": "Reply",
"type": "object",
"properties": {
"id": {
"title": "id",
"type": "string"
},
"comment_id": {
"title": "comment_id",
"type": "string"
},
"content": {
"title": "content",
"type": "string"
}
}
},
"Message_Comment": {
"title": "Comment",
"type": "object",
"properties": {
"id": {
"title": "id",
"type": "string"
},
"content": {
"title": "content",
"type": "string"
},
"replies": {
"title": "replies",
"type": "array",
"items": {
"$ref": "#/definitions/Message_Comment_Reply"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"messageId": "123abc",
"comments": [
{
"id": "abc123",
"content": "first comment",
"replies": [
{
"id": "xyz123",
"commentId": "abc123",
"content": "first reply to first comment"
}
]
},
{
"id": "123abc",
"content": "second comment",
"replies": [
{
"id": "123xyz",
"commentId": "123abc",
"content": "first reply to second comment"
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"$ref": "http://example.com/schemas/AnotherMessage.json"
},
"subMessage": {
"$ref": "#/definitions/SubMessage"
"$ref": "#/definitions/Message_SubMessage"
},
"stringList": {
"title": "stringList",
Expand All @@ -27,7 +27,7 @@
"title": "subMessageList",
"type": "array",
"items": {
"$ref": "#/definitions/SubMessage"
"$ref": "#/definitions/Message_SubMessage"
}
},
"anotherMessageList": {
Expand Down Expand Up @@ -55,7 +55,7 @@
"title": "subMessagesMap",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/SubMessage"
"$ref": "#/definitions/Message_SubMessage"
}
},
"anotherMessagesMap": {
Expand All @@ -74,10 +74,8 @@
}
},
"definitions": {
"SubMessage": {
"Message_SubMessage": {
"title": "SubMessage",
"$id": "/schemas/SubMessage",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"$ref": "http://example.com/schemas/AnotherMessage.json"
},
"sub_message": {
"$ref": "#/definitions/SubMessage"
"$ref": "#/definitions/Message_SubMessage"
},
"string_list": {
"title": "string_list",
Expand All @@ -27,7 +27,7 @@
"title": "sub_message_list",
"type": "array",
"items": {
"$ref": "#/definitions/SubMessage"
"$ref": "#/definitions/Message_SubMessage"
}
},
"another_message_list": {
Expand Down Expand Up @@ -55,7 +55,7 @@
"title": "sub_messages_map",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/SubMessage"
"$ref": "#/definitions/Message_SubMessage"
}
},
"another_messages_map": {
Expand All @@ -74,10 +74,8 @@
}
},
"definitions": {
"SubMessage": {
"Message_SubMessage": {
"title": "SubMessage",
"$id": "/schemas/SubMessage",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
Expand Down
Loading

0 comments on commit 7f9a651

Please sign in to comment.