Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger spec to include operationId ? #154

Closed
MarkEdmondson1234 opened this issue Aug 3, 2017 · 6 comments
Closed

Swagger spec to include operationId ? #154

MarkEdmondson1234 opened this issue Aug 3, 2017 · 6 comments
Labels
difficulty: advanced Best for maintainers to address effort: medium < 3 days of work priority: medium Could be fixed before next release theme: OpenAPI pertains to OpenAPI type: enhancement Adds a new, backwards-compatible feature

Comments

@MarkEdmondson1234
Copy link

Hi, I used the swagger.json from the Plumber app to generate a Google Cloud Endpoint which is neat, but I had to add a operationId to the swagger spec to get it to work. Here is my horrible hack:

library(yaml)
library(jsonlite)

make_openapi <- function(projectId){

  ## this calls the Plumber apps swagger.json
  json <- jsonlite::fromJSON(sprintf("https://%s.appspot.com/swagger.json", projectId))

  ## change host from 127.0.0.1 to the appengine project
  json$host <- sprintf("%s.appspot.com", projectId)
  
  ## add operationId to each endpoint
  ohgod <- lapply(names(json$paths), 
                  function(x) {lapply(json$paths[[x]], 
                                      function(verb) {verb$operationId <- basename(x);verb})})
  json$paths <- setNames(ohgod, names(json$paths))

  # silly formatting to make these entries arrays and not characters
  yaml <- gsub("application/json", "[application/json]", yaml::as.yaml(json))
  yaml <- gsub("schemes: http", "schemes: [http]", yaml)
  
  writeLines(yaml, con = "openapi.yaml")
}

make_openapi("your-project-id")
@trestletech
Copy link
Contributor

Thanks for the suggestion. Do you have any suggestions for how this field should be populated in the Swagger doc? I could just concatenate the verb with the path like @get /my/path becomes operationId: get-my-path? It gets a little awkward with endpoints that support multiple verbs, but I think the idea would still work.

Also would it be useful to include a plumber.yaml for your usecase?

@MarkEdmondson1234
Copy link
Author

MarkEdmondson1234 commented Aug 4, 2017

operationId: get-my-path would be perfect, I forgot to add the verb in my example. It just needs to be unique.

My use case supports JSON or YAML files, but to modify it (such as the hostname) I turned it into YAML so a user can edit the final result easier. Its not a big deal to convert between them, although annoying things like the array vs character would be easier to deal with if you have yaml available in the correct format. So yes, it would be useful :)

@MarkEdmondson1234
Copy link
Author

Actually what would be most useful would be if the swagger.json or yaml file could be generated just from the code, without needing to launch the plumber server.

@alexhallam
Copy link

Hi, I wanted to see if there was an update on this issue?

@schloerke
Copy link
Collaborator

@alexhallam Coming soon! (<= a few weeks)

@schloerke schloerke added type: enhancement Adds a new, backwards-compatible feature difficulty: advanced Best for maintainers to address effort: medium < 3 days of work priority: medium Could be fixed before next release labels Nov 26, 2018
@schloerke schloerke added the theme: OpenAPI pertains to OpenAPI label Dec 7, 2018
@schloerke schloerke added this to the v0.4.8 - Next CRAN release milestone Dec 7, 2018
@schloerke
Copy link
Collaborator

This can be solved in plumber v1.0.0 using

pr %>%
  pr_set_api_spec(function(spec) {
    # update spec object here
    # return spec object
    spec
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: advanced Best for maintainers to address effort: medium < 3 days of work priority: medium Could be fixed before next release theme: OpenAPI pertains to OpenAPI type: enhancement Adds a new, backwards-compatible feature
Projects
None yet
Development

No branches or pull requests

5 participants