├── version.sexp ├── .gitignore ├── code ├── globals.lisp ├── convert.lisp ├── package.lisp ├── json-mop.lisp ├── collections.lisp ├── util.lisp ├── parser.lisp ├── openapi-generator.lisp └── function-generation.lisp ├── test └── test.lisp ├── openapi-generator.asd ├── README.org └── COPYING /version.sexp: -------------------------------------------------------------------------------- 1 | ;; -*- lisp -*- 2 | "0.0.2" 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ---> CommonLisp 2 | *.FASL 3 | *.fasl 4 | *.lisp-temp 5 | *.dfsl 6 | *.pfsl 7 | *.d64fsl 8 | *.p64fsl 9 | *.lx64fsl 10 | *.lx32fsl 11 | *.dx64fsl 12 | *.dx32fsl 13 | *.fx64fsl 14 | *.fx32fsl 15 | *.sx64fsl 16 | *.sx32fsl 17 | *.wx64fsl 18 | *.wx32fsl 19 | 20 | projects/ 21 | data/ 22 | *~ 23 | *# 24 | -------------------------------------------------------------------------------- /code/globals.lisp: -------------------------------------------------------------------------------- 1 | (cl:in-package #:openapi-generator) 2 | 3 | (define-constant constant-data-directory 4 | (system-relative-pathname "openapi-generator" "data/") 5 | :test (function equal)) 6 | 7 | (define-constant constant-projects-directory 8 | (system-relative-pathname "openapi-generator" "projects/") 9 | :test (function equal) 10 | :documentation "Projects directory within libraries data folder") 11 | 12 | (defvar *dereference* t 13 | "This variable influences whether the openapi document will be derefenced or not.") 14 | 15 | (defparameter *converter-url* "https://converter.swagger.io" 16 | "Default swagger converter url.") 17 | -------------------------------------------------------------------------------- /test/test.lisp: -------------------------------------------------------------------------------- 1 | (uiop:define-package #:openapi-generator/test 2 | (:use #:cl) 3 | (:import-from #:fiveam 4 | #:def-suite 5 | #:def-test 6 | #:finishes 7 | #:is-true) 8 | (:shadowing-import-from #:str 9 | #:concat 10 | #:downcase) 11 | (:import-from #:openapi-generator 12 | #:parse-openapi 13 | #:make-openapi-client) 14 | (:shadow #:type) 15 | (:export #:run! 16 | #:test-suite)) 17 | 18 | (in-package #:openapi-generator/test) 19 | 20 | (def-suite test-suite :description "Main Test Suite for Openapi-generator") 21 | 22 | (def-test parsing (:suite test-suite) 23 | (finishes (parse-openapi "openai" 24 | :url "https://raw.githubusercontent.com/hirosystems/stacks-blockchain-api/gh-pages/openapi.resolved.yaml"))) 25 | 26 | 27 | (def-test make-openapi-client (:suite test-suite) 28 | (finishes (make-openapi-client "codeberg" 29 | :url "https://codeberg.org/swagger.v1.json" 30 | :server "https://codeberg.org/api/v1" 31 | :parse t)) 32 | (is-true (funcall (uiop:find-symbol* '#:repo-get '#:codeberg) "kilianmh" "openapi-generator"))) 33 | -------------------------------------------------------------------------------- /openapi-generator.asd: -------------------------------------------------------------------------------- 1 | (asdf:defsystem :openapi-generator 2 | :author "Kilian M. Haemmerle" 3 | :mailto "kilian.haemmerle@protonmail.com" 4 | :version (:read-file-form "version.sexp") 5 | :description "Parse OpenAPI into CLOS object for client generation" 6 | :license "AGPLv3-later" 7 | :depends-on (#:str #:cl-hash-util #:cl-semver #:pathname-utils 8 | #:json-mop #:yason #:com.inuoe.jzon #:cl-project 9 | #:listopia #:alexandria #:serapeum #:quri #:dexador 10 | #:cl-json-pointer #:moptilities #:parse-float #:cl-yaml) 11 | :pathname "code/" 12 | :serial t 13 | :components ((:file "package") 14 | (:file "json-mop") 15 | (:file "globals") 16 | (:file "util") 17 | (:file "classes") 18 | (:file "collections") 19 | (:file "convert") 20 | (:file "parser") 21 | (:file "function-generation") 22 | (:file "openapi-generator")) 23 | :in-order-to ((test-op (test-op "openapi-generator/test")))) 24 | 25 | (asdf:defsystem "openapi-generator/test" 26 | :description "Test suite for the openapi-generator library" 27 | :depends-on (openapi-generator str fiveam) 28 | :pathname "test/" 29 | :serial t 30 | :components ((:file "test")) 31 | :perform (asdf:test-op (o s) 32 | (uiop:symbol-call '#:fiveam '#:run! 33 | (uiop:find-symbol* '#:test-suite 34 | '#:openapi-generator/test)))) 35 | -------------------------------------------------------------------------------- /code/convert.lisp: -------------------------------------------------------------------------------- 1 | (cl:in-package :openapi-generator) 2 | 3 | (defun convert-by-url (url &key (converter-url *converter-url*)) 4 | (dex:get 5 | (render-uri 6 | (quri:merge-uris (concat "api/convert?url=" 7 | (quri:url-encode url)) 8 | converter-url)))) 9 | 10 | (defun convert-by-content (&key content (content-type "application/json") 11 | (converter-url *converter-url*)) 12 | (dex:post 13 | (quri:render-uri (quri:merge-uris "api/convert" converter-url)) 14 | :content content 15 | :headers (remove-empty-values 16 | (list 17 | (cons "Content-Type" 18 | (cond 19 | (content-type 20 | (if (find content-type 21 | (list "application/json" "application/yaml") 22 | :test (function string-equal)) 23 | content-type 24 | (warn 25 | "The body type ~A is not mentioned. Valid content-types are: ~A" 26 | content-type "application/json application/yaml"))) 27 | (t "application/json"))))))) 28 | 29 | (defun convert-to-openapi-3 (&key url content pathname (content-type "json") (converter-url *converter-url*)) 30 | (when (and url content pathname) 31 | (error "You can only supply either url or content, not both simultaneously.")) 32 | (let ((content-type 33 | (case-using (function string-equal) content-type 34 | ("json" 35 | "application/json") 36 | (("yaml" "yml") 37 | "application/yaml")))) 38 | (cond (url 39 | (convert-by-url url :converter-url converter-url)) 40 | (content 41 | (convert-by-content :content content :content-type content-type :converter-url converter-url)) 42 | (pathname 43 | (convert-by-content :content (from-file pathname) :content-type content-type :converter-url converter-url)) 44 | (t 45 | (error "You have to supply either url, content, or pathname."))))) 46 | -------------------------------------------------------------------------------- /code/package.lisp: -------------------------------------------------------------------------------- 1 | (uiop:define-package openapi-generator 2 | (:use #:cl) 3 | (:shadowing-import-from #:closer-mop 4 | #:defclass #:defgeneric #:slot-definition-name) 5 | (:import-from #:uiop 6 | #:default-temporary-directory #:delete-file-if-exists 7 | #:read-file-string #:file-exists-p #:ensure-pathname) 8 | (:import-from #:asdf 9 | #:system-relative-pathname #:load-system) 10 | (:import-from #:cl-project 11 | #:make-project) 12 | (:import-from #:alexandria 13 | #:length= #:define-constant #:string-designator #:when-let #:if-let) 14 | (:import-from #:json-mop 15 | #:json-serializable-class #:json-to-clos) 16 | (:import-from #:yason 17 | #:true) 18 | (:import-from #:str 19 | #:param-case #:downcase #:upcase #:concat #:param-case 20 | #:emptyp #:containsp #:ends-with-p #:starts-with-p 21 | #:split #:substring #:replace-first #:replace-using #:unwords 22 | #:trim #:trim-left #:string-case) 23 | (:import-from #:com.inuoe.jzon 24 | #:parse 25 | #:stringify) 26 | (:import-from #:listopia 27 | #:intersperse) 28 | (:import-from #:quri 29 | #:uri #:uri-scheme #:uri-host #:uri-port #:uri-path 30 | #:make-uri #:render-uri #:merge-uris) 31 | (:import-from #:dexador 32 | #:request) 33 | (:import-from #:pathname-utils 34 | #:file-type) 35 | (:import-from #:serapeum 36 | #:case-using #:ecase-using #:slot-value-safe #:dict #:assuref) 37 | (:import-from #:cl-hash-util 38 | #:hash-keys) 39 | (:import-from #:semver 40 | #:read-version-from-string 41 | #:version= #:version>= #:version<=) 42 | (:import-from #:cl-json-pointer 43 | #:get-by-json-pointer) 44 | (:import-from #:cl-hash-util 45 | #:hash-get) 46 | (:import-from #:parse-float 47 | #:parse-float) 48 | (:export #:parse-openapi 49 | #:generate-client 50 | #:make-openapi-client 51 | #:*dereference* 52 | #:dereference 53 | #:*converter-url* 54 | #:convert-to-openapi-3)) 55 | -------------------------------------------------------------------------------- /code/json-mop.lisp: -------------------------------------------------------------------------------- 1 | (in-package :json-mop) 2 | 3 | (defmethod to-lisp-value ((value hash-table) (json-type (eql :any))) 4 | "When the JSON type is :ANY, Pass the hash-table VALUE unchanged" 5 | value) 6 | 7 | (defmethod to-lisp-value ((value list) (json-type cons)) 8 | "When the JSON type is :ANY, Pass the hash-table VALUE unchanged" 9 | (funcall (function to-lisp-value) (coerce value 'vector) json-type)) 10 | 11 | (defmethod to-lisp-value ((value null) (json-type (eql :bool))) 12 | value) 13 | 14 | (defmethod to-lisp-value ((value (eql t)) (json-type (eql :bool))) 15 | value) 16 | 17 | (defclass json-serializable-slot (closer-mop:standard-direct-slot-definition) 18 | ((json-key :initarg :json-key 19 | :initform nil 20 | :reader json-key-name) 21 | (json-type :initarg :json-type 22 | :initform :any 23 | :reader json-type) 24 | (required :initarg :required 25 | :initform nil 26 | :reader required))) 27 | 28 | (defun initialize-slots-from-json (input lisp-object class-obj &optional (key-count 0)) 29 | "Initializes all slots from LISP-OBJECT from INPUT. 30 | All slots, direct or inherited, that exist in class CLASS-OBJ are considered." 31 | (loop for superclass in (closer-mop:class-direct-superclasses class-obj) 32 | unless (eql superclass (find-class (quote json-serializable))) 33 | do (setf (values lisp-object key-count) 34 | (initialize-slots-from-json input lisp-object superclass key-count))) 35 | 36 | (loop for slot in (closer-mop:class-direct-slots class-obj) 37 | do (let ((json-key-name 38 | (json-key-name slot))) 39 | (when json-key-name 40 | (handler-case 41 | (progn 42 | (setf (slot-value lisp-object 43 | (closer-mop:slot-definition-name slot)) 44 | (to-lisp-value (gethash json-key-name input :null) 45 | (json-type slot))) 46 | (incf key-count)) 47 | (null-value (condition) 48 | (declare (ignore condition)) nil))))) 49 | (values lisp-object key-count)) 50 | 51 | (defmethod json-to-clos ((input hash-table) class &rest initargs) 52 | (multiple-value-bind (lisp-object key-count) 53 | (initialize-slots-from-json input 54 | (apply (function make-instance) class initargs) 55 | (find-class class)) 56 | (when (zerop key-count) 57 | (warn 'no-values-parsed 58 | :hash-table input 59 | :class-name class)) 60 | (values lisp-object key-count))) 61 | 62 | (defmethod json-to-clos ((input list) class &rest initargs) 63 | (declare (ignore initargs)) 64 | (mapcar (lambda (element) 65 | (json-to-clos element class)) 66 | input)) 67 | 68 | (defmethod json-to-clos :after ((input hash-table) class &rest initargs) 69 | "Check if all required slots are present in input." 70 | (declare (ignore initargs)) 71 | (let ((class-object 72 | (find-class class))) 73 | (declare (type json-serializable-class class-object)) 74 | (loop for slot in (closer-mop:class-direct-slots class-object) 75 | do (when (required slot) 76 | (let ((json-key-name 77 | (json-key-name slot))) 78 | (declare (type string json-key-name)) 79 | (unless (gethash json-key-name input) 80 | (error (concatenate 'string "The key \"" json-key-name "\" is not present in " (format nil "~A" input) 81 | ", but required in "(format nil "~A" class-object)".")))))))) 82 | 83 | (defmethod to-lisp-value ((value hash-table) (json-type cons)) 84 | (destructuring-bind (type value-type) 85 | json-type 86 | (ecase type 87 | (:hash-table 88 | (let ((hash-table 89 | (make-hash-table :size (hash-table-size value) 90 | :test (function equal)))) 91 | (maphash (function (lambda (key value) 92 | (setf (gethash key 93 | hash-table) 94 | (to-lisp-value value 95 | value-type)))) 96 | value) 97 | hash-table))))) 98 | -------------------------------------------------------------------------------- /code/collections.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:openapi-generator) 2 | 3 | (define-json-class apis-guru-list nil 4 | (("api-list" (:hash-table apis-guru-api) :reader api-list))) 5 | 6 | (define-json-class apis-guru-api nil 7 | (("added" :string 8 | :documentation "Timestamp when the API was first added to the directory") 9 | ("preferred" :string 10 | :reader preferred 11 | :documentation "Recommended version") 12 | ("versions" (:hash-table apis-guru-api-version) 13 | :reader versions))) 14 | 15 | (defmethod print-object ((object apis-guru-api) stream) 16 | (print-unreadable-object (object stream :type t) 17 | (cl:format stream "updated: ~A, version: ~A" 18 | (substring 0 10 (updated (gethash (preferred object) (versions object)))) 19 | (preferred object)))) 20 | 21 | (define-json-class apis-guru-api-version nil 22 | (("added" :string 23 | :documentation "Timestamp when the version was added") 24 | ("externalDocs" apis-guru-external-documentation 25 | :documentation "Copy of `externalDocs` s…from OpenAPI definition") 26 | ("info" apis-guru-info 27 | :reader info 28 | :documentation "Copy of `info` section from OpenAPI definition") 29 | ("swaggerURL" :string 30 | :documentation "URL to OpenAPI definition in JSON format") 31 | ("swaggerYamlUrl" :string 32 | :documentation "URL to OpenAPI definition in YAML format") 33 | ("updated" :any ; should be string or nil 34 | :reader updated 35 | :documentation "Timestamp when the version was updated") 36 | ("openapiVer" :string))) 37 | 38 | (define-json-class apis-guru-external-documentation (external-documentation) 39 | (("updated" :string))) 40 | 41 | (define-json-class apis-guru-info nil 42 | (("contact" :hash-table) 43 | ("description" :string) 44 | ("title" :string) 45 | ("version" :string) 46 | ("x-apisguru-categories" (:list :string)) 47 | ("x-logo" apis-guru-url) 48 | ("x-origin" (:list apis-guru-origin) :reader x-origin) 49 | ("x-providerName" :string))) 50 | 51 | (define-json-class apis-guru-url nil 52 | (("url" :string :reader url))) 53 | 54 | (define-json-class apis-guru-origin nil 55 | (("format" :string :reader apis-guru-origin-format) 56 | ("url" :string :reader url) 57 | ("version" :string))) 58 | 59 | (eval-when (:compile-toplevel :load-toplevel :execute) 60 | (defun get-apis-guru-list () 61 | "https://api.apis.guru/v2/" 62 | (dex:get 63 | (render-uri (make-uri :scheme "https" :host "api.apis.guru" 64 | :path "v2/list.json")))) 65 | 66 | (defun get-api-guru-apis (&key refresh) 67 | (let ((file-path 68 | (system-relative-pathname "openapi-generator" 69 | "data/apis-guru-apis.json"))) 70 | (when refresh 71 | (delete-file-if-exists file-path)) 72 | (ensure-directories-exist file-path) 73 | (let* ((content-hash-table 74 | (yason:parse 75 | (if (file-exists-p file-path) 76 | (from-file file-path) 77 | (to-file file-path 78 | (get-apis-guru-list))))) 79 | (apis-guru-apis-object 80 | (api-list (json-to-clos (dict "api-list" 81 | content-hash-table) 82 | (quote apis-guru-list))))) 83 | apis-guru-apis-object)))) 84 | 85 | (defparameter apis-guru-apis 86 | (get-api-guru-apis)) 87 | 88 | (defun apis-guru-url (api-name &key (origin t)) 89 | (let ((api-object 90 | (gethash api-name apis-guru-apis))) 91 | (if api-object 92 | (let* ((preferred-version 93 | (gethash (preferred api-object) 94 | (versions api-object))) 95 | (origin-url 96 | (url (car (x-origin (info preferred-version))))) 97 | (swagger-url 98 | (slot-value-safe preferred-version (quote swagger-url))) 99 | (swagger-yaml-url 100 | (slot-value-safe preferred-version (quote swagger-yaml-url)))) 101 | (cond (origin 102 | (return-from apis-guru-url 103 | origin-url)) 104 | (swagger-url 105 | swagger-url) 106 | (swagger-yaml-url 107 | swagger-yaml-url) 108 | (t 109 | origin-url))) 110 | (error (concat "The id " api-name " does not exist in the apis-guru database."))))) 111 | -------------------------------------------------------------------------------- /code/util.lisp: -------------------------------------------------------------------------------- 1 | (in-package :openapi-generator) 2 | 3 | (defun to-file (pathname s) 4 | "Adapted from cl-str. 5 | Write string `s' to file `pathname'. If the file does not exist, create it (use `:if-does-not-exist'), if it already exists, replace its content (`:if-exists'). 6 | 7 | Returns the string written to file." 8 | (with-open-file (f pathname :direction :output :if-exists :supersede :if-does-not-exist :create :external-format :utf-8) 9 | (write-sequence s f))) 10 | 11 | (defun from-file (pathname) 12 | "Adapted from cl-str. 13 | Read the file and return its content as a string. 14 | 15 | It simply uses uiop:read-file-string. There is also uiop:read-file-lines." 16 | (funcall (function uiop:read-file-string) pathname :external-format :utf-8)) 17 | 18 | (defun slot-option-p (item) 19 | (member item (list :reader :write :accessor :allocation 20 | :initarg :initform :type :documentation 21 | :json-type :json-key :required))) 22 | 23 | (deftype slot-option () 24 | '(and keyword (satisfies slot-option-p))) 25 | 26 | (defun json-mop-type-p (item) 27 | (member item (list :any :string :integer :number :hash-table 28 | :vector :list :bool))) 29 | 30 | (deftype json-mop-type () 31 | (quote (and keyword (satisfies json-mop-type-p)))) 32 | 33 | (deftype non-keyword-symbol () 34 | (quote (and symbol (not keyword)))) 35 | 36 | (defun json-mop-composite-type-p (item) 37 | (and (member (first item) (list :hash-table :vector :list)) 38 | (typep (second item) '(or json-mop-type 39 | non-keyword-symbol)))) 40 | 41 | (deftype json-mop-composite-type () 42 | (quote 43 | (and cons (satisfies json-mop-composite-type-p)))) 44 | 45 | (defmacro define-json-class (name direct-superclasses direct-slots &rest options) 46 | (flet ((expand-slot (slot-specifier) 47 | (etypecase slot-specifier 48 | (string 49 | (list (intern (string-upcase (str:param-case slot-specifier))) 50 | :json-key slot-specifier)) 51 | (cons 52 | (let ((first 53 | (first slot-specifier)) 54 | (second 55 | (second slot-specifier))) 56 | (etypecase first 57 | (symbol 58 | (etypecase second 59 | (slot-option 60 | slot-specifier) 61 | (string 62 | (let ((third 63 | (third slot-specifier))) 64 | (etypecase third 65 | (slot-option 66 | (list* first :json-key second (cddr slot-specifier))) 67 | ((or json-mop-type non-keyword-symbol json-mop-composite-type) 68 | (list* first :json-key second :json-type third 69 | (cdddr slot-specifier)))))))) 70 | (string 71 | (let ((slot-name 72 | (intern (string-upcase (str:param-case first))))) 73 | (if (= (length slot-specifier) 1) 74 | (list slot-name :json-key first) 75 | (etypecase second 76 | (slot-option 77 | (list* slot-name :json-key first 78 | (cdr slot-specifier))) 79 | ((or non-keyword-symbol json-mop-type json-mop-composite-type) 80 | (list* slot-name :json-key first :json-type second 81 | (cddr slot-specifier))))))))))))) 82 | (list* 'defclass name direct-superclasses 83 | (mapcar #'expand-slot direct-slots) 84 | (list :metaclass 'json-mop:json-serializable-class) 85 | options))) 86 | 87 | (defgeneric concat-strings (list) 88 | (:documentation "Concatenates strings together and breaks when other element comes") 89 | (:method ((list list)) 90 | (let ((result-list 91 | nil)) 92 | (mapc (function (lambda (item) 93 | (push (typecase item 94 | (string 95 | (let ((first-list-item 96 | (first result-list))) 97 | (typecase first-list-item 98 | (string 99 | (pop result-list) 100 | (concat item first-list-item)) 101 | (otherwise 102 | item)))) 103 | (otherwise 104 | item)) 105 | result-list))) 106 | (reverse list)) 107 | result-list))) 108 | 109 | (defun remove-empty-values (alist) 110 | "Remove empty values from alist (used at run time)" 111 | (remove-if (function (lambda (list) 112 | (unless (cdr list) 113 | t))) 114 | alist)) 115 | 116 | (defun one-item (name) 117 | "Intern the item in the package active during execution of this function" 118 | `(cons ,name 119 | ,(intern (upcase (param-case name))))) 120 | 121 | (defun gen-alist (names) 122 | "Generate association list with the symbol lispified." 123 | (when names 124 | (mapcar (function one-item) names))) 125 | 126 | (defgeneric list-symbols (list) 127 | (:documentation "Filter non-symols out of list") 128 | (:method ((list list)) 129 | (remove-if (function (lambda (item) 130 | (cl:not (symbolp item)))) 131 | list))) 132 | 133 | (defun get-data-file (name &key (type "json")) 134 | "Get data file" 135 | (system-relative-pathname "openapi-generator" 136 | (concat "data/" name "." type))) 137 | 138 | (defun uri-p (uri) 139 | "Uri predicate." 140 | (let ((scheme 141 | (uri-scheme (uri uri)))) 142 | (when (or (string-equal "https" scheme) 143 | (string-equal "http" scheme)) 144 | t))) 145 | 146 | (defmethod hash-copy-recursive ((hash hash-table)) 147 | "Inspired by cl-hash-util:hash-copy, but performs a recursive (deep) hash-table copy 148 | which replaces all internal hash-tables with copies. 149 | This is needed to avoid looping when working with circular json-pointers." 150 | (let ((new-hash (make-hash-table :test (function equal) :size (hash-table-count hash)))) 151 | (loop for k being the hash-keys of hash 152 | for v being the hash-values of hash do 153 | (setf (gethash k new-hash) 154 | (if (typep v (quote hash-table)) 155 | (funcall (function hash-copy-recursive) v) 156 | v))) 157 | new-hash)) 158 | 159 | (defgeneric intern-param (s) 160 | (:documentation "Convert string or list of strings to param-cased symbol(s).") 161 | (:method ((s string)) 162 | (intern (upcase (param-case s)))) 163 | (:method ((s null)) 164 | nil) 165 | (:method ((s vector)) 166 | (mapcar (function intern-param) (coerce s (quote list)))) 167 | (:method ((s list)) 168 | (mapcar (function intern-param) s))) 169 | 170 | (deftype json-string () 171 | (quote (and string (satisfies valid-json-p)))) 172 | 173 | (defun json-true-p (s) 174 | "Predicate for valid json true" 175 | (string-equal "true" s)) 176 | 177 | (deftype json-true () 178 | (quote (and string (satisfies json-true-p)))) 179 | 180 | (defun json-false-p (s) 181 | "Predicate for valid json false" 182 | (string-equal "false" s)) 183 | 184 | (deftype json-false () 185 | (quote (and string (satisfies json-false-p)))) 186 | 187 | (defun json-null-p (s) 188 | "Predicate for valid json null" 189 | (string-equal "null" s)) 190 | 191 | (deftype json-null () 192 | (quote (and string (satisfies json-null-p)))) 193 | 194 | (defun json-number-p (s) 195 | "Predicate for valid json number (string)" 196 | (when (ignore-errors (parse-float s)) 197 | t)) 198 | 199 | (deftype json-number () 200 | (quote (and string (satisfies json-number-p)))) 201 | 202 | (defun integer-string-p (s) 203 | "Predicate for valid json number (string)" 204 | (integerp (ignore-errors (parse-integer s)))) 205 | 206 | (deftype integer-string () 207 | (quote (and string (satisfies integer-string-p)))) 208 | 209 | (defun json-array-p (s) 210 | "Predicate for valid json array (string)" 211 | (vectorp (ignore-errors (parse s)))) 212 | 213 | (deftype json-array () 214 | (quote (and string (satisfies json-array-p)))) 215 | 216 | (defun json-object-p (s) 217 | "Predicate for valid json array (string)" 218 | (hash-table-p (ignore-errors (parse s)))) 219 | 220 | (deftype json-object () 221 | (quote (and string (satisfies json-object-p)))) 222 | 223 | (defpackage dummy-printing-package ) 224 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | * Common Lisp OpenAPI Generator (openapi-generator) 2 | 3 | OpenAPI-generator is an api client system generator. 4 | 5 | ** Features 6 | - Generation of OpenAPI [[https://asdf.common-lisp.dev/][ASDF]]/[[https://www.quicklisp.org][Quicklisp]]-loadable projects within one command 7 | - Support for path, (arbitrary) query, (arbitrary) header, (json) content parameters 8 | - Each system has dynamic variables for changing default server, query, headers, 9 | authentication, cookie, parse 10 | - Multiple function alias options (operation-id, path, summary, description) 11 | - Supported file formats / inputs: [[https://www.json.org][JSON]], [[https://yaml.org/][YAML]], [[https://url.spec.whatwg.org/][URL]], local file, project-id on [[https://apis.guru/][apis.guru]]. 12 | - Conversion of an OpenAPI spec into CLOS object -> explore API within inspector 13 | - Conversion of OpenAPI-2.0 or YAML files to OpenAPI-3.0 JSON with [[https://converter.swagger.io/][swagger 14 | converter]] (network connection required) 15 | ** Codeberg 16 | The main repository of this library is hosted on [[https://codeberg.org/kilianmh/openapi-generator.git][Codeberg]]. 17 | If you are viewing this anywhere else, it is just a mirror. Please use the 18 | [[https://codeberg.org/kilianmh/openapi-generator][Codeberg repository]] for collaboration (issues, pull requests, discussions, 19 | ...). 20 | Thank you! 21 | 22 | ** Warning 23 | - This software is still ALPHA quality. The APIs will be likely to change. 24 | 25 | - Make sure to review the openapi specification file if it is from an untrusted 26 | source or environment file before using openapi-generator, especially before 27 | using the generated system. This is to mitigate potential security risks such 28 | as code injection. For security vulnerabilities, please contact the maintainer of the library. 29 | 30 | ** Quickstart 31 | Load openapi-generator from [[https://ultralisp.org/][Ultralisp]] via ql:quickload, or from local project 32 | folder via asdf:load-system 33 | 34 | #+begin_src lisp 35 | (ql:quickload :openapi-generator) 36 | #+end_src 37 | 38 | Then put this in the REPL: 39 | 40 | #+begin_src lisp 41 | (openapi-generator:make-openapi-client 42 | "codeberg" 43 | :url "https://codeberg.org/swagger.v1.json" 44 | :server "https://codeberg.org/api/v1" 45 | :parse t) 46 | #+end_src 47 | 48 | This creates a client system to access the 49 | [[https://codeberg.org/api/swagger][codeberg api]] in the projects folder 50 | within openapi-generator library and loads it (by default). Now you can use e.g.: 51 | 52 | #+begin_src lisp 53 | (codeberg:repo-get "kilianmh" "openapi-generator") 54 | #+end_src 55 | 56 | ** Interface 57 | *** parse-openapi 58 | #+begin_src lisp 59 | parse-openapi (name &key url collection-id source-directory content (dereference *dereference*)) 60 | #+end_src 61 | 62 | Parse an OpenAPI to a openapi class object. This might be useful for exploration 63 | of the API specification, or you can pass it to the make-openapi-client 64 | function. 65 | 66 | #+begin_src lisp 67 | (openapi-generator:parse-openapi "codeberg" :url "https://codeberg.org/swagger.v1.json") 68 | #+end_src 69 | 70 | **** name 71 | name of the openapi-file which is opened/created 72 | **** url 73 | url to a openapi specification file 74 | **** collection-id 75 | apis-guru-id from [[https://apis.guru/][apis.guru]] 76 | **** source-directory 77 | source-directory (name will be inserted from name parameter) 78 | **** content 79 | openapi spec file string 80 | **** dereference 81 | If set to true, pointers within openapi spec are dereferenced before parsing into 82 | openapi CLOS object. This improves discoverability for humans (and algorithms). 83 | Can be turned off for faster parsing. 84 | *** make-openapi-client 85 | #+begin_src lisp 86 | make-openapi-client (system-name 87 | &key 88 | (server *server*) (parse *parse*) (query *query*) (headers *headers*) 89 | (authorization *authorization*) (cookie *cookie*) 90 | (alias (list :operation-id)) (system-directory :temporary) (load-system t) 91 | openapi (api-name system-name) url source-directory collection-id 92 | content (dereference *dereference*)) 93 | #+end_src 94 | 95 | **** parameters 96 | ***** input same as parse-openapi 97 | ***** openapi 98 | openapi-object 99 | ***** api-name 100 | Name of file in openapi-generator/data folder 101 | ***** system-directory 102 | Options: 103 | - pathname-object -> pathname pathname-object + system-name 104 | Keyword-options 105 | - :temporary -> default, (uiop:temporary-directory) 106 | - :library -> openapi-generator/projects 107 | ***** parse 108 | default: nil, alternative: t, :json. 109 | 110 | #+begin_src lisp 111 | (openapi-generator:make-openapi-client 112 | "wikitionary-api" 113 | :url "https://github.com/shreyashag/openapi-specs/raw/main/thesaurus.yaml" 114 | :parse t) 115 | #+end_src 116 | #+begin_src lisp 117 | (wikitionary-api:find-word "oligopoly") 118 | #+end_src 119 | 120 | ***** server 121 | set default server variable in system (e.g. if server not/incorrect in spec 122 | file) 123 | ***** query 124 | set default query parameters 125 | ***** headers 126 | set default headers (e.g. for api-tokens that have to be supplied often) 127 | ***** authorization 128 | set default authorization value 129 | ***** cookie 130 | set default cookie value 131 | ***** Alias 132 | system exported functions: (multiple options possible): 133 | - :operation-id (param-cased operation-id) (default if there is are operation-id specified) 134 | - :summary (param-cased summary) 135 | - :description (param-case description) 136 | - :path (operation-type + path) (default if no operation-id specified) 137 | ***** load-system 138 | Load system after making it (default: t) 139 | ***** dereference 140 | If set to true (default), then the openapi spec is fully dereferenced before parsing into 141 | the CLOS object. This might be necessary to properly generate the system, e.g. 142 | if pointers are used for schemas. Can be turned off for faster system generation. 143 | **** examples 144 | ***** content 145 | The request body content can be supplied either as "raw" Json or as lisp data 146 | structures. Both are type-checked at run-time. Look at JZON documentation for the type mapping 147 | https://github.com/Zulu-Inuoe/jzon#type-mappings. 148 | You can supply the body-request to generated functions with the content keyword 149 | (default) or alternatively if existing in there is a title in the spec to the 150 | param-cased title keyword request-body id. 151 | Additionally for objects, the first level properties exist as function 152 | parameters. 153 | #+begin_src lisp 154 | (openapi-generator:make-openapi-client 155 | "stacks" 156 | :url "https://raw.githubusercontent.com/hirosystems/stacks-blockchain-api/gh-pages/openapi.resolved.yaml") 157 | #+end_src 158 | #+begin_src lisp 159 | ;; make-openapi-client stacks as described in quickstart 160 | (stacks:call-read-only-function 161 | "SP187Y7NRSG3T9Z9WTSWNEN3XRV1YSJWS81C7JKV7" "imaginary-friends-zebras" "get-token-uri" 162 | :content 163 | "{ 164 | \"sender\": \"STM9EQRAB3QAKF8NKTP15WJT7VHH4EWG3DJB4W29\", 165 | \"arguments\": 166 | [ 167 | \"0x0100000000000000000000000000000095\" 168 | ] 169 | }") 170 | #+end_src 171 | #+begin_src lisp 172 | ;; make-openapi-client stacks as described in quickstart 173 | (stacks:call-read-only-function 174 | "SP187Y7NRSG3T9Z9WTSWNEN3XRV1YSJWS81C7JKV7" "imaginary-friends-zebras" "get-token-uri" 175 | :read-only-function-args ;; request-body title (only if existing in spec) 176 | (serapeum:dict 177 | "sender" "STM9EQRAB3QAKF8NKTP15WJT7VHH4EWG3DJB4W29" 178 | "arguments" (list "0x0100000000000000000000000000000095")) 179 | :parse t) 180 | #+end_src 181 | #+begin_src lisp 182 | ;; first level of the request-body objects can be supplied as lisp datastructures 183 | (stacks:call-read-only-function 184 | "SP187Y7NRSG3T9Z9WTSWNEN3XRV1YSJWS81C7JKV7" "imaginary-friends-zebras" "get-token-uri" 185 | :sender "STM9EQRAB3QAKF8NKTP15WJT7VHH4EWG3DJB4W29" 186 | :arguments (list "0x0100000000000000000000000000000095")) 187 | #+end_src 188 | #+begin_src lisp 189 | ;; first level of the request-body objects can be supplied as JSON strings 190 | (stacks:call-read-only-function 191 | "SP187Y7NRSG3T9Z9WTSWNEN3XRV1YSJWS81C7JKV7" "imaginary-friends-zebras" "get-token-uri" 192 | :sender "STM9EQRAB3QAKF8NKTP15WJT7VHH4EWG3DJB4W29" 193 | :arguments "[\"0x0100000000000000000000000000000095\"]") 194 | #+end_src 195 | ***** header 196 | #+begin_src lisp 197 | ;; This example only works if you generate a valid apikey and insert it after Bearer 198 | ;; in the headers list 199 | (openapi-generator:make-openapi-client 200 | "openai" 201 | :url "https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml" 202 | :bearer "" 203 | :system-directory :temporary) 204 | #+end_src 205 | You have to first open an account and generate an api-key for using this api. 206 | If you supply value of authorization during client-creation, it will be saved 207 | directly in the file as variable. Beware and dont use this if in an untrusted 208 | environment. 209 | #+begin_src lisp 210 | ;; only working with valid API-KEY 211 | (openai:retrieve-engine "davinci") 212 | #+end_src 213 | You can also add add :authorization "Bearer " to each function 214 | call. This is equivalent to adding it to the headers. 215 | #+begin_src lisp 216 | (openai:list-engines 217 | :authorization "Bearer " ;; -> if not supplied during system generation 218 | ) 219 | #+end_src 220 | ***** collection-id 221 | #+begin_src lisp 222 | (openapi-generator:make-openapi-client 223 | "opendatasoft" 224 | :collection-id "opendatasoft.com" 225 | :parse nil) 226 | #+end_src 227 | This creates the api client for opendatasoft by accessing apis.guru forthe URL. 228 | Here an example query: 229 | #+begin_src lisp 230 | (opendatasoft:get-dataset "geonames-all-cities-with-a-population-1000") 231 | #+end_src 232 | ***** from openapi data folder 233 | Each time you load an api, a loadable json is stored in the openapi-generator/data 234 | folder. ELse you can put a file in the this folder manually. 235 | #+begin_src lisp 236 | ;; file with that name has to be present in the folder openapi-generator/data 237 | (openapi-generator:make-openapi-client "codeberg") 238 | #+end_src 239 | *** convert-to-openapi-3 240 | #+begin_src lisp 241 | convert-to-openapi-3 (&key url content pathname (content-type "json")) 242 | #+end_src 243 | Conversion from Openapi 2.0 YAML/JSON to OpenAPI 3.0 JSON. 244 | #+begin_src lisp 245 | (openapi-generator:convert-to-openapi-3 :url "https://converter.swagger.io/api/openapi.json") 246 | #+end_src 247 | *** **dereference** 248 | Global variable that determines whether openapi spec is dereferenced before 249 | parsing into a CLOS object. Set to true by default. Can be overwritten in each 250 | call to parse-openapi / make-openapi-client. 251 | ** Possible Future Improvements 252 | - modularize the project (e.g. separate systems for parsing, function 253 | generation, system generation) 254 | - extensibility with custom classes 255 | - Auto-generation of request body classes for parsing them into CLOS objects 256 | - Response validation & access functions for response content 257 | - websocket support 258 | - integrate JSON-Schema to create an expanded API-Object 259 | - generate client from command line interface (CLI) 260 | - integration in workflows (CI/CD, etc.) 261 | - more regression tests 262 | - support multiple implementations 263 | - offline openapi-spec conversion 264 | - integrate other api standards: json:api, raml, postman collection, har, OData, 265 | GraphQL, gRPC 266 | 267 | ** License on generated code 268 | Generated code is intentionally not subject to this project license. 269 | Code generated shall be considered AS IS and owned by the user. 270 | There are no warranties--expressed or implied--for generated code. 271 | You can do what you wish with it, and once generated, the code is your 272 | responsibility and subject to the licensing terms that you deem appropriate. 273 | 274 | ** Call for collaboration 275 | Feel free to contribute by opening issues, pull request, feature requests etc. 276 | Your help is much appreciated. 277 | 278 | ** Copyright 279 | 280 | (C) 2023 Kilian M. Haemmerle (kilian.haemmerle@protonmail.com) 281 | 282 | ** License 283 | 284 | Licensed under the AGPLv3+ License. 285 | -------------------------------------------------------------------------------- /code/parser.lisp: -------------------------------------------------------------------------------- 1 | (cl:in-package #:openapi-generator) 2 | 3 | (defgeneric get-openapi-version (openapi) 4 | (:documentation "Extract Swagger/Openapi version from api spec") 5 | (:method ((openapi openapi)) 6 | (openapi openapi)) 7 | (:method ((openapi hash-table)) 8 | (let* ((version-string 9 | (or (gethash "openapi" openapi) 10 | (gethash "swagger" openapi))) 11 | (version 12 | (read-version-from-string version-string))) 13 | (cond ((version= (read-version-from-string "2.0") 14 | version) 15 | (quote openapi-2.0)) 16 | ((and (version>= version 17 | (read-version-from-string "3.0")) 18 | (version<= version 19 | (read-version-from-string "3.0.3"))) 20 | (quote openapi-3.0)) 21 | ((version= (read-version-from-string "3.1") 22 | version) 23 | (quote openapi-3.1)) 24 | (t 25 | (error "Version ~A is not supported by this library" version-string))))) 26 | (:method ((openapi string)) 27 | (funcall (function get-openapi-version) (yason:parse openapi))) 28 | (:method ((openapi pathname)) 29 | (funcall (function get-openapi-version) (from-file openapi)))) 30 | 31 | (defgeneric parse-url (name url &key converter-url dereference) 32 | (:documentation "Parse url into package and save file in openapi-generator/data. 33 | Supported are: url / apis-guru / path / name (in openapi-generator/data folder)") 34 | (:method ((file-name string) (url string) &key (converter-url *converter-url*) dereference) 35 | (to-file (get-data-file file-name) 36 | (funcall (function parse-url) nil url 37 | :converter-url converter-url 38 | :dereference dereference))) 39 | (:method ((file-name null) (url string) &key (converter-url *converter-url*) dereference) 40 | (let* ((url-ending 41 | (substring (- (length url) 4) 42 | (length url) 43 | url)) 44 | (converted-content 45 | (case-using (function string-equal) url-ending 46 | (("yaml" ".yml") 47 | (convert-to-openapi-3 :url url :converter-url converter-url)) 48 | ("json" 49 | (let ((result 50 | (dex:get url))) 51 | (if (eql (get-openapi-version result) 52 | (quote openapi-2.0)) 53 | (convert-to-openapi-3 :url url :converter-url converter-url) 54 | result)))))) 55 | (if dereference 56 | (stringify (dereference (parse converted-content) 57 | :path-uri (quri:uri url))) 58 | converted-content)))) 59 | 60 | (defgeneric parse-string (file-name file &key converter-url) 61 | (:documentation "Safe string to file in local folder") 62 | (:method ((file-name string) (file string) &key (converter-url *converter-url*)) 63 | (let ((target-directory 64 | (get-data-file file-name))) 65 | (if (starts-with-p "{" file) 66 | (case (get-openapi-version file) 67 | (openapi-2.0 68 | (to-file target-directory 69 | (convert-to-openapi-3 :content file 70 | :content-type "json" 71 | :converter-url converter-url))) 72 | ((openapi-3.0 openapi-3.1) 73 | file) 74 | (otherwise 75 | (error "Unsupported Openapi Version"))) 76 | (to-file target-directory (convert-to-openapi-3 :content file 77 | :content-type "yaml")))))) 78 | 79 | (defgeneric parse-directory (source-directory target-directory &key converter-url dereference) 80 | (:documentation "Parse file from source directory to target-directory as usable JSON Openapi 3.X") 81 | (:method ((source-directory pathname) (target-directory pathname) &key (converter-url *converter-url*) dereference) 82 | (let* ((file-content 83 | (if dereference 84 | (stringify (dereference (pathname source-directory))) 85 | (from-file source-directory))) 86 | (json-content 87 | (ecase-using (function string=) (file-type source-directory) 88 | ("yaml" 89 | (to-file target-directory 90 | (convert-to-openapi-3 :content file-content 91 | :content-type "yaml" 92 | :converter-url converter-url))) 93 | ("json" 94 | (if (eql (get-openapi-version file-content) 95 | (quote openapi-2.0)) 96 | (to-file target-directory 97 | (convert-to-openapi-3 :content file-content 98 | :converter-url converter-url)) 99 | file-content))))) 100 | json-content))) 101 | 102 | (defgeneric parse-apis-guru-id (file-name apis-guru-id &key converter-url) 103 | (:documentation "parse api guru name with parse url") 104 | (:method ((file-name string) (apis-guru-id string) &key (converter-url *converter-url*)) 105 | (parse-url file-name (apis-guru-url apis-guru-id) :converter-url converter-url))) 106 | 107 | (defmethod ensure-json ((content string)) 108 | "Ensures document is a json" 109 | (let ((first-letter 110 | (subseq content 0 1))) 111 | (serapeum:case-using #'string= first-letter 112 | (("[" "{") content) 113 | (t (stringify (cl-yaml:parse content)))))) 114 | 115 | (defmethod dereference ((table hash-table) &key path-uri pathname) 116 | "Dereference all references recursively. 117 | Also grab external files. 118 | Exit recursion & warn when circular pointer detected" 119 | (let ((dereferenced-table (hash-copy-recursive table)) 120 | (circular-pointer nil)) 121 | (labels ((map-vector (vec used-pointer path) 122 | (let ((counter -1)) 123 | (mapc (function (lambda (element) 124 | (declare (ignore element)) 125 | (funcall (function %dereference) 126 | :used-pointer used-pointer 127 | :path (append path (list (incf counter)))))) 128 | (coerce vec 'list)))) 129 | (map-ht (ht used-pointer path) 130 | (maphash (function (lambda (key value) 131 | (declare (ignore value)) 132 | (funcall (function %dereference) 133 | :path (append path (list key)) 134 | :used-pointer used-pointer))) 135 | ht)) 136 | (%dereference (&key path used-pointer) 137 | (let ((entry 138 | (if path (hash-get dereferenced-table path) 139 | dereferenced-table))) 140 | (typecase entry 141 | (hash-table 142 | (let ((pointer (gethash "$ref" entry))) 143 | (if (and pointer (stringp pointer)) 144 | (if (member pointer used-pointer :test (function string=)) 145 | (unless (member pointer circular-pointer :test (function string=)) 146 | (push pointer circular-pointer) 147 | (warn "circular pointer detected in ~A" pointer)) 148 | (if (str:starts-with-p "." pointer) 149 | (let* ((split-pointer 150 | (str:split "#" pointer)) 151 | (document-location 152 | (first split-pointer)) 153 | (next-pointer 154 | (second split-pointer)) 155 | (rendered-uri 156 | (quri:render-uri (quri:merge-uris document-location (or path-uri 157 | (namestring pathname))))) 158 | (next-document nil)) 159 | (cond (path-uri 160 | (setf next-document 161 | (ensure-json (dex:get rendered-uri)))) 162 | (pathname 163 | (setf next-document 164 | (ensure-json (str:from-file (pathname rendered-uri)))))) 165 | (setf (hash-get dereferenced-table path) 166 | (if next-pointer 167 | (get-by-json-pointer 168 | next-document 169 | next-pointer) 170 | (parse next-document)))) 171 | (let ((pointer-value 172 | (get-by-json-pointer dereferenced-table pointer))) 173 | (etypecase pointer-value 174 | (null nil) 175 | (hash-table 176 | (map-ht (setf (hash-get dereferenced-table path) 177 | (hash-copy-recursive pointer-value)) 178 | (cons pointer used-pointer) path)) 179 | (vector 180 | (map-vector (setf (hash-get dereferenced-table path) 181 | (copy-seq pointer-value)) 182 | (cons pointer used-pointer) 183 | path)))))) 184 | (map-ht entry used-pointer path)))) 185 | (vector 186 | (map-vector entry used-pointer path)))))) 187 | (funcall (function %dereference))) 188 | dereferenced-table)) 189 | 190 | (defmethod dereference ((uri uri) &key) 191 | (dereference (parse (ensure-json (dex:get (render-uri uri)))) :path-uri uri)) 192 | 193 | (defmethod dereference ((path pathname) &key) 194 | (dereference (parse (ensure-json (str:from-file path))) :pathname path)) 195 | 196 | (defgeneric parse-openapi (name &key url source-directory collection-id content dereference converter-url) 197 | (:documentation "Parse json/yaml from a file or uri into openapi class instance 198 | You should mostly submit a file-name, and either ") 199 | (:method ((name string) &key url source-directory collection-id content (dereference *dereference*) (converter-url *converter-url*)) 200 | (let* ((file-pathname 201 | (make-pathname :directory (trim (directory-namestring constant-data-directory) 202 | :char-bag "/") 203 | :name name :type "json")) 204 | (result 205 | (cond (source-directory 206 | (parse-directory source-directory file-pathname 207 | :converter-url converter-url 208 | :dereference dereference)) 209 | (url 210 | (parse-url name url 211 | :converter-url converter-url 212 | :dereference dereference)) 213 | (collection-id 214 | (parse-apis-guru-id name collection-id)) 215 | (content 216 | (parse-string name content :converter-url converter-url)) 217 | (t 218 | (cond ((file-exists-p (get-data-file name)) 219 | (let* ((content 220 | (from-file (get-data-file name))) 221 | (openapi-version 222 | (get-openapi-version content))) 223 | (case openapi-version 224 | (openapi-2.0 (convert-to-openapi-3 :content content 225 | :converter-url converter-url)) 226 | ((openapi-3.0 openapi-3.1) content) 227 | (otherwise (error "The Version ~W is not supported" 228 | openapi-version))))) 229 | ((uiop:file-exists-p (get-data-file name :type "yaml")) 230 | (to-file (get-data-file name) 231 | (convert-to-openapi-3 :pathname (get-data-file name :type "yaml") 232 | :content-type "yaml" 233 | :converter-url converter-url))) 234 | ((uiop:file-exists-p (get-data-file name :type "yml")) 235 | (to-file (get-data-file name) 236 | (convert-to-openapi-3 :pathname (get-data-file name :type "yml") 237 | :content-type "yaml" 238 | :converter-url converter-url))) 239 | (t 240 | (error (concat "There is no " name " json/yaml in the openapi-generator/data folder 241 | Alternativeyl you can supply one of the keyword parameters (source-directory, apis-guru-id, file-content, url)")))))))) 242 | (json-to-clos result (get-openapi-version result))))) 243 | -------------------------------------------------------------------------------- /code/openapi-generator.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:openapi-generator) 2 | 3 | (defgeneric collect-path-types (path) 4 | (:documentation "Collect all bound operation types as symbols") 5 | (:method ((path path)) 6 | (let ((types 7 | nil)) 8 | (mapc (function (lambda (operation-type) 9 | (when (slot-boundp path operation-type) 10 | (push operation-type types)))) 11 | (quote (get head options trace delete put post patch))) 12 | types))) 13 | 14 | (defgeneric collect-function-names (api &key param-case) 15 | (:documentation "Generate all function names") 16 | (:method ((api openapi) &key param-case) 17 | (flet ((path-function-names (api path &key param-case) 18 | (mapcar (function (lambda (operator) 19 | (function-name path operator :param-case param-case))) 20 | (collect-path-types (gethash path (paths api)))))) 21 | (let ((names nil)) 22 | (maphash (function (lambda (path value) 23 | (declare (ignore value)) 24 | (push (path-function-names api path :param-case param-case) names))) 25 | (paths api)) 26 | (listopia:concat names))))) 27 | 28 | (defgeneric collect-alias-exports (api slot) 29 | (:documentation "Create list of upcased string to include in export clause of defpackage.") 30 | (:method ((api openapi) (slot string)) 31 | (funcall (function collect-alias-exports) api (intern (upcase slot) "OPENAPI-GENERATOR"))) 32 | (:method ((api openapi) (slot symbol)) 33 | (let ((result-list 34 | ())) 35 | (maphash (function (lambda (path path-object) 36 | (declare (ignore path)) 37 | (mapc (function (lambda (operator) 38 | (multiple-value-bind (value exist-p) 39 | (slot-value-safe (slot-value path-object operator) 40 | slot) 41 | (unless exist-p 42 | (return-from collect-alias-exports)) 43 | (push (upcase (param-case value)) 44 | result-list)))) 45 | (collect-path-types path-object)))) 46 | (paths api)) 47 | result-list))) 48 | 49 | (defgeneric generate-defpackage (name api &key alias) 50 | (:documentation "Generate defpackage code including alias functions") 51 | (:method (name (api openapi) &key alias) 52 | `(uiop:define-package ,(intern (upcase name)) 53 | (:use) 54 | (:import-from #:cl #:t #:nil) 55 | (:export #:*bearer* #:*authorization* #:*headers* #:*cookie* #:*parse* #:*server* 56 | ,@(append (when (member :path alias) 57 | (collect-function-names api :param-case nil)) 58 | (when (member :summary alias) 59 | (collect-alias-exports api "summary")) 60 | (when (member :description alias) 61 | (collect-alias-exports api "description")) 62 | (when (member :operation-id alias) 63 | (collect-alias-exports api "operation-id"))))))) 64 | 65 | (defgeneric generate-function-code (api &key check-type) 66 | (:documentation "Generate all function code as list") 67 | (:method ((api openapi) &key (check-type t)) 68 | (flet ((path-function-code (api path) 69 | "Create functions for each path operator" 70 | (mapcar (function (lambda (operator) 71 | (generate-function api path operator :check-type check-type))) 72 | (collect-path-types (gethash path (paths api)))))) 73 | (let ((result-list 74 | ())) 75 | (maphash (function (lambda (path value) 76 | (declare (ignore value)) 77 | (let ((concatenated-list 78 | (append (path-function-code api path) result-list))) 79 | (setf result-list concatenated-list)))) 80 | (paths api)) 81 | result-list)))) 82 | 83 | (defmacro %generate-client (&key api url content path (export-symbols t) (check-type t) (converter-url *converter-url*)) 84 | "Generates Common Lisp client by OpenAPI Spec." 85 | (let ((specification (or api 86 | (parse-openapi "generated" 87 | :source-directory path 88 | :url url 89 | :content content 90 | :converter-url converter-url)))) 91 | `(eval-when (:compile-toplevel :load-toplevel :execute) 92 | ,@(generate-function-code specification :check-type check-type) 93 | ,(when export-symbols 94 | `(export ',(mapcar (function intern) 95 | (collect-function-names specification))))))) 96 | 97 | (defun generate-client (&key api url content path (export-symbols t) (check-type t) (converter-url *converter-url*)) 98 | (eval `(%generate-client :api ,api 99 | :url ,url 100 | :content ,content 101 | :path ,path 102 | :export-symbols ,export-symbols 103 | :check-type ,check-type 104 | :converter-url ,converter-url))) 105 | 106 | (defgeneric generate-slot-alias (api slot) 107 | (:documentation "Create list of setf with slot as alias") 108 | (:method ((api openapi) (slot string)) 109 | (funcall (function generate-slot-alias) api (intern (upcase slot) "OPENAPI-GENERATOR"))) 110 | (:method ((api openapi) (slot symbol)) 111 | (let ((result-list 112 | ())) 113 | (maphash (function (lambda (path path-object) 114 | (mapc (function (lambda (operator) 115 | (multiple-value-bind (value exist-p) 116 | (slot-value-safe (slot-value path-object operator) 117 | slot) 118 | (unless exist-p 119 | (return-from generate-slot-alias)) 120 | (push (list (quote serapeum:defalias) 121 | (intern (upcase (param-case value))) 122 | (list (quote quote) 123 | (intern (function-name path operator :param-case nil)))) 124 | result-list)))) 125 | (collect-path-types path-object)))) 126 | (paths api)) 127 | (cons (quote progn) result-list)))) 128 | 129 | 130 | (defgeneric generate-parameters (&key query headers authorization bearer cookie parse server) 131 | (:documentation "Creates code to be included in main.lisp for parameters") 132 | (:method (&key query headers authorization bearer cookie parse server) 133 | (let ((*print-case* :downcase) 134 | (*package* (find-package 'dummy-printing-package))) 135 | (cl:format cl:nil "~{~S~%~}" 136 | (cl:list `(cl:defparameter ,(cl:intern "*PARSE*") ,(cl:when parse parse)) 137 | `(cl:defparameter ,(cl:intern "*AUTHORIZATION*") ,authorization) 138 | `(cl:defparameter ,(cl:intern "*BEARER*") ,bearer) 139 | `(cl:defparameter ,(cl:intern "*SERVER*") ,server) 140 | `(cl:defparameter ,(cl:intern "*COOKIE*") ,cookie) 141 | `(cl:defparameter ,(cl:intern "*HEADERS*") ',headers) 142 | `(cl:defparameter ,(cl:intern "*QUERY*") ',query)))))) 143 | 144 | (defgeneric check-api-slots (api list) 145 | (:documentation "Make sure that the function (alias) can be generated. 146 | Prefered alias source is operation-id. Last resort option is path.") 147 | (:method ((api openapi) (list list)) 148 | (flet ((check-path-slot (api slot) 149 | (maphash (function (lambda (path path-object) 150 | (declare (ignore path)) 151 | (mapc (function (lambda (operator) 152 | (multiple-value-bind (value exist-p) 153 | (slot-value-safe (slot-value path-object operator) 154 | (intern (upcase slot) "OPENAPI-GENERATOR")) 155 | (declare (ignore value)) 156 | (unless exist-p 157 | (return-from check-path-slot))))) 158 | (collect-path-types path-object)))) 159 | (paths api)) 160 | t)) 161 | (let ((result-list 162 | nil)) 163 | (when (and (member :operation-id list) 164 | (check-path-slot api "operation-id")) 165 | (push :operation-id result-list)) 166 | (when (and (member :summary list) 167 | (check-path-slot api "summary")) 168 | (push :summary result-list)) 169 | (when (and (member :description list) 170 | (check-path-slot api "description")) 171 | (push :description result-list)) 172 | (when (or (member :path list) 173 | (length= 0 result-list)) 174 | (push :path result-list)) 175 | result-list)))) 176 | 177 | 178 | (defgeneric generate-code (api name &key parse headers authorization bearer server cookie alias check-type) 179 | (:documentation "Generate all code to be included in the main.lisp file. Includes defpackage + functions + setf alias") 180 | (:method (api name &key parse headers authorization bearer server cookie alias (check-type t)) 181 | (let ((alias-list 182 | (check-api-slots api alias))) 183 | (let ((*print-case* :downcase) 184 | (*package* (find-package 'dummy-printing-package)) 185 | (*print-readably* t) 186 | (*print-escape* nil)) 187 | (concat 188 | (cl:format nil "~S" (generate-defpackage name api :alias alias-list)) 189 | (string #\Newline)(string #\Newline) 190 | (cl:format nil "(cl:in-package :~A)" (downcase (symbol-name name))) 191 | (string #\Newline)(string #\Newline) 192 | (string #\Newline)(string #\Newline) 193 | (generate-parameters :headers headers :authorization authorization :bearer bearer 194 | :cookie cookie :parse parse :server server) 195 | (string #\Newline)(string #\Newline) 196 | (cl:format nil "~{~W~% ~}" (generate-function-code api :check-type check-type) 197 | ) 198 | (when-let (operation-id-alias 199 | (and (member :operation-id alias-list) 200 | (generate-slot-alias api "operation-id"))) 201 | (cl:format nil "~S" operation-id-alias)) 202 | (when-let (summary-alias 203 | (and (member :summary alias-list) 204 | (generate-slot-alias api "summary"))) 205 | (cl:format nil "~S" summary-alias)) 206 | (when-let (description-alias 207 | (and (member :description alias-list) 208 | (generate-slot-alias api "description"))) 209 | (cl:format nil "~S" description-alias))))))) 210 | 211 | (defgeneric ensure-project-directory (directory) 212 | (:documentation "Makes sure that the directory is existing before the template is generated.") 213 | (:method (directory) 214 | (typecase directory 215 | (pathname 216 | (ensure-directories-exist directory)) 217 | (keyword 218 | (case directory 219 | (:temporary 220 | (ensure-directories-exist 221 | (make-pathname :directory 222 | (trim (concat (namestring (default-temporary-directory)) 223 | "openapi-generator/") 224 | :char-bag "/")))) 225 | (:library 226 | (ensure-directories-exist constant-projects-directory)))) 227 | (null 228 | (funcall (function ensure-project-directory) :temporary))))) 229 | 230 | (defun make-openapi-client (system-name 231 | &key 232 | server parse headers authorization bearer cookie 233 | (alias (list :operation-id)) (system-directory :library) (load-system t) 234 | openapi (api-name system-name) url source-directory collection-id content 235 | (dereference *dereference*) (verbose t) (check-type t) 236 | (converter-url *converter-url*)) 237 | "Creates Openapi client by combining a project template with generated code. 238 | Source options are url, source-directory, collection-id, or openapi (openapi class instance). 239 | The options server, parse, headers, authorization, bearer, cookie, content are stored in the library code 240 | as dynamic parameters.." 241 | (let* ((project-pathname 242 | (make-pathname :directory (concat (trim-left 243 | (directory-namestring (ensure-project-directory system-directory)) 244 | :char-bag "/") 245 | system-name))) 246 | (pathname-main 247 | (make-pathname :directory (concat (trim (directory-namestring project-pathname) 248 | :char-bag "/") 249 | "/src") 250 | :name "main" :type "lisp"))) 251 | (make-project 252 | project-pathname 253 | :name system-name 254 | :depends-on (quote (quri str com.inuoe.jzon dexador uiop openapi-generator serapeum)) 255 | :verbose nil 256 | :without-tests t) 257 | (with-open-file (system pathname-main 258 | :direction :output :if-exists :supersede :if-does-not-exist :create) 259 | (write-sequence 260 | (generate-code 261 | (if openapi 262 | openapi 263 | (parse-openapi api-name 264 | :url url 265 | :source-directory (cond ((pathnamep source-directory) 266 | source-directory) 267 | (source-directory 268 | (pathname source-directory))) 269 | :collection-id collection-id 270 | :dereference dereference 271 | :content content 272 | :converter-url converter-url)) 273 | (intern (upcase system-name)) 274 | :headers headers :authorization authorization :bearer bearer :cookie cookie 275 | :parse parse :alias alias :server server :check-type check-type) 276 | system)) 277 | (when verbose 278 | (print (str:concat "The system " system-name " has been generated in the path: " (namestring project-pathname)))) 279 | (when load-system 280 | (load-system (intern system-name))))) 281 | -------------------------------------------------------------------------------- /code/function-generation.lisp: -------------------------------------------------------------------------------- 1 | (cl:in-package #:openapi-generator) 2 | 3 | (defgeneric get-primary-uri (api) 4 | (:documentation "Return first server uri") 5 | (:method ((api openapi)) 6 | (let ((servers (slot-value-safe api 'servers))) 7 | (etypecase servers 8 | (null 9 | "") 10 | (string 11 | servers) 12 | (vector 13 | (if (= 0 (length servers)) 14 | "" 15 | (let ((first-server-object 16 | (car (coerce servers (quote list))))) 17 | (str:trim-right 18 | (typecase first-server-object 19 | (string first-server-object) 20 | (hash-table 21 | (let* ((first-url 22 | (gethash "url" first-server-object)) 23 | (curly-brace-location 24 | (cl:search "{" first-url))) 25 | (if curly-brace-location 26 | (let* ((end 27 | (cl:search "}" first-url)) 28 | (variable-name 29 | (substring (1+ curly-brace-location) 30 | end first-url))) 31 | (replace-first (concat "{" variable-name "}") 32 | (gethash "default" 33 | (gethash variable-name 34 | (gethash "variables" first-server-object))) 35 | first-url)) 36 | first-url)))) 37 | :char-bag "/")))))))) 38 | 39 | (defgeneric collect-parameters (path operation) 40 | (:documentation "Collect all parameters belong to an api a path and operation.") 41 | (:method ((path path) (operation symbol)) 42 | (let* ((path-parameters 43 | (slot-value-safe path (quote parameters))) 44 | (operation-parameters 45 | (slot-value-safe (slot-value path operation) (quote parameters))) 46 | (parameters-with-duplicates 47 | (remove nil (append (when path-parameters 48 | path-parameters) 49 | (when operation-parameters 50 | operation-parameters)))) 51 | (parameters-without-duplicates 52 | (remove nil 53 | (let ((names-list 54 | ())) 55 | (mapcar (function (lambda (parameter) 56 | (let ((name 57 | (slot-value parameter (quote name)))) 58 | (unless (member name names-list :test (function string-equal)) 59 | (push name names-list) 60 | parameter)))) 61 | parameters-with-duplicates))))) 62 | parameters-without-duplicates))) 63 | 64 | (defgeneric get-parameter-type (type parameters) 65 | (:documentation "Get list of parameters with specified type: Can be either query, path, head or cookie.") 66 | (:method ((type string) (parameters list)) 67 | (remove nil 68 | (mapcar (function (lambda (parameter) 69 | (when (string-equal (slot-value parameter (quote in)) 70 | type) 71 | parameter))) 72 | parameters)))) 73 | 74 | (defmethod get-required-parameter ((parameters list)) 75 | "Collect required parameter from list of parameters" 76 | (let ((required-parameter 77 | nil)) 78 | (mapc (function (lambda (parameter) 79 | (when (let ((required 80 | (slot-value-safe parameter (quote required)))) 81 | (or (eql (quote true) required) 82 | (eql (quote t) required))) 83 | (setf required-parameter (append required-parameter (list parameter)))))) 84 | parameters) 85 | required-parameter)) 86 | 87 | (defmethod get-optional-parameter ((parameters list)) 88 | "Collect optional parameter from list of parameters" 89 | (let ((optional-parameter 90 | nil)) 91 | (mapc (function (lambda (parameter) 92 | (unless (let ((required (slot-value-safe parameter (quote required)))) 93 | (or (eql (quote true) required) 94 | (eql (quote t) required))) 95 | (setf optional-parameter (append optional-parameter (list parameter)))))) 96 | parameters) 97 | optional-parameter)) 98 | 99 | 100 | (defgeneric get-uri-type (uri) 101 | (:documentation "") 102 | (:method ((uri string)) 103 | (let* ((uri-path (uri-path (uri uri))) 104 | (uri-path-length (length uri-path)) 105 | (file-type (substring (- uri-path-length 4) 106 | uri-path-length 107 | uri-path))) 108 | (case-using (function string-equal) file-type 109 | (("yml" "yaml") 110 | "yaml") 111 | ("json" 112 | "json") 113 | (otherwise 114 | (warn "~A is the wrong file type. Acceptable values are yaml, yml, or json." file-type)))))) 115 | 116 | (defgeneric function-name (path operation-type &key param-case) 117 | (:documentation "Generate unique symbole name for given operation-type and path") 118 | (:method ((path string) (operation-type symbol) &key param-case) 119 | (concat (symbol-name operation-type) 120 | (upcase (if param-case 121 | (concat "-" (param-case path)) 122 | path))))) 123 | 124 | (defgeneric object-name-symbols (objects) 125 | (:documentation "Outputs list with the name-symbols of the parameter objects in the input list.") 126 | (:method ((objects list)) 127 | (let ((object-names (mapcar (function name) objects))) 128 | (mapcar (function (lambda (object-name) 129 | (intern (upcase (param-case object-name))))) 130 | object-names)))) 131 | 132 | (defgeneric get-lambda-list (required-parameters optional-parameters operation-object 133 | json-body-schema) 134 | (:documentation "Create the lambda list to be included in the generated function.") 135 | (:method ((required-parameters list) (optional-parameters list) (operation-object operation) 136 | json-body-schema) 137 | (flet ((default-parameter-p (item) 138 | (when (typep item (quote string-designator)) 139 | (case-using (function string-equal) item 140 | (("PARSE" "SERVER" "AUTHORIZATION" "BEARER" "HEADERS" "COOKIE" "QUERY") 141 | t) 142 | (otherwise nil))))) 143 | (let ((title-symbol (slot-value-safe json-body-schema (quote title))) 144 | (content-types 145 | (handler-case (hash-keys (content (request-body operation-object))) 146 | (unbound-slot () 147 | nil)))) 148 | (remove-duplicates 149 | (remove-if 150 | (function default-parameter-p) 151 | (remove nil 152 | (append 153 | (object-name-symbols required-parameters) 154 | (list (quote &key) 155 | (list (intern "QUERY") (intern "*QUERY*")) 156 | (list (intern "HEADERS") (intern "*HEADERS*")) 157 | (list (intern "COOKIE") (intern "*COOKIE*")) 158 | (list (intern "AUTHORIZATION") (intern "*AUTHORIZATION*")) 159 | (list (intern "BEARER") (intern "*BEARER*")) 160 | (list (intern "SERVER") (intern "*SERVER*")) 161 | (list (intern "PARSE") (intern "*PARSE*"))) 162 | (when title-symbol (list (intern-param title-symbol))) 163 | (when json-body-schema 164 | (let ((properties (slot-value-safe json-body-schema (quote properties)))) 165 | (when properties 166 | (intern-param (hash-keys properties))))) 167 | (cond ((and content-types 168 | (null (length= 1 content-types))) 169 | (list (intern "CONTENT") (intern "CONTENT-TYPE"))) 170 | ((or content-types (member "CONTENT" 171 | (object-name-symbols optional-parameters) 172 | :test (function string-equal))) 173 | (list (intern "CONTENT"))) 174 | (t 175 | nil)) 176 | (object-name-symbols optional-parameters))))))))) 177 | 178 | (defgeneric get-description (operation-object) 179 | (:documentation "Extract documentation slots summary and description from operation object") 180 | (:method ((operation-object operation)) 181 | (let ((operation-id 182 | (slot-value-safe operation-object (quote operation-id))) 183 | (summary 184 | (slot-value-safe operation-object (quote summary))) 185 | (description 186 | (slot-value-safe operation-object (quote description)))) 187 | (trim (concat 188 | (when (param-case operation-id) 189 | (concat "Operation-id: " (param-case operation-id) (string #\Newline))) 190 | (when summary 191 | (concat "Summary: " summary (string #\Newline))) 192 | (when description 193 | (concat "Description: " description))))))) 194 | 195 | (defgeneric parameter-schema-type (parameter) 196 | (:documentation "Return the parameter type from schema") 197 | (:method ((parameter parameter)) 198 | (let* ((schema 199 | (slot-value-safe parameter (quote schema))) 200 | (schema-type 201 | (slot-value-safe schema (quote type))) 202 | (schema-one-of 203 | (slot-value-safe schema (quote one-of)))) 204 | (cond (schema-type 205 | (typecase schema-type 206 | (string (intern (upcase schema-type))) 207 | (vector (append (list (quote cl:or)) 208 | (mapcar (function (lambda (item) 209 | (intern (upcase item)))) 210 | (coerce schema-type 'list)))))) 211 | (schema-one-of 212 | (cons (quote cl:or) 213 | (mapcar (function (lambda (items) 214 | (intern (upcase (slot-value items (quote type)))))) 215 | (coerce schema-one-of (quote list))))))))) 216 | 217 | (defgeneric assure-required (required-parameters) 218 | (:documentation "Generate code for run-time type checking of required arguments") 219 | (:method ((required-parameter parameter)) 220 | (let ((*print-case* :downcase) 221 | (*package* (find-package 'dummy-printing-package)) 222 | (types 223 | (parameter-schema-type required-parameter))) 224 | (cl:read-from-string (cl:format nil "~{~W~% ~}" (cl:list `(assuref ,(cl:intern (upcase (param-case (name required-parameter)))) 225 | ,(if (consp types) 226 | (mapcar (lambda (type) 227 | (intern (symbol-name type) 228 | :common-lisp)) 229 | types) 230 | (intern (symbol-name types) 231 | :common-lisp)))))))) 232 | (:method ((required-parameters list)) 233 | (mapcar (function (lambda (parameter) 234 | (funcall (function assure-required) parameter))) 235 | required-parameters))) 236 | 237 | (defgeneric assure-optional (optional-parameter) 238 | (:documentation "Generate code for run-time type checking of optional arguments. 239 | This only happens, if arguments supplied.") 240 | (:method ((optional-parameter parameter)) 241 | (let ((types 242 | (parameter-schema-type optional-parameter)) 243 | (parameter-symbol 244 | (intern (upcase (param-case (name optional-parameter))))) 245 | (*print-case* :downcase) 246 | (*package* (find-package 'dummy-printing-package))) 247 | (cl:when (cl:and types 248 | (cl:not (cl:string-equal "content-type" parameter-symbol))) 249 | (cl:read-from-string 250 | (cl:format nil "~{~W~% ~}" 251 | (cl:list 252 | `(cl:when ,parameter-symbol 253 | (serapeum:assuref 254 | ,parameter-symbol 255 | ,(if (consp types) 256 | (mapcar (lambda (type) 257 | (intern (symbol-name type) 258 | :cl)) 259 | types) 260 | (intern (symbol-name types) 261 | :cl)))))))))) 262 | (:method ((optional-parameter list)) 263 | (mapcar (function (lambda (parameter) 264 | (funcall (function assure-optional) parameter))) 265 | optional-parameter))) 266 | 267 | (defgeneric path-list (path) 268 | (:documentation "Convert path string into a list of strings and symbols") 269 | (:method ((path string)) 270 | (remove-if (function emptyp) 271 | (mapcar 272 | (function (lambda (sequence) 273 | (if (starts-with-p "{" sequence) 274 | (intern (upcase 275 | (param-case 276 | (substring 1 277 | (1- (length sequence)) 278 | sequence)))) 279 | sequence))) 280 | (split " " 281 | (replace-using (list "/" " / " 282 | "." " . ") 283 | path)))))) 284 | 285 | (defgeneric path-list-stringified (path-list parameter-list) 286 | (:documentation "Get a list where symbols that will have a value of type strings are untouched, while 287 | symbols will have numbers values are converted into strings at run time.") 288 | (:method ((path-list list) (parameter-list list)) 289 | (flet ((get-parameter-by-name (name parameters) 290 | (remove nil 291 | (mapc (function (lambda (parameter) 292 | (when (string-equal (param-case (slot-value parameter (quote name))) 293 | name) 294 | (return-from get-parameter-by-name parameter)))) 295 | parameters)))) 296 | (remove-if (function (lambda (item) 297 | (when (typep item (quote string)) 298 | (emptyp item)))) 299 | (mapcar (function (lambda (item) 300 | (typecase item 301 | (symbol 302 | (let ((parameter-schema-type 303 | (parameter-schema-type 304 | (get-parameter-by-name (symbol-name item) 305 | parameter-list)))) 306 | (if (consp parameter-schema-type) 307 | (list 'format nil "~A" item) 308 | (case-using (function string-equal) 309 | parameter-schema-type 310 | ((number integer) 311 | (list 'write-to-string item)) 312 | (otherwise 313 | item))))) 314 | (string item)))) 315 | path-list))))) 316 | 317 | (defgeneric get-path (path parameters) 318 | (:documentation "generate path list") 319 | (:method ((path string) (parameters list)) 320 | (let ((path-list 321 | (concat-strings 322 | (path-list-stringified (path-list path) 323 | parameters)))) 324 | (case (length path-list) 325 | (1 (car path-list)) 326 | (otherwise `(concat ,@path-list)))))) 327 | 328 | (defgeneric get-query (parameters) 329 | (:documentation "Generate query (if there are parameters)") 330 | (:method ((parameters list)) 331 | (let ((generated-list 332 | (mapcar (lambda (parameter) 333 | (let ((interned (intern (upcase (param-case (name parameter)))))) 334 | `(cons ,(name parameter) 335 | ,(if (eq 'boolean (parameter-schema-type parameter)) 336 | `(if ,interned 337 | "true" 338 | "false") 339 | interned)))) 340 | (get-parameter-type "query" parameters)))) 341 | (case (length generated-list) 342 | (0 `(remove-empty-values 343 | (when ,(intern "QUERY") ,(intern "QUERY")))) 344 | (otherwise `(remove-empty-values 345 | (append (list ,@generated-list) 346 | (when ,(intern "QUERY") ,(intern "QUERY"))))))))) 347 | 348 | (defgeneric get-headers (parameters operation) 349 | (:documentation "Generate code for run-time header checking. Headers are only send, if they are supplied.") 350 | (:method ((parameters list) (operation operation)) 351 | (let ((content-type-list 352 | (let ((content-types 353 | (handler-case (hash-keys (content (request-body operation))) 354 | (unbound-slot () 355 | nil)))) 356 | (case (length content-types) 357 | (0 nil) 358 | (1 `(cons "Content-Type" ,(car content-types))) 359 | (otherwise `(cons "Content-Type" 360 | (cond (,(intern "CONTENT-TYPE") 361 | (if (find ,(intern "CONTENT-TYPE") 362 | (quote ,content-types) 363 | :test (function string-equal)) 364 | ,(intern "CONTENT-TYPE") 365 | (cl:warn "The body type ~A is not mentioned. Valid content-types are: ~A" 366 | ,(intern "CONTENT-TYPE") ,(unwords content-types)))) 367 | (t 368 | ,(car content-types)))))))) 369 | (generated-alist 370 | (gen-alist 371 | (remove "content-type" 372 | (mapcar (function name) (get-parameter-type "header" parameters)) 373 | :test (function string-equal)))) 374 | (standard-headers 375 | `((when ,(intern "AUTHORIZATION") 376 | (cl:cons "Authorization" ,(intern "AUTHORIZATION"))) 377 | (cl:cons "cookie" ,(intern "COOKIE"))))) 378 | (when content-type-list 379 | (push content-type-list standard-headers)) 380 | (when generated-alist 381 | (setf standard-headers (append generated-alist standard-headers))) 382 | `(remove-empty-values 383 | (append (list ,@standard-headers) 384 | (when ,(intern "HEADERS") ,(intern "HEADERS"))))))) 385 | 386 | (defmethod json-body-schema ((operation operation)) 387 | "Return application/json media type or nil." 388 | (let ((request-body (slot-value-safe operation (quote request-body)))) 389 | (when request-body 390 | (gethash "application/json" 391 | (content request-body))))) 392 | 393 | (defgeneric type-conversion (json-type) 394 | (:documentation "Convert json-type string or list of strings to lisp types.") 395 | (:method ((json-type string)) 396 | (case-using (function string-equal) json-type 397 | (null (list (quote or) (quote json-null) (quote null))) 398 | (number (list (quote or) (quote json-number) (quote number))) 399 | (boolean (list (quote or) (quote json-true) (quote json-false) (quote null) (quote t))) 400 | (string (quote string)) 401 | (array (list (quote or) (quote json-array) (quote list))) 402 | (object (list (quote or) (quote json-object) (quote hash-table))))) 403 | (:method ((json-type vector)) 404 | (let ((type-list 405 | (mapcar (function type-conversion) 406 | (coerce json-type (quote list))))) 407 | (if type-list 408 | (cons (quote or) 409 | type-list) 410 | t))) 411 | (:method ((json-type null)) 412 | t)) 413 | 414 | (defmethod json-content ((schema schema) &key (check-type t)) 415 | "Generate the code to validate request-body or generate it according to the spec." 416 | (let ((intern-content 417 | (intern "CONTENT")) 418 | (title 419 | (intern-param (slot-value-safe schema (quote title)))) 420 | (properties 421 | (slot-value-safe schema (quote properties))) 422 | (required-properties 423 | (slot-value-safe schema (quote required))) 424 | (type-list 425 | (type-conversion (slot-value-safe schema (quote type))))) 426 | (declare (symbol title) (type (or hash-table null) properties) 427 | (type (or cons null) required-properties)) 428 | (labels ((required (name schema) 429 | (declare (schema schema) (string name)) 430 | `((com.inuoe.jzon:write-key* ,name) 431 | (com.inuoe.jzon:write-value* 432 | ,(if check-type 433 | `(progn (assuref ,(intern-param name) 434 | ,(type-conversion (slot-value-safe schema (quote type)))) 435 | (or (ignore-errors (parse ,(intern-param name))) 436 | ,(intern-param name))) 437 | (intern-param name)) 438 | ))) 439 | (optional (name schema) 440 | (declare (schema schema) (string name)) 441 | (let ((name-symbol (intern-param name))) 442 | `((when ,name-symbol 443 | (com.inuoe.jzon:write-key* ,name) 444 | (com.inuoe.jzon:write-value* 445 | ,(if check-type 446 | `(assuref ,name-symbol 447 | ,(type-conversion (slot-value-safe schema (quote type)))) 448 | name-symbol)))))) 449 | (optional-or-required (property) 450 | (declare (string property)) 451 | (if (member property required-properties :test (function string-equal)) 452 | (funcall (function required) property (gethash property properties)) 453 | (funcall (function optional) property (gethash property properties))))) 454 | (remove nil `(cond (,intern-content 455 | ,(if check-type 456 | (if (and (atom type-list) 457 | (string-equal type-list (quote string))) 458 | `(assuref ,intern-content string) 459 | `(if (not (stringp (assuref ,intern-content ,type-list))) 460 | (stringify ,intern-content) 461 | ,intern-content)) 462 | `(stringify ,intern-content))) 463 | ,(when title 464 | `(,title 465 | ,(if check-type 466 | (if (and (atom type-list) 467 | (string-equal type-list (quote string))) 468 | `(assuref ,title string) 469 | `(if (not (stringp (assuref ,title ,type-list))) 470 | (stringify ,title) 471 | ,title)) 472 | `(stringify ,title)))) 473 | ,(when properties 474 | (let ((property-names (delete "content" 475 | (hash-keys properties) 476 | :test (function string=)))) 477 | (declare (type (or cons null) property-names)) 478 | (when (> (list-length property-names) 0) 479 | `((or ,@(intern-param 480 | property-names)) 481 | (let ((,(intern "S") (make-string-output-stream))) 482 | (declare (stream ,(intern "S"))) 483 | (com.inuoe.jzon:with-writer* (:stream ,(intern "S") :pretty t) 484 | (com.inuoe.jzon:with-object* 485 | ,@(mapcan (function optional-or-required) 486 | property-names))) 487 | (let ((,(intern "OUTPUT") 488 | (get-output-stream-string ,(intern "S")))) 489 | (declare (string ,(intern "OUTPUT"))) 490 | (unless (string= "{}" ,(intern "OUTPUT")) 491 | ,(intern "OUTPUT"))))))))))))) 492 | 493 | (defmethod get-response-type ((operation operation)) 494 | "Get response type. Return value can be either :json or nil" 495 | (maphash (lambda (key value) 496 | (declare (ignore key)) 497 | (let ((content (slot-value-safe value (quote content)))) 498 | (declare (type (or null hash-table) content)) 499 | (when content 500 | (let ((hash-keys 501 | (hash-keys content))) 502 | (declare (list hash-keys)) 503 | (mapc (lambda (item) 504 | (when (str:containsp "application/json" item) 505 | (return-from get-response-type :json))) 506 | hash-keys))))) 507 | (slot-value operation (quote responses)))) 508 | 509 | (defgeneric generate-function (api path operation-type &key check-type) 510 | (:documentation "Generate functions for all types of http request") 511 | (:method ((api openapi) (path string) (operation-type symbol) &key (check-type t)) 512 | (let* ((path-object (gethash path (paths api))) 513 | (operation-object (slot-value path-object operation-type)) 514 | (all-parameters (collect-parameters path-object operation-type)) 515 | (required-params (get-required-parameter all-parameters)) 516 | (optional-params (get-optional-parameter all-parameters)) 517 | (json-body (json-body-schema operation-object)) 518 | (json-body-schema (slot-value-safe json-body (quote schema))) 519 | (lambda-list (get-lambda-list required-params optional-params operation-object 520 | json-body-schema)) 521 | (response-type (get-response-type operation-object)) 522 | (description (get-description operation-object)) 523 | (primary-uri (get-primary-uri api)) 524 | (uri-path (get-path path all-parameters)) 525 | (uri-query (get-query all-parameters)) 526 | (intern-response (intern "RESPONSE")) 527 | (intern-content (intern "CONTENT")) 528 | (intern-server-uri (intern "SERVER-URI"))) 529 | `(defun ,(intern (function-name path operation-type :param-case nil)) ,lambda-list 530 | ,description 531 | ,@(when check-type 532 | (append (assure-required required-params) 533 | (assure-optional optional-params))) 534 | (let* ((,intern-server-uri 535 | (uri (or ,(intern "SERVER") 536 | ,primary-uri))) 537 | (,intern-response 538 | (request 539 | (render-uri 540 | (make-uri :scheme (uri-scheme ,intern-server-uri) 541 | :host (uri-host ,intern-server-uri) 542 | :port (uri-port ,intern-server-uri) 543 | :path (concat (uri-path ,intern-server-uri) ,uri-path) 544 | :query ,uri-query)) 545 | ,@(if (member intern-content lambda-list) 546 | `(:content ,(if json-body 547 | (json-content json-body-schema :check-type check-type) 548 | intern-content)) 549 | (values)) 550 | :method (quote ,(intern (symbol-name operation-type))) 551 | :bearer-auth ,(intern "BEARER") 552 | :headers ,(get-headers all-parameters operation-object)))) 553 | ,(if response-type 554 | (case response-type 555 | (:json `(if ,(intern "PARSE") 556 | (parse ,intern-response) 557 | ,intern-response)) 558 | (otherwise `(case ,(intern "PARSE") 559 | (:json (parse ,intern-response)) 560 | (otherwise ,intern-response)))) 561 | `(case ,(intern "PARSE") 562 | (:json (parse ,intern-response)) 563 | (otherwise ,intern-response)))))))) 564 | 565 | "delete/admin/users/{username}/keys/{id}" 566 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | 6 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software. 11 | 12 | The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. 13 | 14 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. 15 | 16 | Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software. 17 | 18 | A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU Affero General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public. 19 | 20 | The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version. 21 | 22 | An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license. 23 | 24 | The precise terms and conditions for copying, distribution and modification follow. 25 | 26 | TERMS AND CONDITIONS 27 | 28 | 0. Definitions. 29 | 30 | "This License" refers to version 3 of the GNU Affero General Public License. 31 | 32 | "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. 33 | 34 | "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. 35 | 36 | To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. 37 | 38 | A "covered work" means either the unmodified Program or a work based on the Program. 39 | 40 | To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. 41 | 42 | To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. 43 | 44 | An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 45 | 1. Source Code. 46 | The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. 47 | 48 | A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. 49 | 50 | The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. 51 | 52 | The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those 53 | subprograms and other parts of the work. 54 | 55 | The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. 56 | 57 | The Corresponding Source for a work in source code form is that same work. 58 | 2. Basic Permissions. 59 | All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. 60 | 61 | You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. 62 | 63 | Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 64 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 65 | No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. 66 | 67 | When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 68 | 4. Conveying Verbatim Copies. 69 | You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. 70 | 71 | You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 72 | 5. Conveying Modified Source Versions. 73 | You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: 74 | a) The work must carry prominent notices stating that you modified it, and giving a relevant date. 75 | b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". 76 | c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. 77 | d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. 78 | 79 | A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 80 | 6. Conveying Non-Source Forms. 81 | You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: 82 | a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. 83 | b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. 84 | c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. 85 | d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. 86 | e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. 87 | 88 | A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. 89 | 90 | A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. 91 | 92 | "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. 93 | 94 | If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). 95 | 96 | The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. 97 | 98 | Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 99 | 7. Additional Terms. 100 | "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. 101 | 102 | When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. 103 | 104 | Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: 105 | a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or 106 | b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or 107 | c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or 108 | d) Limiting the use for publicity purposes of names of licensors or authors of the material; or 109 | e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or 110 | f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. 111 | 112 | All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. 113 | 114 | If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. 115 | 116 | Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 117 | 8. Termination. 118 | 119 | You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). 120 | 121 | However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. 122 | 123 | Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. 124 | 125 | Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 126 | 9. Acceptance Not Required for Having Copies. 127 | 128 | You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 129 | 10. Automatic Licensing of Downstream Recipients. 130 | 131 | Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. 132 | 133 | An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. 134 | 135 | You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 136 | 11. Patents. 137 | 138 | A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". 139 | 140 | A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. 141 | 142 | Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. 143 | 144 | In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to s ue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. 145 | 146 | If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent 147 | license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. 148 | 149 | If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. 150 | 151 | A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. 152 | 153 | Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 154 | 12. No Surrender of Others' Freedom. 155 | 156 | If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may 157 | not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 158 | 13. Remote Network Interaction; Use with the GNU Affero General Public License. 159 | 160 | Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU Affero General Public License that is incorporated pursuant to the following paragraph. 161 | 162 | Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU Affero General Public License. 163 | 14. Revised Versions of this License. 164 | 165 | The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 166 | 167 | Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation. 168 | 169 | If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. 170 | 171 | Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 172 | 15. Disclaimer of Warranty. 173 | 174 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 175 | 16. Limitation of Liability. 176 | 177 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 178 | 17. Interpretation of Sections 15 and 16. 179 | 180 | If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. 181 | 182 | END OF TERMS AND CONDITIONS 183 | 184 | How to Apply These Terms to Your New Programs 185 | 186 | If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. 187 | 188 | To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. 189 | 190 | 191 | Copyright (C) 192 | 193 | This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 194 | 195 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 196 | 197 | You should have received a copy of the GNU Affero General Public License along with this program. If not, see . 198 | 199 | Also add information on how to contact you by electronic and paper mail. 200 | 201 | If your software can interact with users remotely through a computer network, you should also make sure that it provides a way for users to get its source. For example, if your program is a web application, its interface could display a "Source" link that leads users to an archive of the code. There are many ways you could offer source, and different solutions will be better for different programs; see section 13 for the specific requirements. 202 | 203 | You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see . 204 | --------------------------------------------------------------------------------