Skip to content

Commit

Permalink
Implemented: Do not require an id on inline schema definitions #25
Browse files Browse the repository at this point in the history
We're ready for release 0.5.0.
  • Loading branch information
rigolepe committed Feb 19, 2016
1 parent f011553 commit 1c282b0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ notifications:
email: email:
recipients: recipients:
- peter.rigole@atomicbits.io - peter.rigole@atomicbits.io
after_success: #after_success:
- '[[ $TRAVIS_BRANCH == "develop" ]] && { sbt "+ publish" ; };' #- '[[ $TRAVIS_BRANCH == "develop" ]] && { sbt "+ publish" ; };'


env: env:
global: global:
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@


package io.atomicbits.scraml.generator.lookup package io.atomicbits.scraml.generator.lookup


import java.util.UUID

import io.atomicbits.scraml.generator.model.Language import io.atomicbits.scraml.generator.model.Language
import io.atomicbits.scraml.jsonschemaparser.model._ import io.atomicbits.scraml.jsonschemaparser.model._
import io.atomicbits.scraml.jsonschemaparser.{AbsoluteId, JsonSchemaParseException, RootId} import io.atomicbits.scraml.jsonschemaparser.{ImplicitId, AbsoluteId, JsonSchemaParseException, RootId}


import scala.annotation.tailrec import scala.annotation.tailrec


