Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Status
colourYellow
titleIn Progress

Spike goals:

  • check if it is possible to define a RAML file without any endpoints just to declare traits and imports, the goal is to generate DTOs and reusable traits.
  • check if it is possible to extend or inherit JSON schemas. if not demonstrate how to include or wrap JSON schemas.

Let's assume we have following *raml* file 

Code Block
themeConfluence
titlelibrary.raml
#%RAML 1.0

title: Book Library API
version: v0.1

documentation:
  - title: Introduction
    content: Automated access to books
  - title: Licensing
    content: Please respect copyrights on our books.

types:
  book: !include bookParent.json
  bookCollection: !include bookCollection.json
  errors: !include raml-util/schemas/errors.schema

traits:
  language: !include raml-util/traits/language.raml

resourceTypes:
  collection: !include raml-util/rtypes/collection.raml
  collection-item: !include raml-util/rtypes/item-collection.raml

/books:
  description: The collection of library books
  type:
    collection:
      schemaCollection: bookCollection
      schemaItem: book
      exampleCollection: !include examples/bookCollection.sample
      exampleItem: !include examples/book.sample

The definition of the book collection looks similar to other collections used in project

Code Block
themeConfluence
titlebookCollection.json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "Collection of books",
  "properties": {
    "books": {
      "type": "array",
      "items": {
        "type": "object",
        "$ref": "bookChild.json"
      }
    },
    "totalRecords": {
      "type": "integer"
    }
  },
  "required": [
    "books",
    "totalRecords"
  ]
}

Here, for the reference field I have used "bookChild.json" file, which is a child  of other json file

Code Block
themeConfluence
titlebookParent.json
{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "description": "A book parent", "additionalProperties": false, "properties": { "id": { "type": "string" }, "name": { "description": "The name of the book",


Info
iconfalse
  • check if it is possible to define a RAML file without any endpoints just to declare traits and imports, the goal is to generate DTOs and reusable traits

Unfortunately, it is not possible to declare RAML file without any endpoint, one endpoint with at least one method should exist.

Info
iconfalse
  • check if it is possible to extend or inherit JSON schemas. if not demonstrate how to include or wrap JSON schemas.

It is possible to extend JSON schemas. The detailed information on how to do it described below.

Let's assume we have following raml file 

Code Block
themeConfluence
titlelibrary.raml
#%RAML 1.0

title: Book Library API
version: v0.1

documentation:
  - title: Introduction
    content: Automated access to books
  - title: Licensing
    content: Please respect copyrights on our books.

types:
  book: !include bookParent.json
  bookCollection: !include bookCollection.json
  errors: !include raml-util/schemas/errors.schema

traits:
  language: !include raml-util/traits/language.raml

resourceTypes:
  collection: !include raml-util/rtypes/collection.raml
  collection-item: !include raml-util/rtypes/item-collection.raml

/books:
  description: The collection of library books
  type:
    collection:
      schemaCollection: bookCollection
      schemaItem: book
      exampleCollection: !include examples/bookCollection.sample
      exampleItem: !include examples/book.sample


The definition of the book collection looks similar to other collections used in project

Code Block
themeConfluence
titlebookCollection.json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "
string" },
object",
  
"author
"
: { "
description": "
The author
Collection of 
the book
books",
  
"
type
properties": 
"string" },
{
    "
metadata
books": {
      "
description
type": "array"
Metadata about creation and changes, provided by the server (client should not provide)",
,
      "items": {
        "type": "object",
        "$ref": "
raml-util/schemas/metadata.schema",
bookChild.json"
      }
    },
    "totalRecords": {
      "
readonly
type": 
true
"integer"
    }
  },
  "required": [
    "
name
books",
    "
author
totalRecords"
  ]
}

Here, for the reference field I have used "bookChild.json" file, which is a child  of other json file


Code Block
themeConfluence
titlebookChildbookParent.json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "A book child",
  "additionalPropertiestype": true"object",
  "extendsdescription" : {"A book parent",
  "$refadditionalProperties" : "bookParent.json"
  }false,
  "properties": {
    "id": {
      "type": "string"
    },
    "publishername": {
      "description": "The publishername of the book",
      "type": "string"
    },
   } }

