/// Build the basic Swagger 2.0 object
Build the basic Swagger 2.0 object
Build the basic Swagger 2 . 0 object
pub fn build_spec(app: &framework::Application, spec: Spec) -> JsonValue {
    jsonway::object(|json| {
        // Required. Specifies the Swagger Specification version being used.
        // It can be used by the Swagger UI and other clients to interpret the API listing.
        // The value MUST be "2.0".
        json.set("swagger", "2.0".to_string());
        // Required. Provides metadata about the API. The metadata can be used by the clients if needed.
        json.object("info", |info| {
            // Required. The title of the application.
            info.set("title", spec.info.title.clone());
            if spec.info.description.is_some() {
                // A short description of the application. GFM syntax can be used for rich text representation.
                info.set("description", spec.info.description.as_ref().unwrap().clone());
            }
            if spec.info.terms_of_service.is_some() {
                // The Terms of Service for the API.
                info.set("termsOfService", spec.info.terms_of_service.as_ref().unwrap().clone());
            }
            if spec.info.contact.is_some() {
                let contact_spec = spec.info.contact.as_ref().unwrap();
                // The contact information for the exposed API.
                info.object("contact", |contact| {
                    // The identifying name of the contact person/organization.
                    contact.set("name", contact_spec.name.clone());
                    if contact_spec.url.is_some() {
                        // The URL pointing to the contact information. MUST be in the format of a URL.
                        contact.set("url", contact_spec.url.as_ref().unwrap().clone());
                    }
                    if contact_spec.email.is_some() {
                        // The email address of the contact person/organization. MUST be in the format of an email address.
                        contact.set("email", contact_spec.email.as_ref().unwrap().clone());
                    }
                });
            }
            if spec.info.license.is_some() {
                let license_spec = spec.info.license.as_ref().unwrap();
                info.object("license", |license| {
                    // The license name used for the API.
                    license.set("name", license_spec.name.clone());
                    // An URL to the license used for the API. MUST be in the format of a URL.
                    license.set("url", license_spec.url.clone());
                });
            }
            // Required. Provides the version of the application API (not to be confused by the specification version).
            info.set("version", spec.info.version.clone()
                .or(app.root_api.version.clone().map(|v| v.version))
                .unwrap_or_else(|| "0.0.0".to_string()));
        });
        if spec.host.is_some() {
            // The host (name or ip) serving the API. This MUST be the host only and does not
            // include the scheme nor sub-paths. It MAY include a port. If the host is not included,
            // the host serving the documentation is to be used (including the port).
            // The host does not support path templating.
            json.set("host", spec.host.as_ref().unwrap().clone());
        }
        // The base path on which the API is served, which is relative to the host.
        // If it is not included, the API is served directly under the host.
        // The value MUST start with a leading slash (/). The basePath does not support path
        // templating.
        json.set("basePath", spec.base_path.clone().unwrap_or_else(|| {
            // Here we are generating the `basePath` from prefix and version
            // (if versioning strategy is Path)
            let mut base_path = "/".to_string();
            if app.root_api.prefix.is_some() {
                base_path.push_str(&app.root_api.prefix.as_ref().unwrap());
            }
            if app.root_api.version.is_some() {
                match app.root_api.version.as_ref().unwrap() {
                    &framework::Version{ref version, versioning: framework::Versioning::Path}  => {
                        if base_path.len() > 1 {
                            base_path.push_str("/")
                        }
                        base_path.push_str(&version);
                    },
                    _ => ()
                }
            }
            base_path
        }));
        // The transfer protocol of the API. Values MUST be from the list:
        // "http", "https", "ws", "wss". If the schemes is not included, the default
        // scheme to be used is the one used to access the specification.
        if spec.schemes.is_some() {
            let schemes_spec = spec.schemes.as_ref().unwrap();
            json.array("schemes", |schemes| {
                for scheme in schemes_spec.iter() {
                    schemes.push(scheme.to_string())
                }
            })
        }
        if spec.consumes.is_some() {
            let consumes_spec = spec.consumes.as_ref().unwrap();
            // A list of MIME types the APIs can consume. This is global to all APIs but can be
            // overridden on specific API calls. Value MUST be as described under Mime Types.
            json.array("consumes", |consumes| {
                for mime in consumes_spec.iter() {
                    consumes.push(mime.to_string())
                }
            });
        }
        if spec.produces.is_some() {
            let produces_spec = spec.produces.as_ref().unwrap();
            // A list of MIME types the APIs can produce. This is global to all APIs but can
            // be overridden on specific API calls. Value MUST be as described under Mime Types.
            json.array("produces", |produces| {
                for mime in produces_spec.iter() {
                    produces.push(mime.to_string())
                }
            });
        }
        // Required. The available paths and operations for the API.
        json.object("paths", |paths| {
            fill_paths(WalkContext {
                path: "",
                params: vec![]
            }, paths, &app.root_api.handlers);
        });
        // TODO Implement the rest of the spec
        // // An object to hold data types produced and consumed by operations.
        // json.object("definitions", |definitions| {
        // });
        // // An object to hold parameters that can be used across operations.
        // // This property does not define global parameters for all operations.
        // json.object("parameters", |parameters| {
        // });
        // // An object to hold responses that can be used across operations.
        // // This property does not define global responses for all operations.
        // json.object("responses", |responses| {
        // });
        // // Security scheme definitions that can be used across the specification.
        // json.object("securityDefinitions", |security_definitions| {
        // });
        // // A declaration of which security schemes are applied for the API as a whole.
        // // The list of values describes alternative security schemes that can be used
        // // (that is, there is a logical OR between the security requirements).
        // // Individual operations can override this definition.
        // json.array("security", |security| {
        // });
        // // A list of tags used by the specification with additional metadata.
        // // The order of the tags can be used to reflect on their order by the parsing tools.
        // // Not all tags that are used by the Operation Object must be declared.
        // // The tags that are not declared may be organized randomly or based on the tools' logic.
        // // Each tag name in the list MUST be unique.
        // json.array("tags", |tags| {
        // });
        // // Additional external documentation.
        // json.object("externalDocs", |external_docs| {
        // })
    }).unwrap()
}
pub fn build_spec ( app : & framework :: Application , spec : Spec ) -> JsonValue { jsonway :: object ( | json | { // Required. Specifies the Swagger Specification version being used. // It can be used by the Swagger UI and other clients to interpret the API listing. // The value MUST be "2.0". json . set ( " " , " " . to_string ( ) ) ; // Required. Provides metadata about the API. The metadata can be used by the clients if needed. json . object ( " " , | info | { // Required. The title of the application. info . set ( " " , spec . info . title . clone ( ) ) ; if spec . info . description . is_some ( ) { // A short description of the application. GFM syntax can be used for rich text representation. info . set ( " " , spec . info . description . as_ref ( ) . unwrap ( ) . clone ( ) ) ; } if spec . info . terms_of_service . is_some ( ) { // The Terms of Service for the API. info . set ( " " , spec . info . terms_of_service . as_ref ( ) . unwrap ( ) . clone ( ) ) ; } if spec . info . contact . is_some ( ) { let contact_spec = spec . info . contact . as_ref ( ) . unwrap ( ) ; // The contact information for the exposed API. info . object ( " " , | contact | { // The identifying name of the contact person/organization. contact . set ( " " , contact_spec . name . clone ( ) ) ; if contact_spec . url . is_some ( ) { // The URL pointing to the contact information. MUST be in the format of a URL. contact . set ( " " , contact_spec . url . as_ref ( ) . unwrap ( ) . clone ( ) ) ; } if contact_spec . email . is_some ( ) { // The email address of the contact person/organization. MUST be in the format of an email address. contact . set ( " " , contact_spec . email . as_ref ( ) . unwrap ( ) . clone ( ) ) ; } } ) ; } if spec . info . license . is_some ( ) { let license_spec = spec . info . license . as_ref ( ) . unwrap ( ) ; info . object ( " " , | license | { // The license name used for the API. license . set ( " " , license_spec . name . clone ( ) ) ; // An URL to the license used for the API. MUST be in the format of a URL. license . set ( " " , license_spec . url . clone ( ) ) ; } ) ; } // Required. Provides the version of the application API (not to be confused by the specification version). info . set ( " " , spec . info . version . clone ( ) . or ( app . root_api . version . clone ( ) . map ( | v | v . version ) ) . unwrap_or_else ( | | " " . to_string ( ) ) ) ; } ) ; if spec . host . is_some ( ) { // The host (name or ip) serving the API. This MUST be the host only and does not // include the scheme nor sub-paths. It MAY include a port. If the host is not included, // the host serving the documentation is to be used (including the port). // The host does not support path templating. json . set ( " " , spec . host . as_ref ( ) . unwrap ( ) . clone ( ) ) ; } // The base path on which the API is served, which is relative to the host. // If it is not included, the API is served directly under the host. // The value MUST start with a leading slash (/). The basePath does not support path // templating. json . set ( " " , spec . base_path . clone ( ) . unwrap_or_else ( | | { // Here we are generating the `basePath` from prefix and version // (if versioning strategy is Path) let mut base_path = " " . to_string ( ) ; if app . root_api . prefix . is_some ( ) { base_path . push_str ( & app . root_api . prefix . as_ref ( ) . unwrap ( ) ) ; } if app . root_api . version . is_some ( ) { match app . root_api . version . as_ref ( ) . unwrap ( ) { & framework :: Version { ref version , versioning : framework :: Versioning :: Path } => { if base_path . len ( ) > 1 { base_path . push_str ( " " ) } base_path . push_str ( & version ) ; } , _ => ( ) } } base_path } ) ) ; // The transfer protocol of the API. Values MUST be from the list: // "http", "https", "ws", "wss". If the schemes is not included, the default // scheme to be used is the one used to access the specification. if spec . schemes . is_some ( ) { let schemes_spec = spec . schemes . as_ref ( ) . unwrap ( ) ; json . array ( " " , | schemes | { for scheme in schemes_spec . iter ( ) { schemes . push ( scheme . to_string ( ) ) } } ) } if spec . consumes . is_some ( ) { let consumes_spec = spec . consumes . as_ref ( ) . unwrap ( ) ; // A list of MIME types the APIs can consume. This is global to all APIs but can be // overridden on specific API calls. Value MUST be as described under Mime Types. json . array ( " " , | consumes | { for mime in consumes_spec . iter ( ) { consumes . push ( mime . to_string ( ) ) } } ) ; } if spec . produces . is_some ( ) { let produces_spec = spec . produces . as_ref ( ) . unwrap ( ) ; // A list of MIME types the APIs can produce. This is global to all APIs but can // be overridden on specific API calls. Value MUST be as described under Mime Types. json . array ( " " , | produces | { for mime in produces_spec . iter ( ) { produces . push ( mime . to_string ( ) ) } } ) ; } // Required. The available paths and operations for the API. json . object ( " " , | paths | { fill_paths ( WalkContext { path : " " , params : vec ! [ ] } , paths , & app . root_api . handlers ) ; } ) ; // TODO Implement the rest of the spec // // An object to hold data types produced and consumed by operations. // json.object("definitions", |definitions| { // }); // // An object to hold parameters that can be used across operations. // // This property does not define global parameters for all operations. // json.object("parameters", |parameters| { // }); // // An object to hold responses that can be used across operations. // // This property does not define global responses for all operations. // json.object("responses", |responses| { // }); // // Security scheme definitions that can be used across the specification. // json.object("securityDefinitions", |security_definitions| { // }); // // A declaration of which security schemes are applied for the API as a whole. // // The list of values describes alternative security schemes that can be used // // (that is, there is a logical OR between the security requirements). // // Individual operations can override this definition. // json.array("security", |security| { // }); // // A list of tags used by the specification with additional metadata. // // The order of the tags can be used to reflect on their order by the parsing tools. // // Not all tags that are used by the Operation Object must be declared. // // The tags that are not declared may be organized randomly or based on the tools' logic. // // Each tag name in the list MUST be unique. // json.array("tags", |tags| { // }); // // Additional external documentation. // json.object("externalDocs", |external_docs| { // }) } ) . unwrap ( ) }
Build the basic Swagger 2.0 object
Build the basic Swagger 2 . 0 object
app
&framework::Application
spec
Spec