Skip to content

Commit

Permalink
feat: add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid committed Sep 18, 2023
1 parent 5b12231 commit 010de0c
Show file tree
Hide file tree
Showing 77 changed files with 2,728 additions and 154 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ go 1.21.1

require (
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0
github.com/invopop/jsonschema v0.8.0
github.com/lyft/protoc-gen-star/v2 v2.0.3
github.com/samber/lo v1.38.1
google.golang.org/protobuf v1.30.0
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.3.0
)

require (
Expand All @@ -20,4 +19,5 @@ require (
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/text v0.11.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/invopop/jsonschema v0.8.0 h1:9Vblm5uNqURXUSaX0QUYcI/Hcu5rrvOz5MbpWgw0VkM=
github.com/invopop/jsonschema v0.8.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -35,7 +33,6 @@ github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY52
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand All @@ -59,6 +56,10 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
24 changes: 16 additions & 8 deletions pkg/modules/1_middleend_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
func buildFromMessage(message pgs.Message, mo *proto.MessageOptions) *jsonschema.Schema {
schema := &jsonschema.Schema{}
schema.Type = "object"
schema.Title = message.Name().UpperCamelCase().String()
schema.Description = message.SourceCodeInfo().LeadingComments()
schema.Title = proto.GetTitleOrEmpty(mo)
schema.Description = proto.GetDescriptionOrComment(message, mo)
schema.Properties = jsonschema.NewOrderedSchemaMap()

fillSchemaByObjectKeywords(schema, mo.GetObject())
Expand All @@ -26,7 +26,14 @@ func buildFromMessage(message pgs.Message, mo *proto.MessageOptions) *jsonschema
//}

propName := toPropertyName(field.Name())
schema.Properties.Set(propName, &jsonschema.Schema{Ref: toRefId(field)})
fieldSchema := &jsonschema.Schema{Ref: toRefId(field)}
if proto.GetFieldOptions(field).GetNullable() {
fieldSchema = &jsonschema.Schema{AnyOf: []*jsonschema.Schema{
{Type: "null"},
fieldSchema,
}}
}
schema.Properties.Set(propName, fieldSchema)

// If field is not a member of oneOf
if !field.InRealOneOf() && !field.HasOptionalKeyword() {
Expand Down Expand Up @@ -70,7 +77,6 @@ func buildFromMessageField(field pgs.Field, fo *proto.FieldOptions) *jsonschema.
}
}

// TODO: 미완성
func buildFromMapField(field pgs.Field, fo *proto.FieldOptions) *jsonschema.Schema {
schema := &jsonschema.Schema{}
schema.Title = proto.GetTitleOrEmpty(fo)
Expand All @@ -80,10 +86,12 @@ func buildFromMapField(field pgs.Field, fo *proto.FieldOptions) *jsonschema.Sche
valueSchema := &jsonschema.Schema{}
value := field.Type().Element()
protoType := value.ProtoType()
if protoType == pgs.MessageT {
valueSchema.Ref = toRefId(value.Embed())
if protoType.IsInt() {
schema.Type = "integer"
} else if protoType.IsNumeric() {
valueSchema.Type = "number"
} else if protoType == pgs.MessageT {
valueSchema.Ref = toRefId(value.Embed())
} else if protoType == pgs.BoolT {
valueSchema.Type = "boolean"
} else if protoType == pgs.EnumT {
Expand Down Expand Up @@ -156,7 +164,7 @@ func buildFromEnum(enum pgs.Enum) (*jsonschema.Schema, error) {
case proto.EnumOptions_MapToCustom:
evo := proto.GetEnumValueOptions(enumValue)

customValue, err := parseScalaValueFromAny(evo.CustomValue)
customValue, err := parseScalaValueFromAny(evo.GetCustomValue())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -263,7 +271,7 @@ func getWellKnownType(field pgs.Field) WellKnownType {
}

func parseScalaValueFromAny(anyValue *anypb.Any) (any, error) {
if anyValue.Value == nil {
if anyValue == nil || anyValue.Value == nil {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/4_backend_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

pgs "github.com/lyft/protoc-gen-star/v2"
"github.com/pubg/protoc-gen-jsonschema/pkg/proto"
"gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
)

type BackendSerializer interface {
Expand Down
4 changes: 1 addition & 3 deletions pkg/proto/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ func GetDescriptionOrComment(name NameWithSourceResolver, description Descriptio
}

builder := &strings.Builder{}
builder.WriteString(name.SourceCodeInfo().LeadingComments())
builder.WriteString("\n")
for _, comment := range name.SourceCodeInfo().LeadingDetachedComments() {
builder.WriteString(comment)
builder.WriteString("\n")
}
builder.WriteString(name.SourceCodeInfo().LeadingComments())
builder.WriteString(name.SourceCodeInfo().TrailingComments())
comment := strings.TrimSpace(strings.Trim(builder.String(), "\n"))

Expand Down
1 change: 1 addition & 0 deletions proto/jsonschema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ message FileOptions {
default: inherit from PluginOptions.entrypoint_message
*/
string entrypoint_message = 2;

/**
output_file_suffix is used to determine output file name suffix.
Values should end with '.json' or '.yaml' or '.yml'.
Expand Down
5 changes: 3 additions & 2 deletions testdata/cases/README.md → testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ jsonschema-number number validation keyword 테스트
jsonschema-string string validation keyword 테스트
jsonschema-array array validation keyword 테스트
jsonschema-object object validation keyword 테스트
jsonschema-enum enum validation keyword 테스트
jsonschema-const const validation keyword 테스트

[//]: # (jsonschema-enum enum validation keyword 테스트)
[//]: # (jsonschema-const const validation keyword 테스트)

plugin-entrypoint-options plugin entrypoint가 잘 작동하는지 테스트
plugin-output-options output 관련 옵션이 잘 작동하는지 테스트
Expand Down
54 changes: 54 additions & 0 deletions testdata/cases/jsonschema-array/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
syntax = "proto3";

package tests;

import "jsonschema.proto";

option (pubg.jsonschema.file) = {entrypoint_message: "Values"};

message Values {
repeated string val1 = 1;

repeated int64 val2 = 2
[(pubg.jsonschema.field) = {
title: "My Title"
description: "My Description"
nullable: true
}];

repeated float optioned1 = 3
[(pubg.jsonschema.field) = {array:{
max_items: 3
min_items: 1
}}];

repeated double optioned2 = 4
[(pubg.jsonschema.field) = {array:{
max_items: 3
min_items: 1
unique_items: true
}}];

repeated double optioned3 = 5
[(pubg.jsonschema.field) = {array:{
max_items: 3
min_items: 1
unique_items: false
}}];

repeated MyMessage message = 6;

repeated MyEnum enum = 7;
}

message MyMessage {
repeated string val1 = 1;

repeated int64 val2 = 2;
}

enum MyEnum {
FOO = 0;
BAR = 1;
BAZ = 2;
}
138 changes: 138 additions & 0 deletions testdata/cases/jsonschema-array/test.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/.tests.Values",
"$defs": {
".tests.MyEnum": {
"type": "string",
"enum": [
"FOO",
"BAR",
"BAZ"
]
},
".tests.Values": {
"properties": {
"val1": {
"$ref": "#/$defs/.tests.Values.val1"
},
"val2": {
"anyOf": [
{
"type": "null"
},
{
"$ref": "#/$defs/.tests.Values.val2"
}
]
},
"optioned1": {
"$ref": "#/$defs/.tests.Values.optioned1"
},
"optioned2": {
"$ref": "#/$defs/.tests.Values.optioned2"
},
"optioned3": {
"$ref": "#/$defs/.tests.Values.optioned3"
},
"message": {
"$ref": "#/$defs/.tests.Values.message"
},
"enum": {
"$ref": "#/$defs/.tests.Values.enum"
}
},
"type": "object",
"required": [
"val1",
"val2",
"optioned1",
"optioned2",
"optioned3",
"message",
"enum"
]
},
".tests.Values.val1": {
"items": {
"type": "string"
},
"type": "array"
},
".tests.Values.val2": {
"items": {
"type": "integer",
"title": "My Title",
"description": "My Description"
},
"type": "array",
"title": "My Title",
"description": "My Description"
},
".tests.Values.optioned1": {
"items": {
"type": "number"
},
"type": "array",
"maxItems": 3,
"minItems": 1
},
".tests.Values.optioned2": {
"items": {
"type": "number"
},
"type": "array",
"maxItems": 3,
"minItems": 1,
"uniqueItems": true
},
".tests.Values.optioned3": {
"items": {
"type": "number"
},
"type": "array",
"maxItems": 3,
"minItems": 1,
"uniqueItems": false
},
".tests.Values.message": {
"items": {
"$ref": "#/$defs/.tests.MyMessage"
},
"type": "array"
},
".tests.Values.enum": {
"items": {
"$ref": "#/$defs/.tests.MyEnum"
},
"type": "array"
},
".tests.MyMessage": {
"properties": {
"val1": {
"$ref": "#/$defs/.tests.MyMessage.val1"
},
"val2": {
"$ref": "#/$defs/.tests.MyMessage.val2"
}
},
"type": "object",
"required": [
"val1",
"val2"
]
},
".tests.MyMessage.val1": {
"items": {
"type": "string"
},
"type": "array"
},
".tests.MyMessage.val2": {
"items": {
"type": "integer"
},
"type": "array"
}
},
"type": "object"
}
11 changes: 11 additions & 0 deletions testdata/cases/jsonschema-array/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: "jsonschema-array"
description: ""

inputFiles: ["test.proto"]
inputParameters: {}

expectResultFiles: ["test.schema.json"]
expectResultIsNull: false
expectFail: false

expectedBehaviorDescription: ""
Loading

0 comments on commit 010de0c

Please sign in to comment.