The main point is to use 

Code Block"extends"
"author": {

  
"$ref" : "<json_file>"
    
}

After project build we will have following models:

Code Block
themeConfluence
titleBookParent
package org.folio.rest.jaxrs.model;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;


/**
 * A book parent
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "id",
    "name",
    "author",
    "metadata"
})
public class BookParent {

    @JsonProperty("id")
    private String id;
    /**
     * The name of the book
     * (Required)
     * 
     */
    @JsonProperty("name")
    @JsonPropertyDescription("The name of the book")
    @NotNull
    private String name;
    /**
     * The author of the book
     * (Required)
     * 
     */
    @JsonProperty("author")
    @JsonPropertyDescription("The author of the book")
    @NotNull
    private String author;
    /**
     * Metadata Schema
     * <p>
     * Metadata about creation and changes to records, provided by the server (client should not provide)
     * 
     */"description": "The author of the book",
      "type": "string"
    },
    "metadata": {
      "description": "Metadata about creation and changes, provided by the server (client should not provide)",
      "type": "object",
      "$ref": "raml-util/schemas/metadata.schema",
      "readonly": true
    }
  },
  "required": [
    "name",
    "author"
  ]
}



Code Block
themeConfluence
titlebookChild.json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "A book child",
  "additionalProperties": false,
  "extends" : {
    "$ref" : "bookParent.json"
  },
  "properties": {
    "publisher": {
      "description": "The publisher of the book",
      "type": "string"
    }
  }
}


The main point is to use 

Code Block
"extends" : {
    "$ref" : "<json_file>"
  }


After project build we will have following models:


Code Block
themeConfluence
titleBookParent
package org.folio.rest.jaxrs.model;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;


