swagger 2.x reference model from other file - java

I want to create an API with following spec.:
project-api.json
{
"Project": {
"required": [
"uuid"
],
"properties": {
"uuid": {
"type": "string",
"format": "string"
}
"Tasks": {
"type": "array",
"items": {
"$ref": "models/task.json"
}
}
}
...
task.json
{
"Task": {
"required": [
"uuid"
],
"properties": {
"user": {
"type": "string"
},
"title": {
"type": "string"
},
...
Instead of making a reference to models/tasks.json, the generated api generates:
List<ModelstasksJson>
Anyone know how to reference to models from another json file?

Related

Json Schema validation not working for Draft 7 required properties in nested object depending on a value

Request JSON:
{
"apiVersion":"0.0.9b",
"apiDate":"18.01.19",
"general":{
"documentType": "invoice",
},
"references":{
"invoiceId":"123",
"invoiceDate":"01.01.1970",
"creditNoteId":"123",
"creditNoteDate":"01.01.1970"
}
}
I have to validate request json based on documentType value if documentType is invoice then invoiceId and invoiceDate required if documentType is creditNote then creditNoteId and creditNoteDate required.
I tried the below schema but it is not working as expected
Updated JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"apiVersion",
"apiDate"
],
"oneOf": [
{
"properties": {
"general": {
"properties": {
"documentType": {
"enum": [
"invoice"
]
}
}
},
"references": {
"required": [
"invoiceId",
"invoiceDate"
]
}
}
},
{
"properties": {
"general": {
"properties": {
"documentType": {
"enum": [
"creditNote"
]
}
}
},
"references": {
"required": [
"creditNoteId",
"creditNoteDate"
]
}
}
}
],
"properties": {
"apiVersion": {
"type": "string",
"description": "The version of the json file"
},
"apiDate": {
"type": "string",
"description": "The date when the json version was published"
},
"general": {
"$ref": "#/definitions/general_identifiers"
},
"references": {
"type": "object",
"properties": {
"invoiceId": {
"type": "string"
},
"invoiceDate": {
"type": "string"
},
"creditNoteId": {
"type": "string"
},
"creditNoteDate": {
"type": "string"
}
}
}
},
"definitions": {
"general_identifiers": {
"type": "object",
"required": [
"documentType"
],
"properties": {
"documentType": {
"enum": [
"invoice",
"creditNote"
]
}
}
}
}
}
No validation Error even if m not setting any of required field.
What have I missed?
Thanks in advance :)
The validator response is correct. Your schema requires the "references" property to be under the "general" node.
But this is not the case in your sample json (it is on the same level, not as child)
"general":{
"documentType": "invoice",
},
"references":{
...}

Elastic Search, creating index with sources and settings by Rest Java Client

I'm trying to create index following this guide: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-create-index.html#_providing_the_whole_source
The problem is, that index is not created properly. Looks like whole settings section as well as completion type is ignored.
My json file:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"analysis": {
"filter": {},
"analyzer": {
"keyword_analyzer": {
"filter": [
"lowercase",
"asciifolding",
"trim"
],
"char_filter": [],
"type": "custom",
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"first": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
},
"second": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
},
"third": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
},
"fourth": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
}
}
}
}
}
Java code:
CreateIndexRequest indexRequest = new CreateIndexRequest(ESClientConfiguration.INDEX_NAME);
URL url = Resources.getResource(TERYT_INDEX_CONFIGURATION_FILE_NAME);
return Try.of(() -> Resources.toString(url, Charsets.UTF_8))
.map(jsonIndexConfiguration -> indexRequest.source(jsonIndexConfiguration, XContentType.JSON))
.get();
createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2));
Try.of(() -> client.indices().create(createIndexRequest, RequestOptions.DEFAULT))...
Index is created but when I look into Index Metadata, it looks completly wrong:
{
"state": "open",
"settings": {
"index": {
"creation_date": "1556379012380",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "L5fmkrjeQ6eKmuDyZ3MP3g",
"version": {
"created": "7000099"
},
"provided_name": "my_index"
}
},
"mappings": {
"my_type": {
"properties": {
"first": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"second": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"third": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"fourth": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
},
"aliases": [],
"primary_terms": {
"0": 1
},
"in_sync_allocations": {
"0": [
"Cx6tBeohR8mzbTO74dwsCw",
"FcTUhpb_SL2LiaEyy_uwkg"
]
}
}
There is only 1 shard without replicas, I also don't see any informations about completion type. Does someone could tell me what I'm doing wrong here?
I think that this line:
Try.of(() -> client.indices().create(createIndexRequest, RequestOptions.DEFAULT))...
is hiding an important exception.
Here you are using elasticsearch 7.0.0 which does not allow anymore giving a "type" name in your mapping.
Instead of
"mappings": {
"my_type": {
"properties": {
You should write:
"mappings": {
"properties": {
Because of the exception and the fact that probably after index creation you are indexing some documents, default index settings and mapping are applied.
Which explains what you are seeing.
You need to fix your index settings first.
I'd recommend doing that in Kibana dev console.

Insert object to nested array

I'm trying to insert an object to a nested array through the java api, but I get the following error
MapperParsingException[object mapping for [X] tried to parse field [null] as object, but found a concrete value]
doing it through Kibana as shown below the same script works.
Any ideas on how to fix this?
The java code is the following
HashMap<String, Object> params = new HashMap<>();
params.put("object", objectAsString);
Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.media.add(params.object)", params);
UpdateResponse result = elasticClient.prepareUpdate(indexName, "Type", documentId).setScript(script).execute().actionGet();
Trhough Kibana
POST index/document/id/_update
{
"script": {
"lang": "painless",
"inline": "ctx._source.media.add(params.object)",
"params": {
"object": {
"description" : "A second image",
"height" : 5,
"weight": 5,
"name" : "Test",
"orientation" : "Vertical",
"url" : "htttp://newurl.jpg",
"tags":["first","second"],
"type":"image"
}
}
}
}
The template mapping is the following:
"mappings": {
"FOLDER_MODULE": {
"properties": {
"name": {
"type": "keyword"
},
"publisherId": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"media": {
"type": "nested",
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"publisherId": {
"type": "short"
},
"tags": {
"type": "text"
},
"description": {
"type": "text"
},
"url": {
"type": "keyword"
},
"createdDate": {
"format": "strict_date_optional_time||epoch_millis",
"type": "date"
},
"height": {
"type": "float"
},
"width": {
"type": "float"
},
"weight": {
"type": "float"
},
"orientation": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"type": {
"type": "keyword"
},
"username": {
"type": "keyword"
}
}
}
}
}
}

How to execute mail Task process on camunda cockpit?

we have workflow that has service task attached to the box , Below template is applied to bmpn:serviceTask, Now when we deploy workflow to camunda using https://engine-rest/deployment/create , we are able to run the process but we see this error on incident tab,we are using bpmn-js node library for modeler and create workflows, where we define camunda:class, as you can see error below, Any idea how to resolve this camunda class issue ?
we are just trying to send email notification when process starts ?
New to camunda stuff any help will be appreciated.
template.js
{
"name": "Mail_Task_2",
"id": "com.camunda.example.MailTask_2",
"createdBy": "sh529u",
"users": [],
"groups": [],
"properties": [
{
"binding": {
"name": "camunda:class",
"type": "property"
},
"editable": false,
"value": "com.mycompany.MailTaskImpl",
"type": "String",
"label": "Implementation Type"
},
{
"label": "Sender",
"type": "String",
"binding": {
"type": "camunda:inputParameter",
"name": "sender"
},
"constraints": {
"notEmpty": true
}
},
{
"label": "Receivers",
"type": "String",
"binding": {
"type": "camunda:inputParameter",
"name": "receivers"
},
"constraints": {
"notEmpty": true
}
},
{
"label": "Template",
"description": "By the way, you can use freemarker templates here",
"value": "Hello ${firstName}!",
"type": "Text",
"binding": {
"type": "camunda:inputParameter",
"name": "messageBody",
"scriptFormat": "freemarker"
},
"constraints": {
"notEmpty": true
}
},
{
"label": "Result Status",
"description": "The process variable to which to assign the send result to",
"type": "String",
"value": "mailSendResult",
"binding": {
"type": "camunda:outputParameter",
"source": "${ resultStatus }"
}
},
{
"label": "Send Async?",
"type": "Boolean",
"value": true,
"binding": {
"type": "property",
"name": "camunda:asyncBefore"
}
}
],
"appliesTo": [
"bpmn:ServiceTask"
]
}
ERROR
org.camunda.bpm.engine.ProcessEngineException: ENGINE-09008 Exception while instantiating class 'com.mycompany.MailTaskImpl': ENGINE-09017 Cannot load class 'com.mycompany.MailTaskImpl': com.mycompany.MailTaskImpl
at org.camunda.bpm.engine.impl.util.EngineUtilLogger.exceptionWhileInstantiatingClass(EngineUtilLogger.java:78)

generating POJOs from JSON Schema for non-object types

I am trying to generate POJOs from the JSON Schema of XMBC.
I do this with jsonschema2pojo.
However, nothing gets generated. It doesn't even bring me an error.
This is a reduced sample json schema I am trying to generate from:
{
"description": "JSON-RPC API of XBMC",
"id": "http://xbmc.org/jsonrpc/ServiceDescription.json",
"methods": {
"Addons.ExecuteAddon": {
"description": "Executes the given addon with the given parameters (if possible)",
"params": [
{
"name": "addonid",
"required": true,
"type": "string"
},
{
"default": "",
"name": "params",
"type": [
{
"additionalProperties": {
"default": "",
"type": "string"
},
"type": "object"
},
{
"items": {
"type": "string"
},
"type": "array"
},
{
"description": "URL path (must start with / or ?",
"type": "string"
}
]
},
{
"default": false,
"name": "wait",
"type": "boolean"
}
],
"returns": {
"type": "string"
},
"type": "method"
}
},
"notifications": {
"Application.OnVolumeChanged": {
"description": "The volume of the application has changed.",
"params": [
{
"name": "sender",
"required": true,
"type": "string"
},
{
"name": "data",
"properties": {
"muted": {
"required": true,
"type": "boolean"
},
"volume": {
"maximum": 100,
"minimum": 0,
"required": true,
"type": "integer"
}
},
"required": true,
"type": "object"
}
],
"returns": null,
"type": "notification"
}
},
"types": {
"Addon.Content": {
"default": "unknown",
"enums": [
"unknown",
"video",
"audio",
"image",
"executable"
],
"id": "Addon.Content",
"type": "string"
}
},
"version": "6.14.3"
}
I must admin that my knowledge of JSON is very terse, maybe it is just a simple fault of mine. But can anyone help me how I can generate Java objects from such a JSON Schema?
The JSON Schema doesn't support method. JSON schema defines json data structure, it would not be used to define your methods. Most important attribute in JSON schema is properties.
It's good to generate POJO data models from a JSON schema, but not business logic. You can learn the JSON schema from those examples.

Categories