@@ -49,35 +49,36 @@ import (
49
49
"github.com/googleapis/gnostic/discovery"
50
50
"github.com/googleapis/gnostic/jsonwriter"
51
51
plugins "github.com/googleapis/gnostic/plugins"
52
+ surface "github.com/googleapis/gnostic/surface"
52
53
"gopkg.in/yaml.v2"
53
54
)
54
55
55
- const ( // OpenAPI Version
56
- openAPIvUnknown = 0
57
- openAPIv2 = 2
58
- openAPIv3 = 3
59
- discoveryFormat = 4
56
+ const ( // Source Format
57
+ SourceFormatUnknown = 0
58
+ SourceFormatOpenAPI2 = 2
59
+ SourceFormatOpenAPI3 = 3
60
+ SourceFormatDiscovery = 4
60
61
)
61
62
62
63
// Determine the version of an OpenAPI description read from JSON or YAML.
63
64
func getOpenAPIVersionFromInfo (info interface {}) int {
64
65
m , ok := compiler .UnpackMap (info )
65
66
if ! ok {
66
- return openAPIvUnknown
67
+ return SourceFormatUnknown
67
68
}
68
69
swagger , ok := compiler .MapValueForKey (m , "swagger" ).(string )
69
70
if ok && strings .HasPrefix (swagger , "2.0" ) {
70
- return openAPIv2
71
+ return SourceFormatOpenAPI2
71
72
}
72
73
openapi , ok := compiler .MapValueForKey (m , "openapi" ).(string )
73
74
if ok && strings .HasPrefix (openapi , "3.0" ) {
74
- return openAPIv3
75
+ return SourceFormatOpenAPI3
75
76
}
76
77
kind , ok := compiler .MapValueForKey (m , "kind" ).(string )
77
78
if ok && kind == "discovery#restDescription" {
78
- return discoveryFormat
79
+ return SourceFormatDiscovery
79
80
}
80
- return openAPIvUnknown
81
+ return SourceFormatUnknown
81
82
}
82
83
83
84
const (
@@ -91,7 +92,7 @@ type pluginCall struct {
91
92
}
92
93
93
94
// Invokes a plugin.
94
- func (p * pluginCall ) perform (document proto.Message , openAPIVersion int , sourceName string ) error {
95
+ func (p * pluginCall ) perform (document proto.Message , sourceFormat int , sourceName string ) error {
95
96
if p .Name != "" {
96
97
request := & plugins.Request {}
97
98
@@ -142,13 +143,16 @@ func (p *pluginCall) perform(document proto.Message, openAPIVersion int, sourceN
142
143
request .OutputPath = outputLocation
143
144
144
145
request .SourceName = sourceName
145
- switch openAPIVersion {
146
- case openAPIv2 :
146
+ switch sourceFormat {
147
+ case SourceFormatOpenAPI2 :
147
148
request .Openapi2 = document .(* openapi_v2.Document )
148
- case openAPIv3 :
149
+ request .Surface , _ = surface .NewModelFromOpenAPI2 (request .Openapi2 )
150
+ case SourceFormatOpenAPI3 :
149
151
request .Openapi3 = document .(* openapi_v3.Document )
152
+ request .Surface , _ = surface .NewModelFromOpenAPI3 (request .Openapi3 )
150
153
default :
151
154
}
155
+
152
156
requestBytes , _ := proto .Marshal (request )
153
157
154
158
cmd := exec .Command (executableName , "-plugin" )
@@ -232,7 +236,7 @@ type Gnostic struct {
232
236
resolveReferences bool
233
237
pluginCalls []* pluginCall
234
238
extensionHandlers []compiler.ExtensionHandler
235
- openAPIVersion int
239
+ sourceFormat int
236
240
}
237
241
238
242
// Initialize a structure to store global application state.
@@ -340,18 +344,18 @@ func (g *Gnostic) readOpenAPIText(bytes []byte) (message proto.Message, err erro
340
344
return nil , err
341
345
}
342
346
// Determine the OpenAPI version.
343
- g .openAPIVersion = getOpenAPIVersionFromInfo (info )
344
- if g .openAPIVersion == openAPIvUnknown {
347
+ g .sourceFormat = getOpenAPIVersionFromInfo (info )
348
+ if g .sourceFormat == SourceFormatUnknown {
345
349
return nil , errors .New ("unable to identify OpenAPI version" )
346
350
}
347
351
// Compile to the proto model.
348
- if g .openAPIVersion == openAPIv2 {
352
+ if g .sourceFormat == SourceFormatOpenAPI2 {
349
353
document , err := openapi_v2 .NewDocument (info , compiler .NewContextWithExtensions ("$root" , nil , & g .extensionHandlers ))
350
354
if err != nil {
351
355
return nil , err
352
356
}
353
357
message = document
354
- } else if g .openAPIVersion == openAPIv3 {
358
+ } else if g .sourceFormat == SourceFormatOpenAPI3 {
355
359
document , err := openapi_v3 .NewDocument (info , compiler .NewContextWithExtensions ("$root" , nil , & g .extensionHandlers ))
356
360
if err != nil {
357
361
return nil , err
@@ -373,21 +377,21 @@ func (g *Gnostic) readOpenAPIBinary(data []byte) (message proto.Message, err err
373
377
documentV3 := & openapi_v3.Document {}
374
378
err = proto .Unmarshal (data , documentV3 )
375
379
if err == nil && strings .HasPrefix (documentV3 .Openapi , "3.0" ) {
376
- g .openAPIVersion = openAPIv3
380
+ g .sourceFormat = SourceFormatOpenAPI3
377
381
return documentV3 , nil
378
382
}
379
383
// if that failed, try to read an OpenAPI v2 document
380
384
documentV2 := & openapi_v2.Document {}
381
385
err = proto .Unmarshal (data , documentV2 )
382
386
if err == nil && strings .HasPrefix (documentV2 .Swagger , "2.0" ) {
383
- g .openAPIVersion = openAPIv2
387
+ g .sourceFormat = SourceFormatOpenAPI2
384
388
return documentV2 , nil
385
389
}
386
390
// if that failed, try to read a Discovery Format document
387
391
discoveryDocument := & discovery_v1.Document {}
388
392
err = proto .Unmarshal (data , discoveryDocument )
389
393
if err == nil { // && strings.HasPrefix(documentV2.Swagger, "2.0") {
390
- g .openAPIVersion = discoveryFormat
394
+ g .sourceFormat = SourceFormatDiscovery
391
395
return discoveryDocument , nil
392
396
}
393
397
return nil , err
@@ -416,19 +420,19 @@ func (g *Gnostic) writeJSONYAMLOutput(message proto.Message) {
416
420
var rawInfo yaml.MapSlice
417
421
var ok bool
418
422
var err error
419
- if g .openAPIVersion == openAPIv2 {
423
+ if g .sourceFormat == SourceFormatOpenAPI2 {
420
424
document := message .(* openapi_v2.Document )
421
425
rawInfo , ok = document .ToRawInfo ().(yaml.MapSlice )
422
426
if ! ok {
423
427
rawInfo = nil
424
428
}
425
- } else if g .openAPIVersion == openAPIv3 {
429
+ } else if g .sourceFormat == SourceFormatOpenAPI3 {
426
430
document := message .(* openapi_v3.Document )
427
431
rawInfo , ok = document .ToRawInfo ().(yaml.MapSlice )
428
432
if ! ok {
429
433
rawInfo = nil
430
434
}
431
- } else if g .openAPIVersion == discoveryFormat {
435
+ } else if g .sourceFormat == SourceFormatDiscovery {
432
436
document := message .(* discovery_v1.Document )
433
437
rawInfo , ok = document .ToRawInfo ().(yaml.MapSlice )
434
438
if ! ok {
@@ -467,10 +471,10 @@ func (g *Gnostic) writeJSONYAMLOutput(message proto.Message) {
467
471
func (g * Gnostic ) performActions (message proto.Message ) (err error ) {
468
472
// Optionally resolve internal references.
469
473
if g .resolveReferences {
470
- if g .openAPIVersion == openAPIv2 {
474
+ if g .sourceFormat == SourceFormatOpenAPI2 {
471
475
document := message .(* openapi_v2.Document )
472
476
_ , err = document .ResolveReferences (g .sourceName )
473
- } else if g .openAPIVersion == openAPIv3 {
477
+ } else if g .sourceFormat == SourceFormatOpenAPI3 {
474
478
document := message .(* openapi_v3.Document )
475
479
_ , err = document .ResolveReferences (g .sourceName )
476
480
}
@@ -492,7 +496,7 @@ func (g *Gnostic) performActions(message proto.Message) (err error) {
492
496
}
493
497
// Call all specified plugins.
494
498
for _ , p := range g .pluginCalls {
495
- err := p .perform (message , g .openAPIVersion , g .sourceName )
499
+ err := p .perform (message , g .sourceFormat , g .sourceName )
496
500
if err != nil {
497
501
writeFile (g .errorOutputPath , g .errorBytes (err ), g .sourceName , "errors" )
498
502
defer os .Exit (- 1 ) // run all plugins, even when some have errors
0 commit comments