I am looking for a way to add a fixed parameter to every request the client sends to the server.
For example: param1=false. The default value for the server is param1=true, but I want the generated client to send false with every request. Is this somehow possible?
I have tried:
default: false - which is documented to not work for this case
defaultValue: false - which seems to only work for the UI
enum: -false - which also seems to only work for the UI
Edit
When I generate Java Code with
- name: param1
in: query
type: boolean
required: true
enum: [true]
The generated code looks like this:
private com.squareup.okhttp.Call routeGetCall(Boolean param1){
Object localVarPostBody = null;
// verify the required parameter 'param1' is set
if (param1 == null) {
throw new ApiException("Missing the required parameter 'param1' when calling routeGet(Async)");
}
... more code ...
Param1 is never forced to be true. I can even set it false. Therefore, enum seems to be only working for the UI?
While it's possible to have a constant parameter with just one possible value, such as ?param1=true:
parameters:
- name: param1
in: query
type: boolean
required: true
enum: [true]
if a parameter has multiple possible values, such as true / false (as in your example), the spec cannot force any specific value for the parameter. It's up to the client to decide which value to use.
That is, the generated client code needs to be modified to use a specific parameter value.
Related
When I send only one parameter, I got query result like string, not like string[]. This heppend only from UI swagger, if I send from Postman - it works good.
I just want send from swagger-ui one parammeter and got array of string, not string
How I can fix it? Help me please.
Example1: send one paramenter and in my controller I got string like '25'
Example2: when I send 2 parameters in controller I can see array of strings ('25', '21')
export class List {
#ApiProperty({ isArray: true, type: String, required: false })
#IsOptional()
public categories?: string[];
}
You should try to spread your parameter in a const in services
edit:
I don't know how to explain in formal words, but a array of strings of one item, for JAVASCRIPT, seems with the same thing as one string value.
Because array is not a type, but a form of a type....
So, if you, in your controller, before do anything with it, you redeclare as:
#Get(":id")
findManybyId(#Param("id") id: string[]) {
const idArray = [...id];
return await this.service.findManyById(idArray);
}
It will solve your problem about being an array
old answer:
You should try to change in your controller where you make your input decorator.
in your case, i don't know if you are using ID to get, but you must to do as the example:
#ApiOperation({
summary: "Get many categories by ID",
})
async getMany(
#Param("id") ids: string[],
) {
return await this.categoriesService.getMany(id);
}
when you fill a single category the query param will be translated as a string while when you fill in several categories understand it as a array.
to solve this problem I added in DTO :
#Transform(({ value }) => (Array.isArray(value) ? value : Array(value)))
I force the cast to array
I have a usecase to do like, if a variable is already defined then return that value else invoke a rest endpoint to get the variable.
get_value = value {
data.value
value
}else = value {
value := <> #invoke rest
}
I will be running OPA as a server and my expectation is like, in the first invokation it will go to the else block then to the first block for rest of the calls. Could you please help me
You cannot write to the in-memory store from a policy, no. Except for that though, your code looks fine, and I'd say it's a pretty common pattern to first check for the existence of a value in data before fetching it via http.send. One thing to note though is that you may use caching on the http.send call, which would effectively do the same thing you're after:
value := http.send({
"url": "https://example.com",
"method": "get",
# Cached for one hour
"force_cache": true,
"force_cache_duration": 3600
})
If the server responds with caching headers, you won't even need to use force_cache but can simply say cache: true and the http.send client will cache according to what the server suggests.
I have an API which is accepting query param as Object. I am using this to add multiple filters to filter the result.
When I hit the request from swagger, I am getting null for my filter object in the controller.
userFilter is the POJO class. It is used as a query param and in the controller, it is coming as null.
On swagger, it is showing as below
userFilter object is not getting constructed and getting NullPointerException in controller class when trying to access any field from userFilter.
I got the solution from swagger.io.
As per the explanation, content is used in complex serialization scenarios that are not covered by style and explode. For example, if we need to send a JSON string in the query string like so:
filter={"type":"t-shirt","color":"blue"}
In this case, we need to wrap the parameter schema into content/<media-type> as shown below.
We need to add content = {#Content(schema = #Schema(type = "object"))} to the #Parameter.
#Parameter(description = "Filters", required = true, content = {#Content(schema = #Schema(type = "object"))})
In JSON format it will look like below.
parameters:
- in: query
name: filter
# Wrap 'schema' into 'content.<media-type>'
content:
application/json: # <---- media type indicates how to serialize / deserialize the parameter content
schema:
type: object
properties:
type:
type: string
color:
type: string
In Twilio Studio I'm setting a number of variables. I then need to send them to a function. I'm using {{flow.variables}} as a parameter to the function. The result I'm getting in the function is
{
All_VARS: '{childcare=yes, camp=yes}'
}
Why is this formatted as a string with =? I would expect {childcare: 'yes', camp: 'yes'}
when I send each variable separately with {{flow.variables.<key>}}I get what I would expect
{
campVar: 'yes',
childcareVar: 'yes'
}
But I don't want to send all the flow set variables individually as parameters, it seems that I should be able simply use {{flow.variables}} but the return value is not usable. What am I missing?
I have a parametrized jenkins job with 2 parameters:
1st job parameter is APIKEY of type 'Password parameter'
2nd job parameter is SERVICE of type 'Active Choices Reactive Parameter' - single select, referencing parameter APIKEY and using following groovy script code which returns value of APIKEY parameter in the single select UI control:
[ APIKEY ]
When I start the build of this job, value offered in single select UI control for parameter SERVICE is garbled (encrypted?) value of APIKEY.
What I want is to be able to use actual (decrypted) value of entered APIKEY password parameter in the script code of SERVICE parameter.
I tried decrypting the APIKEY garbled value by using hudson.util.Secret like below but with no luck:
def apikey = hudson.util.Secret.fromString(APIKEY).getPlainText()
Is there any way to get actual password parameter value from active choices reactive parameter groovy script code?
After a little bit more trying this out it turns out this is working properly after all - but only when password parameter is entered manually, not with the default password parameter value (not sure if this is a bug or a feature).
First time the job is run default password parameter value provided is garbled, but entering the value again in the password field then gives the correct value in groovy script.
This worked for me:
run job build
at this point APIKEY value in groovy script code of the SERVICE field is not evaluated correctly - it is garbled value
enter correct value in APIKEY password parameter field - e.g. "abc123"
switch focus to SERVICE field
SERVICE field groovy code gets executed now and shows actual entered value of APIKEY: "abc123"
Since my use case is such that entering APIKEY is mandatory every time job is build this is good enough for me.
This is an old topic, but I found a solution so I'll add it here in case anyone else still needs it. This was working code, but I sanitized it for publication.
This Groovy script runs in an Active Choices Reactive Parameter. The task is to provide a list of the build versions available to deploy from an internal Artifactory archive. The API key needed for the REST call is stored as Secret Text in our Jenkins instance. So this code reads from the Credentials plugin's repo to find the secret text, then adds it to the header of the http request.
This is a clunky solution. There is much more elegant withCredentials method for Groovy, but it may only work in Jenkins pipelines. I didn't find a way to use it in this parameter.
This solution also does not use HTTPBuilder, which would have been simpler, but wasn't available in our Groovy plugin.
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import groovy.json.JsonSlurper;
def APP_FULL_NAME = "My.Project.Name"
def request = new HttpGet("https://fakeDns/artifactory/api/search/versions?r=releases&a="+APP_FULL_NAME)
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
Jenkins.instance,
null,
null
);
def apiKey
for (creds in jenkinsCredentials)
{
//println creds.id
//println creds.class
if(creds.id == "my_target_api_key")
{
apiKey = creds.secret.toString(creds.secret);
break
}
}
request.addHeader("X-API-KEY", apiKey)
def responseString = new DefaultHttpClient().execute(request, new BasicResponseHandler());
def branchList = new JsonSlurper().parseText(responseString)
//return branchList
def myList= []
branchList.results.each { myList << it }
return myList.version