Expand All @@ -40,22 +42,22 @@ object SchemaLookupParser {




/** /**
* Expand all relative ids to absolute ids and register them in the schema lookup and also expand all $ref pointers. * Expand all relative ids to absolute ids and register them in the schema lookup and also expand all $ref pointers.
* *
* @param schema * @param schema
* @return * @return
*/ */
def expandRelativeToAbsoluteIds(schema: Schema): Schema = { def expandRelativeToAbsoluteIds(schema: Schema): Schema = {


/** /**
* Expand the ids in a schema based on the nearest root id of the enclosing schemas. * Expand the ids in a schema based on the nearest root id of the enclosing schemas.
* *
* @param schema the schema whose ids need expanding * @param schema the schema whose ids need expanding
* @param root the nearest (original) root id that was found in the enclosing schemas * @param root the nearest (original) root id that was found in the enclosing schemas
* @param expandingRoot the root that we're expanding (creating) based on the seed (the nearest original root id) * @param expandingRoot the root that we're expanding (creating) based on the seed (the nearest original root id)
* @param path the fragment path we're on * @param path the fragment path we're on
* @return a copy of the original schema in which all ids are replaced by root ids * @return a copy of the original schema in which all ids are replaced by root ids
*/ */
def expandWithRootAndPath(schema: Schema, root: RootId, expandingRoot: RootId, path: List[String] = List.empty): Schema = { def expandWithRootAndPath(schema: Schema, root: RootId, expandingRoot: RootId, path: List[String] = List.empty): Schema = {


val currentRoot = val currentRoot =
Expand All @@ -78,7 +80,8 @@ object SchemaLookupParser {
objEl.copy( objEl.copy(
fragments = objEl.fragments.map(expandFragment), fragments = objEl.fragments.map(expandFragment),
properties = objEl.properties.map(expandFragment), properties = objEl.properties.map(expandFragment),
selection = objEl.selection.map(select => select.map(schema => expandWithRootAndPath(schema, currentRoot, expandingRoot, path))) selection = objEl.selection
.map(select => select.map(schema => expandWithRootAndPath(schema, currentRoot, expandingRoot, path)))
) )
case frag: Fragment => frag.copy(fragments = frag.fragments.map(expandFragment)) case frag: Fragment => frag.copy(fragments = frag.fragments.map(expandFragment))
case arr: ArrayEl => case arr: ArrayEl =>
Expand Down Expand Up @@ -106,19 +109,24 @@ object SchemaLookupParser {


schema.id match { schema.id match {
case rootId: RootId => expandWithRootAndPath(schema, rootId, rootId) case rootId: RootId => expandWithRootAndPath(schema, rootId, rootId)
case ImplicitId =>
// We assume we hit an inline schema without an id, so we may just invent a random unique one since it will never be referenced.
val uniqueName = UUID.randomUUID().toString
val rootId = RootId(s"http://atomicbits.io/schema/$uniqueName.json")
expandWithRootAndPath(schema.updated(rootId), rootId, rootId)
case _ => throw JsonSchemaParseException("We cannot expand the ids in a schema that has no absolute root id.") case _ => throw JsonSchemaParseException("We cannot expand the ids in a schema that has no absolute root id.")
} }


} }




/** /**
* *
* @param lookup The schema lookup * @param lookup The schema lookup
* @param linkedSchema A tuple containing a field name and the schema the field refers to. Nothing is done with the * @param linkedSchema A tuple containing a field name and the schema the field refers to. Nothing is done with the
* field name, it is there to make folding easier on schema fragments and object properties. * field name, it is there to make folding easier on schema fragments and object properties.
* @return The schema lookup with added object references. * @return The schema lookup with added object references.
*/ */
def updateLookupTableAndObjectMap(lookup: SchemaLookup, linkedSchema: (String, Schema)): SchemaLookup = { def updateLookupTableAndObjectMap(lookup: SchemaLookup, linkedSchema: (String, Schema)): SchemaLookup = {




Expand Down Expand Up @@ -176,9 +184,9 @@ object SchemaLookupParser {




/** /**
* For each unprocessed object, lookup the selection references and collect al selection objects recursively and * For each unprocessed object, lookup the selection references and collect al selection objects recursively and
* fill in the parent-child relations. * fill in the parent-child relations.
*/ */
def updateObjectHierarchy(schemaLookup: SchemaLookup): SchemaLookup = { def updateObjectHierarchy(schemaLookup: SchemaLookup): SchemaLookup = {


@tailrec @tailrec
Expand Down Expand Up @@ -213,9 +221,9 @@ object SchemaLookupParser {




/** /**
* Check if there is a type field present in each leaf-object that is an EnumEl with one element and fill in the * Check if there is a type field present in each leaf-object that is an EnumEl with one element and fill in the
* typeDiscriminatorValue field in each of them. * typeDiscriminatorValue field in each of them.
*/ */
def updateTypeDiscriminatorFields(schemaLookup: SchemaLookup): SchemaLookup = { def updateTypeDiscriminatorFields(schemaLookup: SchemaLookup): SchemaLookup = {


schemaLookup.objectMap.foldLeft(schemaLookup) { (lookup, objPair) => schemaLookup.objectMap.foldLeft(schemaLookup) { (lookup, objPair) =>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
application/vnd-v1.0+json: application/vnd-v1.0+json:
schema: | schema: |
{ {
"id": "http://atomicbits.io/schema/anonymous3.json",
"$ref": "http://atomicbits.io/schema/paged-list.json#", "$ref": "http://atomicbits.io/schema/paged-list.json#",
"genericTypes": { "genericTypes": {
"T": { "T": {
Expand All @@ -117,7 +116,6 @@
application/vnd-v1.0+json: application/vnd-v1.0+json:
schema: | schema: |
{ {
"id": "http://atomicbits.io/schema/anonymous1.json",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "user.json" "$ref": "user.json"
Expand All @@ -129,7 +127,6 @@
application/vnd-v1.0+json: application/vnd-v1.0+json:
schema: | schema: |
{ {
"id": "http://atomicbits.io/schema/anonymous2.json",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "user.json" "$ref": "user.json"
Expand Down
2 changes: 1 addition & 1 deletion project/BuildSettings.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ trait BuildSettings {
publishTo := { publishTo := {
val nexus = "https://oss.sonatype.org/" val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value) if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots") None // Some("snapshots" at nexus + "content/repositories/snapshots")
else else
Some("releases" at nexus + "service/local/staging/deploy/maven2") Some("releases" at nexus + "service/local/staging/deploy/maven2")
}, },
Expand Down

0 comments on commit 1c282b0

Please sign in to comment.