Generate response example values using swagger with GRPC - swagger

I'm generating swagger json file using protoc-gen-swagger with gRPC service. The output json is being generated with empty response examples, I want to add response examples to the definition so it gets automatically populated in the generated json.
This is my current definition.
service UserService {
rpc GetUser (GetUserRequest) returns (UserResponse){
option (google.api.http) = {
get: "/api/v1/user/{username}"
response_body: "*"
};
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
description: "Returns user object";
operation_id: "get_user";
summary: "Get User";
};
}
}
message GetUserRequest {
string username = 1;
}
message UserResponse {
User user = 1;
}
message User {
string first_name = 1;
string last_name = 2;
string username = 3;
}
When I generate the swagger file using the command
protoc -I ${PROTOPATH} \
-I $GOPATH/src \
--swagger_out=logtostderr=true:${OUT_PATH}
I get a swagger file with this user object definition
"User": {
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"username": {
"type": "string"
},
}
}
What I want is to generate it with example values like this:
"User": {
"type": "object",
"properties": {
"first_name": {
"type": "string",
"example": "Adam"
},
"last_name": {
"type": "string",
"example": "Smith"
},
"username": {
"type": "string",
"example": "asmith79"
},
}
}

Found the answer to this here: https://github.com/grpc-ecosystem/grpc-gateway/blob/master/examples/internal/proto/examplepb/a_bit_of_everything.proto#L197
Simple by adding grpc.gateway.protoc_gen_swagger.options.openapiv2_schema as an option to the message.
import "protoc-gen-swagger/options/annotations.proto";
message User {
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
example: { value: '{ "first_name": "Adam", "last_name":"Smith", "username":"asmith79"}' }
};
string first_name = 1;
string last_name = 2;
string username = 3;
}

Related

why nestjs swargger string array not working

export class AdministratorLoginDto {
#ApiProperty()
userName: number[];
}
I set userName with number[], but i get the openapi is string[].
I set userName with number[], but i get the openapi json is string[].
{
"AdministratorLoginDto": {
"type": "object",
"properties": {
"userName": {
"type": "array",
"items": {
"type": "string" // question: why this type is not number?
}
}
},
"required": [
"userName"
]
}
}
Thank you!
Try this :
export class AdministratorLoginDto {
#ApiProperty({
type: Number,
isArray: true,
})
userName: number[];
}

I am having difficulty specifying an XML example for a response in OpenAPI 3

I have a JSON file for OpenAPI 3 that contains the following:
"trait_hasProjectResponse_200": {
"description": "",
"content": {
"application/xml": {
"example": {
"value" : "<project><foo>1</foo><bar>2</bar></project>"
}
}
}
},
This results in the following display on the current swagger-ui:
How do I specify an example of XML for a parameter or response in the OpenAPI 3 spec? I have looked through the documentation and it seems to be targest mostly at JSON. What do I need to do the my output that is generating the OpenAPI 3 JSON file.
I have also tried to use the externalValue and have has similar difficulties.
Remove the value key from the example (value is only used with multiple examples).
"example": "<project><foo>1</foo><bar>2</bar></project>"
Alternatively, you can define a schema for the response, and Swagger UI will generate examples based on the schema. In your example, the schema is a project object that contains the foo array. You can specify [1, 2] as the example values for the foo array:
"components": {
"responses": {
"trait_hasProjectResponse_200": {
"description": "",
"content": {
"application/xml": {
"schema": {
"$ref": "#/components/schemas/project"
}
}
}
}
},
"schemas": {
"project": {
"type": "object",
"properties": {
"foo": {
"type": "array",
"items": {
"type": "integer"
},
"xml": {
"wrapped": false
},
"example": [1, 2]
}
}
}
}
}
This will give you:

Elasticsearch create index errors out

I am very new to Elasticsearch.Using ES 5.1.1, I am trying to create the following simple index:
curl -XPOST "http://localhost:9200/user" -d'
{
"mappings": {
"post": {
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"birth_date": {
"type": "date"
}
}
}
}
}'
But I am getting an HTTP 400 with the following error message:
No handler found for uri [/user] and method [POST]
Since ES 5, you must use PUT instead of POST when creating new indices:
curl -XPUT "http://localhost:9200/user" -d'
{
"mappings": {
"post": {
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"birth_date": {
"type": "date"
}
}
}
}
}'