/**
 * A book parent
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "id",
    "name",
    "author",
    "metadata"
})
public class BookParent {

    @JsonProperty("metadataid")
    private String id;
 @JsonPropertyDescription("Metadata about creation and changes to records, provided by the server (client should not provide)")   /**
     * The name of the book
     * @Null(Required)
    @Valid * 
  private Metadata metadata; */
    @JsonProperty("idname")
    @JsonPropertyDescription("The name publicof Stringthe getId(book")
 {   @NotNull
    private returnString idname;
    }/**
     *  @JsonProperty("id")
    public void setId(String id) {
The author of the book
     * (Required)
     * 
this.id = id;   */
 }   @JsonProperty("author")
  public BookParent withId(String id@JsonPropertyDescription("The author of the book")
{    @NotNull
    this.idprivate =String idauthor;
    /**
    return * this;Metadata Schema
   }  * <p>
  /**   * Metadata about *creation Theand namechanges ofto therecords, bookprovided by the server (client should *not (Requiredprovide)
     * 
     */
    @JsonProperty("namemetadata")
    public String getName() {
        return name;@JsonPropertyDescription("Metadata about creation and changes to records, provided by the server (client should not provide)")
    }@Null
    @Valid
/**    private Metadata metadata;
*
The name of the book @JsonProperty("id")
    public *String getId(Required) {
    *    return id;
 */   }

    @JsonProperty("nameid")
    public void setNamesetId(String nameid) {
        this.nameid = nameid;
    }

    public BookParent withNamewithId(String nameid) {
        this.nameid = nameid;
        return this;
    }

    /**
     * The authorname of the book
     * (Required)
     * 
     */
    @JsonProperty("authorname")
    public String getAuthorgetName() {
        return authorname;
    }

    /**
     * The authorname of the book
     * (Required)
     * 
     */
    @JsonProperty("authorname")
    public void setAuthorsetName(String authorname) {
        this.authorname = authorname;
    }

    public BookParent withAuthorwithName(String authorname) {
        this.authorname = authorname;
        return this;
    }

    /**
     * MetadataThe Schemaauthor of the book
  * <p>
     * Metadata about creation and changes to records, provided by the server (client should not* provide(Required)
     * 
     */
    @JsonProperty("metadataauthor")
    public MetadataString getMetadatagetAuthor() {
        return metadataauthor;
    }

    /**
     * Metadata SchemaThe author of the book
     * (Required)
     * 
     */
    @JsonProperty("author")
    public void * <p>
setAuthor(String author) {
    * Metadata about creation andthis.author changes= toauthor;
records, provided by the server}
(client
should not provide)  public BookParent withAuthor(String author) *{
      */     @JsonProperty("metadata")
this.author = author;
   public void setMetadata(Metadata metadata) { return this;
    }

this.metadata = metadata;  /**
  }   * Metadata Schema
public BookParent withMetadata(Metadata metadata) { * <p>
     * this.metadataMetadata =about metadata;creation and changes to records, provided by the server return(client this;should not provide)
  }  } * 
Code Block
titleBookChild
package org.folio.rest.jaxrs.model;  import java.util.HashMap;
import java.util.Map;
import javax.validation.Valid;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;


/**
 * A book child
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "id",
    "publisher"
})
public class Book
    extends BookParent
{

    @JsonProperty("id")
    private String id;
    /** */
    @JsonProperty("metadata")
    public Metadata getMetadata() {
        return metadata;
    }

    /**
     * Metadata Schema
     * <p>
     * Metadata about creation and changes to records, provided by the server (client should not provide)
     * 
     */
 The publisher of the book @JsonProperty("metadata")
    public void *setMetadata(Metadata metadata) {
    */     @JsonProperty("publisher")
this.metadata = metadata;
   @JsonPropertyDescription("The publisher}
of
the book")   public BookParent private String publisher;withMetadata(Metadata metadata) {
    @JsonIgnore    this.metadata @Valid= metadata;
   private Map<String, Object> additionalProperties = new HashMap<String, Object>()return this;
    }
@JsonProperty("id")
}



Code Block
titleBookChild
package org.folio.rest.jaxrs.model;

 public String getId() {
        return id;
    }
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;


/**
 * A book child
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    @JsonProperty("idpublisher"
})
public class BookChild
 public  void setId(String id)extends BookParent
{

    /**
  this.id = id; * The publisher of }the book
    public Book* withId(String
id) {    */
    this.id = id;@JsonProperty("publisher")
    @JsonPropertyDescription("The publisher of the book")
 return this;  private String  }publisher;

    /**
     * The publisher of the book
     * 
     */
    @JsonProperty("publisher")
    public String getPublisher() {
        return publisher;
    }

    /**
     * The publisher of the book
     * 
     */
    @JsonProperty("publisher")
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public BookBookChild withPublisher(String publisher) {
        this.publisher = publisher;
        return this;
    }

    @Override
    public Book withName(String name) {
        super.withName(name) publisher;
        return this;
    }

    @Override
    public BookBookChild withAuthorwithId(String authorid) {
        super.withAuthorwithId(authorid);
        return this;
    }

    @Override
    public BookBookChild withMetadatawithName(MetadataString metadataname) {
        super.withMetadatawithName(metadataname);
        return this;
    }

    @JsonAnyGetter@Override
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetterBookChild withAuthor(String author) {
      public void setAdditionalProperty(String name, Object value) {super.withAuthor(author);
        return this;
   this.additionalProperties.put(name, value); }

   } @Override
    public BookBookChild withAdditionalPropertywithMetadata(String name, Object valueMetadata metadata) {
        thissuper.additionalProperties.put(name, valuewithMetadata(metadata);
        return this;
    }

}