added vendors

This commit is contained in:
mickymiek
2018-12-19 15:29:25 +01:00
parent 12e6881669
commit 8ee6bc4b91
2952 changed files with 1124359 additions and 1 deletions

View File

@ -0,0 +1,18 @@
TGT=gnostic-lint-responses-swift
BINDIR=.build/debug
all:
swift build
install: all
cp $(BINDIR)/$(TGT) $(GOPATH)/bin/$(TGT)
clean :
rm -rf Packages
rm -rf .build
rm -f Package.pins Package.resolved
run:
gnostic ../../../examples/v2.0/yaml/petstore.yaml --lint-responses-swift

View File

@ -0,0 +1,26 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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.
import PackageDescription
let package = Package(
name: "gnostic-lint-responses-swift",
targets: [
Target(name: "gnostic-lint-responses-swift", dependencies: [ "Gnostic" ]),
Target(name: "Gnostic")
],
dependencies: [
.Package(url: "https://github.com/apple/swift-protobuf.git", Version(1,0,2))
]
)

View File

@ -0,0 +1,12 @@
This directory contains a gnostic linter written with Swift.
### SETUP
- Install protoc (https://github.com/google/protobuf)
- Install the Swift Protocol Buffer plugin (https://github.com/apple/swift-protobuf)
- Run `sh compile-protos` to generate Protocol Buffer support code.
- Run `make install` to build and install the plugin.
### TRY IT
- Run `make run` to test-run the plugin.

View File

@ -0,0 +1,77 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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.
import Foundation
// The I/O code below is derived from Apple's swift-protobuf project.
// https://github.com/apple/swift-protobuf
// BEGIN swift-protobuf derivation
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
enum PluginError: Error {
/// Raised for any errors reading the input
case readFailure
}
// Alias clib's write() so Stdout.write(bytes:) can call it.
private let _write = write
class Stdin {
static func readall() throws -> Data {
let fd: Int32 = 0
let buffSize = 32
var buff = [UInt8]()
while true {
var fragment = [UInt8](repeating: 0, count: buffSize)
let count = read(fd, &fragment, buffSize)
if count < 0 {
throw PluginError.readFailure
}
if count < buffSize {
buff += fragment[0..<count]
return Data(bytes: buff)
}
buff += fragment
}
}
}
class Stdout {
static func write(bytes: Data) {
bytes.withUnsafeBytes { (p: UnsafePointer<UInt8>) -> () in
_ = _write(1, p, bytes.count)
}
}
static func write(_ string: String) {
self.write(bytes:string.data(using:.utf8)!)
}
}
class Stderr {
static func write(bytes: Data) {
bytes.withUnsafeBytes { (p: UnsafePointer<UInt8>) -> () in
_ = _write(2, p, bytes.count)
}
}
static func write(_ string: String) {
self.write(bytes:string.data(using:.utf8)!)
}
}
// END swift-protobuf derivation

View File

@ -0,0 +1,116 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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.
import Foundation
import Gnostic
extension Gnostic_Plugin_V1_Response {
mutating func message(level:Gnostic_Plugin_V1_Message.Level,
code:String,
text:String,
path:[String]=[]) {
var message = Gnostic_Plugin_V1_Message()
message.level = level
message.code = code
message.text = text
message.keys = path
messages.append(message)
}
}
class ResponseLinter {
var document : Openapi_V2_Document = Openapi_V2_Document()
func run(_ request : Gnostic_Plugin_V1_Request,
_ response : inout Gnostic_Plugin_V1_Response) throws {
for model in request.models {
if model.typeURL == "openapi.v2.Document" {
let document = try Openapi_V2_Document(serializedData: model.value)
self.document = document
for pair in document.paths.path {
let path = ["paths", pair.name]
let v = pair.value
if v.hasGet {
checkOperation(v.get, path:path + ["get"], response:&response)
}
if v.hasPost {
checkOperation(v.post, path:path + ["post"], response:&response)
}
if v.hasPut {
checkOperation(v.put, path:path + ["put"], response:&response)
}
if v.hasDelete {
checkOperation(v.delete, path:path + ["delete"], response:&response)
}
}
}
}
}
func checkOperation(_ operation:Openapi_V2_Operation,
path:[String],
response:inout Gnostic_Plugin_V1_Response) {
for responseCode in operation.responses.responseCode {
let code = responseCode.name
if responseCode.value.response.hasSchema {
var schema = responseCode.value.response.schema.schema
if schema.ref != "" {
if let resolvedSchema = resolveReference(schema.ref) {
schema = resolvedSchema
}
}
checkSchemaType(schema, path: path + ["responses", code, "schema"], response: &response)
}
}
}
func checkSchemaType(_ schema:Openapi_V2_Schema,
path:[String],
response:inout Gnostic_Plugin_V1_Response) {
if schema.hasType {
for type in schema.type.value {
if type == "array" {
response.message(
level: .error,
code: "NO_ARRAY_RESPONSES",
text: "Arrays MUST NOT be returned as the top-level structure in a response body.",
path: path)
}
}
}
}
func resolveReference(_ reference:String) -> Openapi_V2_Schema? {
let prefix = "#/definitions/"
if reference.hasPrefix(prefix) {
let schemaName = reference.dropFirst(prefix.count)
for pair in document.definitions.additionalProperties {
if pair.name == schemaName {
return pair.value
}
}
}
return nil
}
}
func main() throws {
let request = try Gnostic_Plugin_V1_Request(serializedData: Stdin.readall())
var response = Gnostic_Plugin_V1_Response()
try ResponseLinter().run(request, &response)
let serializedResponse = try response.serializedData()
Stdout.write(bytes: serializedResponse)
}
try main()

View File

@ -0,0 +1,54 @@
#!/bin/sh
#
# Copyright 2018 Google Inc. All Rights Reserved.
#
# 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.
# Use this script to run protoc and swift-proto to generate
# support code for gnostic protos.
GNOSTIC=$GOPATH/src/github.com/googleapis/gnostic
PROTOS=(
plugins/plugin.proto
OpenAPIv2/OpenAPIv2.proto
OpenAPIv3/OpenAPIv3.proto
surface/surface.proto
discovery/discovery.proto
)
mkdir -p Sources/Gnostic
# remove old compiled pb files
rm -rf Sources/Gnostic/*.pb.swift
# remove any prior compilations
rm -rf Sources/Gnostic/github.com
# compile protos
for proto in "${PROTOS[@]}"
do
echo "COMPILING $proto"
protoc $GNOSTIC/$proto \
--swift_opt=Visibility=Public \
--swift_out=Sources/Gnostic \
--proto_path=$GOPATH/src
# relocate compiled protos
find Sources/Gnostic/github.com -name "*.pb.swift" -exec mv {} Sources/Gnostic \;
# remove scaffolding of compilation
rm -rf Sources/Gnostic/github.com
done