Webhook input not available in scripting step?

I have an internal app that uses a webhook listener and some scripting to manipulate the input data. I'm posting this to it:
curl -X POST -d '{
"assignment_id": 12345,
"updated_custom_fields": [{
"name": "RNVIDAYEBB",
"value": "updated!"
},
{
"name": "QUFTXSIBYA",
"value": "and me too"
}
],
"custom_fields": [{
"id": 981,
"name": "RDEXDPVKRD",
"fields": [
{
"id": 4096,
"name": "RNVIDAYEBB",
"default": "EDJEAJICYW",
"required": true,
"value": "Blah"
},
{
"id": 4097,
"name": "QUFTXSIBYA",
"default": "",
"required": true,
"value": ""
}]
}]
}' "https://hooks.zapier.com/hooks/catch/......"
My script is as follows:
update_custom_fields_by_name_pre_write: function(bundle) {
var updatedFields = _.map(bundle.request.data.custom_fields, function(group) {
return _.map(group.fields, function(field) {
return _.extend(field, _.findWhere(bundle.request.data.updated_custom_fields, { name: field.name} ));
});
});
bundle.request.data = updatedFields;
return bundle.request;
}
I know that the merging logic is good, but it appears that the custom_fields and updated_custom_fields arrays are not present in the bundle.request.data object. Anyone know how to get access to them in the script?
It seems like you should be using update_custom_fields_by_name_catch_hook to capture the incoming static webhook data (instead of _pre_write). If you use that, you can capture the data within bundle.cleaned_request.custom_fields and bundle.cleaned_request.updated_custom_fields.

How to store business ID in elasticsearch?

I'm trying to store tweets in some Elasticsearch index using Spring Data Elasticsearch (for tweet requesting , I'm using twitter4j).
I have followed some basic example and I'm using this basic annotated POJO (metadatas with complex type have been removed) :
#Document(indexName = "twitter", type = "tweet")
public class StorableTweet {
#Id
private long id;
private String createdAt;
private String text;
private String source;
private boolean isTruncated;
private long inReplyToStatusId;
private long inReplyToUserId;
private boolean isFavorited;
private boolean isRetweeted;
private int favoriteCount;
private String inReplyToScreenName;
private String userScreenName = null;
// Getters/setters removed
}
To store a tweet using this model, I use :
public interface TweetRepository extends ElasticsearchRepository<StorableTweet, Long> {
}
and in my storing service :
tweetRepository.save(storableTweet);
It works fine, but my tweet Id is stored in "_id" (why not) and some other number coming from nowhere is stored in "id" (why....?) :
{
"_index": "twitter",
"_type": "tweet",
**"_id": "655008947099840512"**, <-- this is the real tweet id
"_version": 1,
"found": true,
"_source":
{
**"id": 655008947099840500**, <-- this number comes from nowhere
"createdAt": "Fri Oct 16 15:14:37 CEST 2015",
"text": "tweet text(...)",
"source": "Twitter for iPhone",
"inReplyToStatusId": -1,
"inReplyToUserId": -1,
"favoriteCount": 0,
"inReplyToScreenName": null,
"user": "971jml",
"favorited": false,
"retweeted": false,
"truncated": false
}
}
What I would like is either my tweet id stored in "_id" (and no "id" field), either my tweet id stored in "id" an having a generated number in "_id", and get rid of this random useless number in "id".
EDIT
mapping :
{
"twitter":
{
"mappings":
{
"tweet":
{
"properties":
{
"createdAt":
{
"type": "string"
},
"favoriteCount":
{
"type": "long"
},
"favorited":
{
"type": "boolean"
},
"inReplyToScreenName":
{
"type": "string"
},
"inReplyToStatusId":
{
"type": "long"
},
"inReplyToUserId":
{
"type": "long"
},
"retweeted":
{
"type": "boolean"
},
"source":
{
"type": "string"
},
"text":
{
"type": "string"
},
"truncated":
{
"type": "boolean"
},
"tweetId":
{
"type": "long"
},
"user":
{
"type": "string"
}
}
}
}
}
}
EDIT 2 : It looks like the problem is not about #Id annotation but about "long" type. Some other longs (not all) are transformed (a few units more or less) when stored into elasticsearch via Spring Data Elasticsearch.

Resources