What is the proper format to expect when using an api to create a resource that references another resource - spring

I am adding a feature that allows users to select from a list of people of a certain type, Type1 and Type2. A type would be chosen from a dropdown, and the data from the API would look like
{
"id": 1,
"name": "TYPE1",
"desc": "Type 1 Person"
}
I am creating a POST endpoint that allows an admin user to insert more people into the list, but I'm unsure on the best way for the admin to include the person's type. In other languages/frameworks, I would do something like this:
{
"first_name": "John",
"last_name": "Doe",
"type_id": 1
}
then handle adding the entry in my own SQL. In Spring though, I'm trying to leverage an object being created from the data automatically. For this to be successful, I've need to send the data as:
{
"first_name": "John",
"last_name": "Doe",
"type": {
"id": 1,
"name": "TYPE1",
"desc": "Type 1 Person"
}
}
My question is in two parts.
In Spring, is there anything I can leverage that would allow me to just pass an identifier for person type when creating a new person entry? (I've looked into DTOs, but I've never used them, so I don't know if that is the proper solution)
In REST in general, how much data should be required when adding a resource that references another resource?

Related

Is there a way to write an Expression in Power Automate to retrieve item from SurveyMonkey?

There is no dynamic content you can get from the SurveyMonkey trigger in Power Automate except for the Analyze URL, Created Date, and Link. Is it possible I could retrieve the data with an expression so I could add fields to SharePoint or send emails based on answers to questions?
For instance, here is some JSON data for a county multiple choice field, that I would like to know the county so I can have the email sent to the correct person:
{
"id": "753498214",
"answers": [
{
"choice_id": "4963767255",
"simple_text": "Williamson"
}
],
"family": "single_choice",
"subtype": "menu",
"heading": "County where the problem is occurring:"
}
And basically, a way to create dynamic fields from the content so it would be more usable?
I am a novice so your answer will have to assume I know nothing!
Thanks for considering the question.
Overall, anything I have tried is unsuccessful!
I was able to get an answer on Microsoft Power Users support.
Put this data in compose action:
{
"id": "753498214",
"answers": [
{
"choice_id": "4963767255",
"simple_text": "Williamson"
}
],
"family": "single_choice",
"subtype": "menu",
"heading": "County where the problem is occurring:"
}
Then these expressions in additional compose actions:
To get choice_id:
outputs('Compose')?['answers']?[0]?['choice_id']
To get simple_text:
outputs('Compose')?['answers']?[0]?['simple_text']
Reference link here where I retrieved the answer is here.
https://powerusers.microsoft.com/t5/General-Power-Automate/How-to-write-an-expression-to-retrieve-answer/m-p/1960784#M114215

What is the best way to accept multiple objects as input for a CLI?

Let's say I have a CLI which talks to a banking server to create a bank account. A bank account has a few properties, and a list of users associated with the account. If the bank account is represented as a json, it'll be
{
"accountType": "Savings",
"Balance": 500,
"Users": [
{
"firstName": "tony",
"lastName": "stark",
"nickName": "ironman"
},
{
"firstName": "Peter",
"lastName": "Parker",
"nickName": "spiderman"
}
]
}
I want to create this account object using a CLI, and I want to pass all the users at create time. Like
bank-cli account create --acount-type Savings --balance 500 <And the users>
How do I go about adding the users in the CLI? Adding the values like --firstName1 --lastName1 --firstName2 --lastName2 does not seem like a good UX. My current implementation is that this CLI opens up another editor, which accepts multiple lines of these parameters as
--firstName tony --lastName stark --nickname ironman
--firstName peter --lastName parker --nickname spiderman
And this is converted to an array of objects in the code. Is there a better way to do this?
I have considered accepting the parameters as json/toml or other formats. They didn't look too clean.

Insert Document with reference to Existing Document in another Collection

I'm using Java Spring and MongoDB.
I have two collections: customer and order.
I have a reference from the order to the customer collection.
I have an already existing customer.
I want to create a new order with reference to the existing customer.
My POST body request looks like this:
{
"type": "SaaS",
"units": 5,
"price": 30000,
"customer":{
"$ref": "customer",
"$id": {
"oid": "6230853866f97257c050d330"
}
}
}
However, the java serialization process can't resolve the customer subdocument. I understand that I need to apply some logic here but I can't find nor understand how to do it. Basically in mongosh syntax it look similar to this:
db.order.updateOne({_id: ObjectId("623070ab3207ac1de9f8351c")}, {$set: {customer: new DBRef('customer', new ObjectId("6230824c942afc6dee673f3b"))}})

Generating GraphQL GUI from Schema and Schema from GUI

While using GraphiQL works well, my boss has asked me to implement a user interface where users can check elements presented to them via UI elements like checkbox, map relationships and get the data and doing this will generate a graphql input for the person, call the API and get the result back to the user.
So, basically this involves 2 generations. Generating a user interface from a GraphQL schema and generating a GraphQL input query from the user's selection.
I searched and I was not able to find any tools which already do this. My server is in Node and I am using Express GraphQL. I converted my express schema to GraphQLSchema language using https://github.com/graphql-cli/graphql-cli and I introspected the GraphQLSchema language using the introspect function at https://github.com/sheerun/graphqlviz/blob/master/cli.js
The object which I got was something like this (only partial schema output given below)
`
"data": {
"__schema": {
"queryType": {
"name": "Query"
},
"mutationType": {
"name": "Mutation"
},
"subscriptionType": null,
"types": [{
"kind": "OBJECT",
"name": "Query",
"description": null,
"fields": [{
"name": "employee",
"description": null,
"args": [{
"name": "ecode",
"description": null,
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}],
`
I am looping through the elements trying to generate UI but I am quite stuck.
What is the best way to do this? Thanks in advance.
Well for the part of generating the ui from the introspection query, I think that the response contains enough data for a sufficient ui (description for each field can be used as a placeholder for each field's input box). If you're asking how can you generate a dynamic form from the introspection response, you can take a look at other projects that created transformers from json to html forms for inspiration/usage (take a look at https://github.com/brutusin/json-forms/blob/master/README.md#cdn). For complex fields (not primitive types) you may need to do some more work.
For the schema generation from the ui, you can use any query builder tool, and build a query according to the user inputs. Each combobox will be mapped to a specific SCHEMANAME.FIELDNAME and the value will be the value of the input box.
I hope it helped a bit. BTW, it sounds like an interesting tool so let us know if you succeed!

Output for SCIM User PUT method when updating a composite attribute

I have a user in mu user store with following attributes.
{
"id": "bfae138c-9f57-4ff1-ab63-599f2034371f",
"schemas":[
"urn:scim:schemas:core:1.0"
],
"name":{
"formatted": "Ms. Barbara J Jensen III",
"familyName": "Jensen",
"givenName": "Barbara"
},
"userName": "bjensen123",
"externalId": "bjensen",
"meta":{
"lastModified": "2015-05-25T08:59:28",
"location": "https://localhost:9443/wso2/scim/Users/bfae138c-9f57-4ff1-ab63-599f2034371f",
"created": "2015-05-25T08:59:28"
}
}
I'm sending a put request to this resource with following method body.
{
"schemas":["urn:scim:schemas:core:1.0"],
"userName":"bjensen123",
"name":{
"formatted":"Ms. Bb",
}
}
What should be the name attribute of my resulting resource?
"name":{
"formatted":"Ms. Bb",
}
or
"name":{
"formatted": "Ms. Bb",
"familyName": "Jensen",
"givenName": "Barbara"
}
The PUT request is to be handled as a complete update, as opposed to a PATCH, which would only update specified attributes, and is optional for the implementer (per the SCIM 1.1 spec). The intent of a PUT is that the requester first perform a read (GET) of the user, change the desired attributes, and provide a comprehensive update, to include those attributes that aren’t actually changing (password being the one exception). Any attributes that aren’t specified with values in the PUT request would be blown away. Thus, per your example, the PUT response would come back as:
"name":{"formatted":"Ms. Bb"}
If you don’t want to lose the familyName and givenName, you have to re-specify those as well (along with any other attributes that you don’t want to blow away). Here’s the spec definition:
http://www.simplecloud.info/specs/draft-scim-api-01.html

Resources