generator_test.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. package genopenapi_test
  2. import (
  3. "os"
  4. "reflect"
  5. "sort"
  6. "strings"
  7. "testing"
  8. "github.com/google/go-cmp/cmp"
  9. "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
  10. "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi"
  11. "gopkg.in/yaml.v3"
  12. "google.golang.org/protobuf/encoding/prototext"
  13. "google.golang.org/protobuf/proto"
  14. "google.golang.org/protobuf/types/descriptorpb"
  15. "google.golang.org/protobuf/types/pluginpb"
  16. )
  17. func TestGenerate_YAML(t *testing.T) {
  18. t.Parallel()
  19. req := &pluginpb.CodeGeneratorRequest{
  20. ProtoFile: []*descriptorpb.FileDescriptorProto{{
  21. Name: proto.String("file.proto"),
  22. Package: proto.String("example"),
  23. Options: &descriptorpb.FileOptions{
  24. GoPackage: proto.String("goexample/v1;goexample"),
  25. },
  26. }},
  27. FileToGenerate: []string{
  28. "file.proto",
  29. },
  30. }
  31. resp := requireGenerate(t, req, genopenapi.FormatYAML, false, false)
  32. if len(resp) != 1 {
  33. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  34. }
  35. var p map[string]interface{}
  36. err := yaml.Unmarshal([]byte(resp[0].GetContent()), &p)
  37. if err != nil {
  38. t.Fatalf("failed to unmarshall yaml: %s", err)
  39. }
  40. }
  41. func TestGenerateExtension(t *testing.T) {
  42. t.Parallel()
  43. const in = `
  44. file_to_generate: "exampleproto/v1/example.proto"
  45. parameter: "output_format=yaml,allow_delete_body=true"
  46. proto_file: {
  47. name: "exampleproto/v1/example.proto"
  48. package: "example.v1"
  49. message_type: {
  50. name: "Foo"
  51. field: {
  52. name: "bar"
  53. number: 1
  54. label: LABEL_OPTIONAL
  55. type: TYPE_STRING
  56. json_name: "bar"
  57. options: {
  58. [grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field]: {
  59. description: "This is bar"
  60. extensions: {
  61. key: "x-go-default"
  62. value: {
  63. string_value: "0.5s"
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. service: {
  71. name: "TestService"
  72. method: {
  73. name: "Test"
  74. input_type: ".example.v1.Foo"
  75. output_type: ".example.v1.Foo"
  76. options: {}
  77. }
  78. }
  79. options: {
  80. go_package: "exampleproto/v1;exampleproto"
  81. }
  82. }`
  83. var req pluginpb.CodeGeneratorRequest
  84. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  85. t.Fatalf("failed to marshall yaml: %s", err)
  86. }
  87. formats := [...]genopenapi.Format{
  88. genopenapi.FormatJSON,
  89. genopenapi.FormatYAML,
  90. }
  91. for _, format := range formats {
  92. format := format
  93. t.Run(string(format), func(t *testing.T) {
  94. t.Parallel()
  95. resp := requireGenerate(t, &req, format, false, false)
  96. if len(resp) != 1 {
  97. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  98. }
  99. content := resp[0].GetContent()
  100. t.Log(content)
  101. if !strings.Contains(content, "x-go-default") {
  102. t.Fatal("x-go-default not found in content message")
  103. }
  104. })
  105. }
  106. }
  107. func TestGenerateYAML(t *testing.T) {
  108. t.Parallel()
  109. tests := []struct {
  110. name string
  111. inputProtoText string
  112. wantYAML string
  113. }{
  114. {
  115. // It tests https://github.com/grpc-ecosystem/grpc-gateway/issues/3557.
  116. name: "path item object",
  117. inputProtoText: "testdata/generator/path_item_object.prototext",
  118. wantYAML: "testdata/generator/path_item_object.swagger.yaml",
  119. },
  120. }
  121. for _, tt := range tests {
  122. tt := tt
  123. t.Run(tt.name, func(t *testing.T) {
  124. t.Parallel()
  125. b, err := os.ReadFile(tt.inputProtoText)
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. var req pluginpb.CodeGeneratorRequest
  130. if err := prototext.Unmarshal(b, &req); err != nil {
  131. t.Fatal(err)
  132. }
  133. resp := requireGenerate(t, &req, genopenapi.FormatYAML, false, true)
  134. if len(resp) != 1 {
  135. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  136. }
  137. got := resp[0].GetContent()
  138. want, err := os.ReadFile(tt.wantYAML)
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. diff := cmp.Diff(string(want), got)
  143. if diff != "" {
  144. t.Fatalf("content not match\n%s", diff)
  145. }
  146. })
  147. }
  148. }
  149. func requireGenerate(
  150. tb testing.TB,
  151. req *pluginpb.CodeGeneratorRequest,
  152. format genopenapi.Format,
  153. preserveRPCOrder bool,
  154. allowMerge bool,
  155. ) []*descriptor.ResponseFile {
  156. tb.Helper()
  157. reg := descriptor.NewRegistry()
  158. reg.SetPreserveRPCOrder(preserveRPCOrder)
  159. reg.SetAllowMerge(allowMerge)
  160. if err := reg.Load(req); err != nil {
  161. tb.Fatalf("failed to load request: %s", err)
  162. }
  163. var targets []*descriptor.File
  164. for _, target := range req.FileToGenerate {
  165. f, err := reg.LookupFile(target)
  166. if err != nil {
  167. tb.Fatalf("failed to lookup file: %s", err)
  168. }
  169. targets = append(targets, f)
  170. }
  171. g := genopenapi.New(reg, format)
  172. resp, err := g.Generate(targets)
  173. switch {
  174. case err != nil:
  175. tb.Fatalf("failed to generate targets: %s", err)
  176. case len(resp) != len(targets) && !allowMerge:
  177. tb.Fatalf("invalid count, expected: %d, actual: %d", len(targets), len(resp))
  178. }
  179. return resp
  180. }
  181. func TestGeneratedYAMLIndent(t *testing.T) {
  182. // It tests https://github.com/grpc-ecosystem/grpc-gateway/issues/2745.
  183. const in = `
  184. file_to_generate: "exampleproto/v1/exampleproto.proto"
  185. parameter: "output_format=yaml,allow_delete_body=true"
  186. proto_file: {
  187. name: "exampleproto/v1/exampleproto.proto"
  188. package: "repro"
  189. message_type: {
  190. name: "RollupRequest"
  191. field: {
  192. name: "type"
  193. number: 1
  194. label: LABEL_OPTIONAL
  195. type: TYPE_ENUM
  196. type_name: ".repro.RollupType"
  197. json_name: "type"
  198. }
  199. }
  200. message_type: {
  201. name: "RollupResponse"
  202. }
  203. enum_type: {
  204. name: "RollupType"
  205. value: {
  206. name: "UNKNOWN_ROLLUP"
  207. number: 0
  208. }
  209. value: {
  210. name: "APPLE"
  211. number: 1
  212. }
  213. value: {
  214. name: "BANANA"
  215. number: 2
  216. }
  217. value: {
  218. name: "CARROT"
  219. number: 3
  220. }
  221. }
  222. service: {
  223. name: "Repro"
  224. method: {
  225. name: "GetRollup"
  226. input_type: ".repro.RollupRequest"
  227. output_type: ".repro.RollupResponse"
  228. options: {
  229. [google.api.http]: {
  230. get: "/rollup"
  231. }
  232. }
  233. }
  234. }
  235. options: {
  236. go_package: "repro/foobar"
  237. }
  238. source_code_info: {
  239. location: {
  240. path: 5
  241. path: 0
  242. path: 2
  243. path: 1
  244. span: 24
  245. span: 4
  246. span: 14
  247. leading_comments: " Apples are good\n"
  248. }
  249. location: {
  250. path: 5
  251. path: 0
  252. path: 2
  253. path: 3
  254. span: 28
  255. span: 4
  256. span: 15
  257. leading_comments: " Carrots are mediocre\n"
  258. }
  259. }
  260. syntax: "proto3"
  261. }
  262. `
  263. var req pluginpb.CodeGeneratorRequest
  264. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  265. t.Fatalf("failed to marshall yaml: %s", err)
  266. }
  267. resp := requireGenerate(t, &req, genopenapi.FormatYAML, false, false)
  268. if len(resp) != 1 {
  269. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  270. }
  271. content := resp[0].GetContent()
  272. err := yaml.Unmarshal([]byte(content), map[string]interface{}{})
  273. if err != nil {
  274. t.Log(content)
  275. t.Fatalf("got invalid yaml: %s", err)
  276. }
  277. }
  278. func TestGenerateRPCOrderPreserved(t *testing.T) {
  279. t.Parallel()
  280. const in = `
  281. file_to_generate: "exampleproto/v1/example.proto"
  282. parameter: "output_format=yaml,allow_delete_body=true"
  283. proto_file: {
  284. name: "exampleproto/v1/example.proto"
  285. package: "example.v1"
  286. message_type: {
  287. name: "Foo"
  288. field: {
  289. name: "bar"
  290. number: 1
  291. label: LABEL_OPTIONAL
  292. type: TYPE_STRING
  293. json_name: "bar"
  294. }
  295. }
  296. service: {
  297. name: "TestService"
  298. method: {
  299. name: "Test1"
  300. input_type: ".example.v1.Foo"
  301. output_type: ".example.v1.Foo"
  302. options: {
  303. [google.api.http]: {
  304. get: "/b/first"
  305. }
  306. }
  307. }
  308. method: {
  309. name: "Test2"
  310. input_type: ".example.v1.Foo"
  311. output_type: ".example.v1.Foo"
  312. options: {
  313. [google.api.http]: {
  314. get: "/a/second"
  315. }
  316. }
  317. }
  318. method: {
  319. name: "Test3"
  320. input_type: ".example.v1.Foo"
  321. output_type: ".example.v1.Foo"
  322. options: {
  323. [google.api.http]: {
  324. get: "/c/third"
  325. }
  326. }
  327. }
  328. }
  329. options: {
  330. go_package: "exampleproto/v1;exampleproto"
  331. }
  332. }`
  333. var req pluginpb.CodeGeneratorRequest
  334. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  335. t.Fatalf("failed to marshall yaml: %s", err)
  336. }
  337. formats := [...]genopenapi.Format{
  338. genopenapi.FormatJSON,
  339. genopenapi.FormatYAML,
  340. }
  341. for _, format := range formats {
  342. format := format
  343. t.Run(string(format), func(t *testing.T) {
  344. t.Parallel()
  345. resp := requireGenerate(t, &req, format, true, false)
  346. if len(resp) != 1 {
  347. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  348. }
  349. content := resp[0].GetContent()
  350. t.Log(content)
  351. contentsSlice := strings.Fields(content)
  352. expectedPaths := []string{"/b/first", "/a/second", "/c/third"}
  353. foundPaths := []string{}
  354. for _, contentValue := range contentsSlice {
  355. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  356. }
  357. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  358. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  359. }
  360. })
  361. }
  362. }
  363. func TestGenerateRPCOrderNotPreserved(t *testing.T) {
  364. t.Parallel()
  365. const in = `
  366. file_to_generate: "exampleproto/v1/example.proto"
  367. parameter: "output_format=yaml,allow_delete_body=true"
  368. proto_file: {
  369. name: "exampleproto/v1/example.proto"
  370. package: "example.v1"
  371. message_type: {
  372. name: "Foo"
  373. field: {
  374. name: "bar"
  375. number: 1
  376. label: LABEL_OPTIONAL
  377. type: TYPE_STRING
  378. json_name: "bar"
  379. }
  380. }
  381. service: {
  382. name: "TestService"
  383. method: {
  384. name: "Test1"
  385. input_type: ".example.v1.Foo"
  386. output_type: ".example.v1.Foo"
  387. options: {
  388. [google.api.http]: {
  389. get: "/b/first"
  390. }
  391. }
  392. }
  393. method: {
  394. name: "Test2"
  395. input_type: ".example.v1.Foo"
  396. output_type: ".example.v1.Foo"
  397. options: {
  398. [google.api.http]: {
  399. get: "/a/second"
  400. }
  401. }
  402. }
  403. method: {
  404. name: "Test3"
  405. input_type: ".example.v1.Foo"
  406. output_type: ".example.v1.Foo"
  407. options: {
  408. [google.api.http]: {
  409. get: "/c/third"
  410. }
  411. }
  412. }
  413. }
  414. options: {
  415. go_package: "exampleproto/v1;exampleproto"
  416. }
  417. }`
  418. var req pluginpb.CodeGeneratorRequest
  419. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  420. t.Fatalf("failed to marshall yaml: %s", err)
  421. }
  422. formats := [...]genopenapi.Format{
  423. genopenapi.FormatJSON,
  424. genopenapi.FormatYAML,
  425. }
  426. for _, format := range formats {
  427. format := format
  428. t.Run(string(format), func(t *testing.T) {
  429. t.Parallel()
  430. resp := requireGenerate(t, &req, format, false, false)
  431. if len(resp) != 1 {
  432. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  433. }
  434. content := resp[0].GetContent()
  435. t.Log(content)
  436. contentsSlice := strings.Fields(content)
  437. expectedPaths := []string{"/a/second", "/b/first", "/c/third"}
  438. foundPaths := []string{}
  439. for _, contentValue := range contentsSlice {
  440. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  441. }
  442. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  443. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  444. }
  445. })
  446. }
  447. }
  448. func TestGenerateRPCOrderPreservedMultipleServices(t *testing.T) {
  449. t.Parallel()
  450. const in = `
  451. file_to_generate: "exampleproto/v1/example.proto"
  452. parameter: "output_format=yaml,allow_delete_body=true"
  453. proto_file: {
  454. name: "exampleproto/v1/example.proto"
  455. package: "example.v1"
  456. message_type: {
  457. name: "Foo"
  458. field: {
  459. name: "bar"
  460. number: 1
  461. label: LABEL_OPTIONAL
  462. type: TYPE_STRING
  463. json_name: "bar"
  464. }
  465. }
  466. service: {
  467. name: "TestServiceOne"
  468. method: {
  469. name: "Test1"
  470. input_type: ".example.v1.Foo"
  471. output_type: ".example.v1.Foo"
  472. options: {
  473. [google.api.http]: {
  474. get: "/d/first"
  475. }
  476. }
  477. }
  478. method: {
  479. name: "Test2"
  480. input_type: ".example.v1.Foo"
  481. output_type: ".example.v1.Foo"
  482. options: {
  483. [google.api.http]: {
  484. get: "/e/second"
  485. }
  486. }
  487. }
  488. method: {
  489. name: "Test3"
  490. input_type: ".example.v1.Foo"
  491. output_type: ".example.v1.Foo"
  492. options: {
  493. [google.api.http]: {
  494. get: "/c/third"
  495. }
  496. }
  497. }
  498. }
  499. service: {
  500. name: "TestServiceTwo"
  501. method: {
  502. name: "Test1"
  503. input_type: ".example.v1.Foo"
  504. output_type: ".example.v1.Foo"
  505. options: {
  506. [google.api.http]: {
  507. get: "/b/first"
  508. }
  509. }
  510. }
  511. method: {
  512. name: "Test2"
  513. input_type: ".example.v1.Foo"
  514. output_type: ".example.v1.Foo"
  515. options: {
  516. [google.api.http]: {
  517. get: "/a/second"
  518. }
  519. }
  520. }
  521. method: {
  522. name: "Test3"
  523. input_type: ".example.v1.Foo"
  524. output_type: ".example.v1.Foo"
  525. options: {
  526. [google.api.http]: {
  527. get: "/g/third"
  528. }
  529. }
  530. }
  531. }
  532. options: {
  533. go_package: "exampleproto/v1;exampleproto"
  534. }
  535. }`
  536. var req pluginpb.CodeGeneratorRequest
  537. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  538. t.Fatalf("failed to marshall yaml: %s", err)
  539. }
  540. formats := [...]genopenapi.Format{
  541. genopenapi.FormatJSON,
  542. genopenapi.FormatYAML,
  543. }
  544. for _, format := range formats {
  545. format := format
  546. t.Run(string(format), func(t *testing.T) {
  547. t.Parallel()
  548. resp := requireGenerate(t, &req, format, true, false)
  549. if len(resp) != 1 {
  550. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  551. }
  552. content := resp[0].GetContent()
  553. t.Log(content)
  554. contentsSlice := strings.Fields(content)
  555. expectedPaths := []string{"/d/first", "/e/second", "/c/third", "/b/first", "/a/second", "/g/third"}
  556. foundPaths := []string{}
  557. for _, contentValue := range contentsSlice {
  558. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  559. }
  560. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  561. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  562. }
  563. })
  564. }
  565. }
  566. func TestGenerateRPCOrderNotPreservedMultipleServices(t *testing.T) {
  567. t.Parallel()
  568. const in = `
  569. file_to_generate: "exampleproto/v1/example.proto"
  570. parameter: "output_format=yaml,allow_delete_body=true"
  571. proto_file: {
  572. name: "exampleproto/v1/example.proto"
  573. package: "example.v1"
  574. message_type: {
  575. name: "Foo"
  576. field: {
  577. name: "bar"
  578. number: 1
  579. label: LABEL_OPTIONAL
  580. type: TYPE_STRING
  581. json_name: "bar"
  582. }
  583. }
  584. service: {
  585. name: "TestServiceOne"
  586. method: {
  587. name: "Test1"
  588. input_type: ".example.v1.Foo"
  589. output_type: ".example.v1.Foo"
  590. options: {
  591. [google.api.http]: {
  592. get: "/d/first"
  593. }
  594. }
  595. }
  596. method: {
  597. name: "Test2"
  598. input_type: ".example.v1.Foo"
  599. output_type: ".example.v1.Foo"
  600. options: {
  601. [google.api.http]: {
  602. get: "/e/second"
  603. }
  604. }
  605. }
  606. method: {
  607. name: "Test3"
  608. input_type: ".example.v1.Foo"
  609. output_type: ".example.v1.Foo"
  610. options: {
  611. [google.api.http]: {
  612. get: "/c/third"
  613. }
  614. }
  615. }
  616. }
  617. service: {
  618. name: "TestServiceTwo"
  619. method: {
  620. name: "Test1"
  621. input_type: ".example.v1.Foo"
  622. output_type: ".example.v1.Foo"
  623. options: {
  624. [google.api.http]: {
  625. get: "/b/first"
  626. }
  627. }
  628. }
  629. method: {
  630. name: "Test2"
  631. input_type: ".example.v1.Foo"
  632. output_type: ".example.v1.Foo"
  633. options: {
  634. [google.api.http]: {
  635. get: "/a/second"
  636. }
  637. }
  638. }
  639. method: {
  640. name: "Test3"
  641. input_type: ".example.v1.Foo"
  642. output_type: ".example.v1.Foo"
  643. options: {
  644. [google.api.http]: {
  645. get: "/g/third"
  646. }
  647. }
  648. }
  649. }
  650. options: {
  651. go_package: "exampleproto/v1;exampleproto"
  652. }
  653. }`
  654. var req pluginpb.CodeGeneratorRequest
  655. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  656. t.Fatalf("failed to marshall yaml: %s", err)
  657. }
  658. formats := [...]genopenapi.Format{
  659. genopenapi.FormatJSON,
  660. genopenapi.FormatYAML,
  661. }
  662. for _, format := range formats {
  663. format := format
  664. t.Run(string(format), func(t *testing.T) {
  665. t.Parallel()
  666. resp := requireGenerate(t, &req, format, false, false)
  667. if len(resp) != 1 {
  668. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  669. }
  670. content := resp[0].GetContent()
  671. t.Log(content)
  672. contentsSlice := strings.Fields(content)
  673. expectedPaths := []string{"/d/first", "/e/second", "/c/third", "/b/first", "/a/second", "/g/third"}
  674. sort.Strings(expectedPaths)
  675. foundPaths := []string{}
  676. for _, contentValue := range contentsSlice {
  677. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  678. }
  679. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  680. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  681. }
  682. })
  683. }
  684. }
  685. func TestGenerateRPCOrderPreservedMergeFiles(t *testing.T) {
  686. t.Parallel()
  687. const in1 = `
  688. file_to_generate: "exampleproto/v1/example.proto"
  689. parameter: "output_format=yaml,allow_delete_body=true"
  690. proto_file: {
  691. name: "exampleproto/v1/example.proto"
  692. package: "example.v1"
  693. message_type: {
  694. name: "Foo"
  695. field: {
  696. name: "bar"
  697. number: 1
  698. label: LABEL_OPTIONAL
  699. type: TYPE_STRING
  700. json_name: "bar"
  701. }
  702. }
  703. service: {
  704. name: "TestService"
  705. method: {
  706. name: "Test1"
  707. input_type: ".example.v1.Foo"
  708. output_type: ".example.v1.Foo"
  709. options: {
  710. [google.api.http]: {
  711. get: "/c/cpath"
  712. }
  713. }
  714. }
  715. method: {
  716. name: "Test2"
  717. input_type: ".example.v1.Foo"
  718. output_type: ".example.v1.Foo"
  719. options: {
  720. [google.api.http]: {
  721. get: "/b/bpath"
  722. }
  723. }
  724. }
  725. method: {
  726. name: "Test3"
  727. input_type: ".example.v1.Foo"
  728. output_type: ".example.v1.Foo"
  729. options: {
  730. [google.api.http]: {
  731. get: "/a/apath"
  732. }
  733. }
  734. }
  735. }
  736. options: {
  737. go_package: "exampleproto/v1;exampleproto"
  738. }
  739. }`
  740. const in2 = `
  741. file_to_generate: "exampleproto/v2/example.proto"
  742. parameter: "output_format=yaml,allow_delete_body=true"
  743. proto_file: {
  744. name: "exampleproto/v2/example.proto"
  745. package: "example.v2"
  746. message_type: {
  747. name: "Foo"
  748. field: {
  749. name: "bar"
  750. number: 1
  751. label: LABEL_OPTIONAL
  752. type: TYPE_STRING
  753. json_name: "bar"
  754. }
  755. }
  756. service: {
  757. name: "TestService"
  758. method: {
  759. name: "Test1"
  760. input_type: ".example.v2.Foo"
  761. output_type: ".example.v2.Foo"
  762. options: {
  763. [google.api.http]: {
  764. get: "/f/fpath"
  765. }
  766. }
  767. }
  768. method: {
  769. name: "Test2"
  770. input_type: ".example.v2.Foo"
  771. output_type: ".example.v2.Foo"
  772. options: {
  773. [google.api.http]: {
  774. get: "/e/epath"
  775. }
  776. }
  777. }
  778. method: {
  779. name: "Test3"
  780. input_type: ".example.v2.Foo"
  781. output_type: ".example.v2.Foo"
  782. options: {
  783. [google.api.http]: {
  784. get: "/d/dpath"
  785. }
  786. }
  787. }
  788. }
  789. options: {
  790. go_package: "exampleproto/v2;exampleproto"
  791. }
  792. }`
  793. var req1, req2 pluginpb.CodeGeneratorRequest
  794. if err := prototext.Unmarshal([]byte(in1), &req1); err != nil {
  795. t.Fatalf("failed to marshall yaml: %s", err)
  796. }
  797. if err := prototext.Unmarshal([]byte(in2), &req2); err != nil {
  798. t.Fatalf("failed to marshall yaml: %s", err)
  799. }
  800. req1.ProtoFile = append(req1.ProtoFile, req2.ProtoFile...)
  801. req1.FileToGenerate = append(req1.FileToGenerate, req2.FileToGenerate...)
  802. formats := [...]genopenapi.Format{
  803. genopenapi.FormatJSON,
  804. genopenapi.FormatYAML,
  805. }
  806. for _, format := range formats {
  807. format := format
  808. t.Run(string(format), func(t *testing.T) {
  809. t.Parallel()
  810. resp := requireGenerate(t, &req1, format, true, true)
  811. if len(resp) != 1 {
  812. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  813. }
  814. content := resp[0].GetContent()
  815. t.Log(content)
  816. contentsSlice := strings.Fields(content)
  817. expectedPaths := []string{"/c/cpath", "/b/bpath", "/a/apath", "/f/fpath", "/e/epath", "/d/dpath"}
  818. foundPaths := []string{}
  819. for _, contentValue := range contentsSlice {
  820. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  821. }
  822. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  823. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  824. }
  825. })
  826. }
  827. }
  828. func TestGenerateRPCOrderNotPreservedMergeFiles(t *testing.T) {
  829. t.Parallel()
  830. const in1 = `
  831. file_to_generate: "exampleproto/v1/example.proto"
  832. parameter: "output_format=yaml,allow_delete_body=true"
  833. proto_file: {
  834. name: "exampleproto/v1/example.proto"
  835. package: "example.v1"
  836. message_type: {
  837. name: "Foo"
  838. field: {
  839. name: "bar"
  840. number: 1
  841. label: LABEL_OPTIONAL
  842. type: TYPE_STRING
  843. json_name: "bar"
  844. }
  845. }
  846. service: {
  847. name: "TestService"
  848. method: {
  849. name: "Test1"
  850. input_type: ".example.v1.Foo"
  851. output_type: ".example.v1.Foo"
  852. options: {
  853. [google.api.http]: {
  854. get: "/c/cpath"
  855. }
  856. }
  857. }
  858. method: {
  859. name: "Test2"
  860. input_type: ".example.v1.Foo"
  861. output_type: ".example.v1.Foo"
  862. options: {
  863. [google.api.http]: {
  864. get: "/b/bpath"
  865. }
  866. }
  867. }
  868. method: {
  869. name: "Test3"
  870. input_type: ".example.v1.Foo"
  871. output_type: ".example.v1.Foo"
  872. options: {
  873. [google.api.http]: {
  874. get: "/a/apath"
  875. }
  876. }
  877. }
  878. }
  879. options: {
  880. go_package: "exampleproto/v1;exampleproto"
  881. }
  882. }`
  883. const in2 = `
  884. file_to_generate: "exampleproto/v2/example.proto"
  885. parameter: "output_format=yaml,allow_delete_body=true"
  886. proto_file: {
  887. name: "exampleproto/v2/example.proto"
  888. package: "example.v2"
  889. message_type: {
  890. name: "Foo"
  891. field: {
  892. name: "bar"
  893. number: 1
  894. label: LABEL_OPTIONAL
  895. type: TYPE_STRING
  896. json_name: "bar"
  897. }
  898. }
  899. service: {
  900. name: "TestService"
  901. method: {
  902. name: "Test1"
  903. input_type: ".example.v2.Foo"
  904. output_type: ".example.v2.Foo"
  905. options: {
  906. [google.api.http]: {
  907. get: "/f/fpath"
  908. }
  909. }
  910. }
  911. method: {
  912. name: "Test2"
  913. input_type: ".example.v2.Foo"
  914. output_type: ".example.v2.Foo"
  915. options: {
  916. [google.api.http]: {
  917. get: "/e/epath"
  918. }
  919. }
  920. }
  921. method: {
  922. name: "Test3"
  923. input_type: ".example.v2.Foo"
  924. output_type: ".example.v2.Foo"
  925. options: {
  926. [google.api.http]: {
  927. get: "/d/dpath"
  928. }
  929. }
  930. }
  931. }
  932. options: {
  933. go_package: "exampleproto/v2;exampleproto"
  934. }
  935. }`
  936. var req1, req2 pluginpb.CodeGeneratorRequest
  937. if err := prototext.Unmarshal([]byte(in1), &req1); err != nil {
  938. t.Fatalf("failed to marshall yaml: %s", err)
  939. }
  940. if err := prototext.Unmarshal([]byte(in2), &req2); err != nil {
  941. t.Fatalf("failed to marshall yaml: %s", err)
  942. }
  943. req1.ProtoFile = append(req1.ProtoFile, req2.ProtoFile...)
  944. req1.FileToGenerate = append(req1.FileToGenerate, req2.FileToGenerate...)
  945. formats := [...]genopenapi.Format{
  946. genopenapi.FormatJSON,
  947. genopenapi.FormatYAML,
  948. }
  949. for _, format := range formats {
  950. format := format
  951. t.Run(string(format), func(t *testing.T) {
  952. t.Parallel()
  953. resp := requireGenerate(t, &req1, format, false, true)
  954. if len(resp) != 1 {
  955. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  956. }
  957. content := resp[0].GetContent()
  958. t.Log(content)
  959. contentsSlice := strings.Fields(content)
  960. expectedPaths := []string{"/c/cpath", "/b/bpath", "/a/apath", "/f/fpath", "/e/epath", "/d/dpath"}
  961. sort.Strings(expectedPaths)
  962. foundPaths := []string{}
  963. for _, contentValue := range contentsSlice {
  964. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  965. }
  966. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  967. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  968. }
  969. })
  970. }
  971. }
  972. func TestGenerateRPCOrderPreservedAdditionalBindings(t *testing.T) {
  973. t.Parallel()
  974. const in = `
  975. file_to_generate: "exampleproto/v1/example.proto"
  976. parameter: "output_format=yaml,allow_delete_body=true"
  977. proto_file: {
  978. name: "exampleproto/v1/example.proto"
  979. package: "example.v1"
  980. message_type: {
  981. name: "Foo"
  982. field: {
  983. name: "bar"
  984. number: 1
  985. label: LABEL_OPTIONAL
  986. type: TYPE_STRING
  987. json_name: "bar"
  988. }
  989. }
  990. service: {
  991. name: "TestService"
  992. method: {
  993. name: "Test1"
  994. input_type: ".example.v1.Foo"
  995. output_type: ".example.v1.Foo"
  996. options: {
  997. [google.api.http]: {
  998. get: "/b/first"
  999. additional_bindings {
  1000. get: "/a/additional"
  1001. }
  1002. }
  1003. }
  1004. }
  1005. method: {
  1006. name: "Test2"
  1007. input_type: ".example.v1.Foo"
  1008. output_type: ".example.v1.Foo"
  1009. options: {
  1010. [google.api.http]: {
  1011. get: "/a/second"
  1012. additional_bindings {
  1013. get: "/z/zAdditional"
  1014. }
  1015. }
  1016. }
  1017. }
  1018. method: {
  1019. name: "Test3"
  1020. input_type: ".example.v1.Foo"
  1021. output_type: ".example.v1.Foo"
  1022. options: {
  1023. [google.api.http]: {
  1024. get: "/c/third"
  1025. additional_bindings {
  1026. get: "/b/bAdditional"
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. options: {
  1033. go_package: "exampleproto/v1;exampleproto"
  1034. }
  1035. }`
  1036. var req pluginpb.CodeGeneratorRequest
  1037. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  1038. t.Fatalf("failed to marshall yaml: %s", err)
  1039. }
  1040. formats := [...]genopenapi.Format{
  1041. genopenapi.FormatJSON,
  1042. genopenapi.FormatYAML,
  1043. }
  1044. for _, format := range formats {
  1045. format := format
  1046. t.Run(string(format), func(t *testing.T) {
  1047. t.Parallel()
  1048. resp := requireGenerate(t, &req, format, true, false)
  1049. if len(resp) != 1 {
  1050. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  1051. }
  1052. content := resp[0].GetContent()
  1053. t.Log(content)
  1054. contentsSlice := strings.Fields(content)
  1055. expectedPaths := []string{"/b/first", "/a/additional", "/a/second", "/z/zAdditional", "/c/third", "/b/bAdditional"}
  1056. foundPaths := []string{}
  1057. for _, contentValue := range contentsSlice {
  1058. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  1059. }
  1060. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  1061. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  1062. }
  1063. })
  1064. }
  1065. }
  1066. func TestGenerateRPCOneOfFieldBodyAdditionalBindings(t *testing.T) {
  1067. t.Parallel()
  1068. const in = `
  1069. file_to_generate: "exampleproto/v1/example.proto"
  1070. parameter: "output_format=yaml,allow_delete_body=true"
  1071. proto_file: {
  1072. name: "exampleproto/v1/example.proto"
  1073. package: "example.v1"
  1074. message_type: {
  1075. name: "Foo"
  1076. oneof_decl: {
  1077. name: "foo"
  1078. }
  1079. field: {
  1080. name: "bar"
  1081. number: 1
  1082. label: LABEL_OPTIONAL
  1083. type: TYPE_STRING
  1084. json_name: "bar"
  1085. oneof_index: 0
  1086. }
  1087. field: {
  1088. name: "baz"
  1089. number: 2
  1090. label: LABEL_OPTIONAL
  1091. type: TYPE_STRING
  1092. json_name: "bar"
  1093. oneof_index: 0
  1094. }
  1095. }
  1096. service: {
  1097. name: "TestService"
  1098. method: {
  1099. name: "Test1"
  1100. input_type: ".example.v1.Foo"
  1101. output_type: ".example.v1.Foo"
  1102. options: {
  1103. [google.api.http]: {
  1104. post: "/b/foo"
  1105. body: "*"
  1106. additional_bindings {
  1107. post: "/b/foo/bar"
  1108. body: "bar"
  1109. }
  1110. additional_bindings {
  1111. post: "/b/foo/baz"
  1112. body: "baz"
  1113. }
  1114. }
  1115. }
  1116. }
  1117. }
  1118. options: {
  1119. go_package: "exampleproto/v1;exampleproto"
  1120. }
  1121. }`
  1122. var req pluginpb.CodeGeneratorRequest
  1123. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  1124. t.Fatalf("failed to marshall yaml: %s", err)
  1125. }
  1126. formats := [...]genopenapi.Format{
  1127. genopenapi.FormatJSON,
  1128. genopenapi.FormatYAML,
  1129. }
  1130. for _, format := range formats {
  1131. format := format
  1132. t.Run(string(format), func(t *testing.T) {
  1133. t.Parallel()
  1134. resp := requireGenerate(t, &req, format, true, false)
  1135. if len(resp) != 1 {
  1136. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  1137. }
  1138. content := resp[0].GetContent()
  1139. t.Log(content)
  1140. contentsSlice := strings.Fields(content)
  1141. expectedPaths := []string{"/b/foo", "/b/foo/bar", "/b/foo/baz"}
  1142. foundPaths := []string{}
  1143. for _, contentValue := range contentsSlice {
  1144. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  1145. }
  1146. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  1147. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  1148. }
  1149. // The input message only contains oneof fields, so no other fields should be mapped to the query.
  1150. if strings.Contains(content, "query") {
  1151. t.Fatalf("Found query in content, expected not to find any")
  1152. }
  1153. })
  1154. }
  1155. }
  1156. func TestGenerateRPCOrderNotPreservedAdditionalBindings(t *testing.T) {
  1157. t.Parallel()
  1158. const in = `
  1159. file_to_generate: "exampleproto/v1/example.proto"
  1160. parameter: "output_format=yaml,allow_delete_body=true"
  1161. proto_file: {
  1162. name: "exampleproto/v1/example.proto"
  1163. package: "example.v1"
  1164. message_type: {
  1165. name: "Foo"
  1166. field: {
  1167. name: "bar"
  1168. number: 1
  1169. label: LABEL_OPTIONAL
  1170. type: TYPE_STRING
  1171. json_name: "bar"
  1172. }
  1173. }
  1174. service: {
  1175. name: "TestService"
  1176. method: {
  1177. name: "Test1"
  1178. input_type: ".example.v1.Foo"
  1179. output_type: ".example.v1.Foo"
  1180. options: {
  1181. [google.api.http]: {
  1182. get: "/b/first"
  1183. additional_bindings {
  1184. get: "/a/additional"
  1185. }
  1186. }
  1187. }
  1188. }
  1189. method: {
  1190. name: "Test2"
  1191. input_type: ".example.v1.Foo"
  1192. output_type: ".example.v1.Foo"
  1193. options: {
  1194. [google.api.http]: {
  1195. get: "/a/second"
  1196. additional_bindings {
  1197. get: "/z/zAdditional"
  1198. }
  1199. }
  1200. }
  1201. }
  1202. method: {
  1203. name: "Test3"
  1204. input_type: ".example.v1.Foo"
  1205. output_type: ".example.v1.Foo"
  1206. options: {
  1207. [google.api.http]: {
  1208. get: "/c/third"
  1209. additional_bindings {
  1210. get: "/b/bAdditional"
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. options: {
  1217. go_package: "exampleproto/v1;exampleproto"
  1218. }
  1219. }`
  1220. var req pluginpb.CodeGeneratorRequest
  1221. if err := prototext.Unmarshal([]byte(in), &req); err != nil {
  1222. t.Fatalf("failed to marshall yaml: %s", err)
  1223. }
  1224. formats := [...]genopenapi.Format{
  1225. genopenapi.FormatJSON,
  1226. genopenapi.FormatYAML,
  1227. }
  1228. for _, format := range formats {
  1229. format := format
  1230. t.Run(string(format), func(t *testing.T) {
  1231. t.Parallel()
  1232. resp := requireGenerate(t, &req, format, false, false)
  1233. if len(resp) != 1 {
  1234. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  1235. }
  1236. content := resp[0].GetContent()
  1237. t.Log(content)
  1238. contentsSlice := strings.Fields(content)
  1239. expectedPaths := []string{"/b/first", "/a/additional", "/a/second", "/z/zAdditional", "/c/third", "/b/bAdditional"}
  1240. sort.Strings(expectedPaths)
  1241. foundPaths := []string{}
  1242. for _, contentValue := range contentsSlice {
  1243. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  1244. }
  1245. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  1246. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  1247. }
  1248. })
  1249. }
  1250. }
  1251. func TestGenerateRPCOrderPreservedMergeFilesAdditionalBindingsMultipleServices(t *testing.T) {
  1252. t.Parallel()
  1253. const in1 = `
  1254. file_to_generate: "exampleproto/v1/example.proto"
  1255. parameter: "output_format=yaml,allow_delete_body=true"
  1256. proto_file: {
  1257. name: "exampleproto/v1/example.proto"
  1258. package: "example.v1"
  1259. message_type: {
  1260. name: "Foo"
  1261. field: {
  1262. name: "bar"
  1263. number: 1
  1264. label: LABEL_OPTIONAL
  1265. type: TYPE_STRING
  1266. json_name: "bar"
  1267. }
  1268. }
  1269. service: {
  1270. name: "TestServiceOne"
  1271. method: {
  1272. name: "Test1"
  1273. input_type: ".example.v1.Foo"
  1274. output_type: ".example.v1.Foo"
  1275. options: {
  1276. [google.api.http]: {
  1277. get: "/d/first"
  1278. }
  1279. }
  1280. }
  1281. method: {
  1282. name: "Test2"
  1283. input_type: ".example.v1.Foo"
  1284. output_type: ".example.v1.Foo"
  1285. options: {
  1286. [google.api.http]: {
  1287. get: "/e/second"
  1288. }
  1289. }
  1290. }
  1291. method: {
  1292. name: "Test3"
  1293. input_type: ".example.v1.Foo"
  1294. output_type: ".example.v1.Foo"
  1295. options: {
  1296. [google.api.http]: {
  1297. get: "/c/third"
  1298. }
  1299. }
  1300. }
  1301. }
  1302. service: {
  1303. name: "TestServiceTwo"
  1304. method: {
  1305. name: "Test1"
  1306. input_type: ".example.v1.Foo"
  1307. output_type: ".example.v1.Foo"
  1308. options: {
  1309. [google.api.http]: {
  1310. get: "/b/first"
  1311. }
  1312. }
  1313. }
  1314. method: {
  1315. name: "Test2"
  1316. input_type: ".example.v1.Foo"
  1317. output_type: ".example.v1.Foo"
  1318. options: {
  1319. [google.api.http]: {
  1320. get: "/a/second"
  1321. }
  1322. }
  1323. }
  1324. method: {
  1325. name: "Test3"
  1326. input_type: ".example.v1.Foo"
  1327. output_type: ".example.v1.Foo"
  1328. options: {
  1329. [google.api.http]: {
  1330. get: "/g/third"
  1331. }
  1332. }
  1333. }
  1334. }
  1335. options: {
  1336. go_package: "exampleproto/v1;exampleproto"
  1337. }
  1338. }`
  1339. const in2 = `
  1340. file_to_generate: "exampleproto/v2/example.proto"
  1341. parameter: "output_format=yaml,allow_delete_body=true"
  1342. proto_file: {
  1343. name: "exampleproto/v2/example.proto"
  1344. package: "example.v2"
  1345. message_type: {
  1346. name: "Foo"
  1347. field: {
  1348. name: "bar"
  1349. number: 1
  1350. label: LABEL_OPTIONAL
  1351. type: TYPE_STRING
  1352. json_name: "bar"
  1353. }
  1354. }
  1355. service: {
  1356. name: "TestService"
  1357. method: {
  1358. name: "Test1"
  1359. input_type: ".example.v2.Foo"
  1360. output_type: ".example.v2.Foo"
  1361. options: {
  1362. [google.api.http]: {
  1363. get: "/b/bpath"
  1364. additional_bindings {
  1365. get: "/a/additional"
  1366. }
  1367. }
  1368. }
  1369. }
  1370. method: {
  1371. name: "Test2"
  1372. input_type: ".example.v2.Foo"
  1373. output_type: ".example.v2.Foo"
  1374. options: {
  1375. [google.api.http]: {
  1376. get: "/a/apath"
  1377. additional_bindings {
  1378. get: "/z/zAdditional"
  1379. }
  1380. }
  1381. }
  1382. }
  1383. method: {
  1384. name: "Test3"
  1385. input_type: ".example.v2.Foo"
  1386. output_type: ".example.v2.Foo"
  1387. options: {
  1388. [google.api.http]: {
  1389. get: "/c/cpath"
  1390. additional_bindings {
  1391. get: "/b/bAdditional"
  1392. }
  1393. }
  1394. }
  1395. }
  1396. }
  1397. options: {
  1398. go_package: "exampleproto/v2;exampleproto"
  1399. }
  1400. }`
  1401. var req1, req2 pluginpb.CodeGeneratorRequest
  1402. if err := prototext.Unmarshal([]byte(in1), &req1); err != nil {
  1403. t.Fatalf("failed to marshall yaml: %s", err)
  1404. }
  1405. if err := prototext.Unmarshal([]byte(in2), &req2); err != nil {
  1406. t.Fatalf("failed to marshall yaml: %s", err)
  1407. }
  1408. req1.ProtoFile = append(req1.ProtoFile, req2.ProtoFile...)
  1409. req1.FileToGenerate = append(req1.FileToGenerate, req2.FileToGenerate...)
  1410. formats := [...]genopenapi.Format{
  1411. genopenapi.FormatJSON,
  1412. genopenapi.FormatYAML,
  1413. }
  1414. for _, format := range formats {
  1415. format := format
  1416. t.Run(string(format), func(t *testing.T) {
  1417. t.Parallel()
  1418. resp := requireGenerate(t, &req1, format, true, true)
  1419. if len(resp) != 1 {
  1420. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  1421. }
  1422. content := resp[0].GetContent()
  1423. t.Log(content)
  1424. contentsSlice := strings.Fields(content)
  1425. expectedPaths := []string{"/d/first", "/e/second", "/c/third",
  1426. "/b/first", "/a/second", "/g/third", "/b/bpath", "/a/additional",
  1427. "/a/apath", "/z/zAdditional", "/c/cpath", "/b/bAdditional"}
  1428. foundPaths := []string{}
  1429. for _, contentValue := range contentsSlice {
  1430. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  1431. }
  1432. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  1433. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  1434. }
  1435. })
  1436. }
  1437. }
  1438. func TestGenerateRPCOrderNotPreservedMergeFilesAdditionalBindingsMultipleServices(t *testing.T) {
  1439. t.Parallel()
  1440. const in1 = `
  1441. file_to_generate: "exampleproto/v1/example.proto"
  1442. parameter: "output_format=yaml,allow_delete_body=true"
  1443. proto_file: {
  1444. name: "exampleproto/v1/example.proto"
  1445. package: "example.v1"
  1446. message_type: {
  1447. name: "Foo"
  1448. field: {
  1449. name: "bar"
  1450. number: 1
  1451. label: LABEL_OPTIONAL
  1452. type: TYPE_STRING
  1453. json_name: "bar"
  1454. }
  1455. }
  1456. service: {
  1457. name: "TestServiceOne"
  1458. method: {
  1459. name: "Test1"
  1460. input_type: ".example.v1.Foo"
  1461. output_type: ".example.v1.Foo"
  1462. options: {
  1463. [google.api.http]: {
  1464. get: "/d/first"
  1465. }
  1466. }
  1467. }
  1468. method: {
  1469. name: "Test2"
  1470. input_type: ".example.v1.Foo"
  1471. output_type: ".example.v1.Foo"
  1472. options: {
  1473. [google.api.http]: {
  1474. get: "/e/second"
  1475. }
  1476. }
  1477. }
  1478. method: {
  1479. name: "Test3"
  1480. input_type: ".example.v1.Foo"
  1481. output_type: ".example.v1.Foo"
  1482. options: {
  1483. [google.api.http]: {
  1484. get: "/c/third"
  1485. }
  1486. }
  1487. }
  1488. }
  1489. service: {
  1490. name: "TestServiceTwo"
  1491. method: {
  1492. name: "Test1"
  1493. input_type: ".example.v1.Foo"
  1494. output_type: ".example.v1.Foo"
  1495. options: {
  1496. [google.api.http]: {
  1497. get: "/b/first"
  1498. }
  1499. }
  1500. }
  1501. method: {
  1502. name: "Test2"
  1503. input_type: ".example.v1.Foo"
  1504. output_type: ".example.v1.Foo"
  1505. options: {
  1506. [google.api.http]: {
  1507. get: "/a/second"
  1508. }
  1509. }
  1510. }
  1511. method: {
  1512. name: "Test3"
  1513. input_type: ".example.v1.Foo"
  1514. output_type: ".example.v1.Foo"
  1515. options: {
  1516. [google.api.http]: {
  1517. get: "/g/third"
  1518. }
  1519. }
  1520. }
  1521. }
  1522. options: {
  1523. go_package: "exampleproto/v1;exampleproto"
  1524. }
  1525. }`
  1526. const in2 = `
  1527. file_to_generate: "exampleproto/v2/example.proto"
  1528. parameter: "output_format=yaml,allow_delete_body=true"
  1529. proto_file: {
  1530. name: "exampleproto/v2/example.proto"
  1531. package: "example.v2"
  1532. message_type: {
  1533. name: "Foo"
  1534. field: {
  1535. name: "bar"
  1536. number: 1
  1537. label: LABEL_OPTIONAL
  1538. type: TYPE_STRING
  1539. json_name: "bar"
  1540. }
  1541. }
  1542. service: {
  1543. name: "TestService"
  1544. method: {
  1545. name: "Test1"
  1546. input_type: ".example.v2.Foo"
  1547. output_type: ".example.v2.Foo"
  1548. options: {
  1549. [google.api.http]: {
  1550. get: "/b/bpath"
  1551. additional_bindings {
  1552. get: "/a/additional"
  1553. }
  1554. }
  1555. }
  1556. }
  1557. method: {
  1558. name: "Test2"
  1559. input_type: ".example.v2.Foo"
  1560. output_type: ".example.v2.Foo"
  1561. options: {
  1562. [google.api.http]: {
  1563. get: "/a/apath"
  1564. additional_bindings {
  1565. get: "/z/zAdditional"
  1566. }
  1567. }
  1568. }
  1569. }
  1570. method: {
  1571. name: "Test3"
  1572. input_type: ".example.v2.Foo"
  1573. output_type: ".example.v2.Foo"
  1574. options: {
  1575. [google.api.http]: {
  1576. get: "/c/cpath"
  1577. additional_bindings {
  1578. get: "/b/bAdditional"
  1579. }
  1580. }
  1581. }
  1582. }
  1583. }
  1584. options: {
  1585. go_package: "exampleproto/v2;exampleproto"
  1586. }
  1587. }`
  1588. var req1, req2 pluginpb.CodeGeneratorRequest
  1589. if err := prototext.Unmarshal([]byte(in1), &req1); err != nil {
  1590. t.Fatalf("failed to marshall yaml: %s", err)
  1591. }
  1592. if err := prototext.Unmarshal([]byte(in2), &req2); err != nil {
  1593. t.Fatalf("failed to marshall yaml: %s", err)
  1594. }
  1595. req1.ProtoFile = append(req1.ProtoFile, req2.ProtoFile...)
  1596. req1.FileToGenerate = append(req1.FileToGenerate, req2.FileToGenerate...)
  1597. formats := [...]genopenapi.Format{
  1598. genopenapi.FormatJSON,
  1599. genopenapi.FormatYAML,
  1600. }
  1601. for _, format := range formats {
  1602. format := format
  1603. t.Run(string(format), func(t *testing.T) {
  1604. t.Parallel()
  1605. resp := requireGenerate(t, &req1, format, false, true)
  1606. if len(resp) != 1 {
  1607. t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
  1608. }
  1609. content := resp[0].GetContent()
  1610. t.Log(content)
  1611. contentsSlice := strings.Fields(content)
  1612. expectedPaths := []string{"/d/first", "/e/second", "/c/third",
  1613. "/b/first", "/a/second", "/g/third", "/b/bpath", "/a/additional",
  1614. "/a/apath", "/z/zAdditional", "/c/cpath", "/b/bAdditional"}
  1615. sort.Strings(expectedPaths)
  1616. foundPaths := []string{}
  1617. for _, contentValue := range contentsSlice {
  1618. findExpectedPaths(&foundPaths, expectedPaths, contentValue)
  1619. }
  1620. if allPresent := reflect.DeepEqual(foundPaths, expectedPaths); !allPresent {
  1621. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, expectedPaths)
  1622. }
  1623. })
  1624. }
  1625. }
  1626. // Tries to find expected paths from a provided substring and store them in the foundPaths
  1627. // slice.
  1628. func findExpectedPaths(foundPaths *[]string, expectedPaths []string, potentialPath string) {
  1629. seenPaths := map[string]struct{}{}
  1630. // foundPaths may not be empty when this function is called multiple times,
  1631. // so we add them to seenPaths map to avoid duplicates.
  1632. for _, path := range *foundPaths {
  1633. seenPaths[path] = struct{}{}
  1634. }
  1635. for _, path := range expectedPaths {
  1636. _, pathAlreadySeen := seenPaths[path]
  1637. if strings.Contains(potentialPath, path) && !pathAlreadySeen {
  1638. *foundPaths = append(*foundPaths, path)
  1639. seenPaths[path] = struct{}{}
  1640. }
  1641. }
  1642. }
  1643. func TestFindExpectedPaths(t *testing.T) {
  1644. t.Parallel()
  1645. testCases := [...]struct {
  1646. testName string
  1647. requiredPaths []string
  1648. potentialPath string
  1649. expectedPathsFound []string
  1650. }{
  1651. {
  1652. testName: "One potential path present",
  1653. requiredPaths: []string{"/d/first", "/e/second", "/c/third", "/b/first"},
  1654. potentialPath: "[{\"path: \"/d/first\"",
  1655. expectedPathsFound: []string{"/d/first"},
  1656. },
  1657. {
  1658. testName: "No potential Paths present",
  1659. requiredPaths: []string{"/d/first", "/e/second", "/c/third", "/b/first"},
  1660. potentialPath: "[{\"path: \"/z/zpath\"",
  1661. expectedPathsFound: []string{},
  1662. },
  1663. {
  1664. testName: "Multiple potential paths present",
  1665. requiredPaths: []string{"/d/first", "/e/second", "/c/third", "/b/first", "/d/first"},
  1666. potentialPath: "[{\"path: \"/d/first\"someData\"/c/third\"someData\"/b/third\"",
  1667. expectedPathsFound: []string{"/d/first", "/c/third"},
  1668. },
  1669. }
  1670. for _, tc := range testCases {
  1671. tc := tc
  1672. t.Run(tc.testName, func(t *testing.T) {
  1673. t.Parallel()
  1674. foundPaths := []string{}
  1675. findExpectedPaths(&foundPaths, tc.requiredPaths, tc.potentialPath)
  1676. if correctPathsFound := reflect.DeepEqual(foundPaths, tc.expectedPathsFound); !correctPathsFound {
  1677. t.Fatalf("Found paths differed from expected paths. Got: %#v, want %#v", foundPaths, tc.expectedPathsFound)
  1678. }
  1679. })
  1680. }
  1681. }