Skip to content

Commit

Permalink
Release v1.1.9 (#13)
Browse files Browse the repository at this point in the history
* Remove dead code
* Fix the maxDepth config bug
* Fix the Option[] wrap bug
* Updated README with conversion example
* 1.1.9
  • Loading branch information
cchandurkar authored Feb 21, 2021
1 parent 535cd10 commit b3c0773
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 18 deletions.
65 changes: 61 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
# json-schema-to-case-class
A library to convert complex JSON Schema to [Scala Case Classes](https://docs.scala-lang.org/tour/case-classes.html). Supports both NodeJs and Browser environments.
[Try Online Editor](https://cchandurkar.github.io/case-class-generator/).
<br />[**Try Online Editor**](https://cchandurkar.github.io/case-class-generator/).

![Build Status](https://github.com/cchandurkar/json-schema-to-case-class/actions/workflows/build-and-deploy.yml/badge.svg?branch=main)
![Build Status](https://github.com/cchandurkar/json-schema-to-case-class/actions/workflows/build-and-deploy.yml/badge.svg?branch=main)
[![npm version](https://badge.fury.io/js/json-schema-to-case-class.svg)](https://badge.fury.io/js/json-schema-to-case-class)
[![License](https://img.shields.io/npm/l/json-schema-to-case-class.svg)](LICENSE)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![PRs Welcome](https://img.shields.io/badge/$-support-green.svg?style=flat-square)](https://github.com/sponsors/cchandurkar)

<table width="100%">
<tr>
<th>
JSON Schema
</th>
<th>
Generated Scala Case Classes
</th>
</tr>
<tr>
<td>
<pre>
{
"title": "Product",
"type": "object",
"required": [ "productId", "productName", "price" ],
"properties": {
"productId": { "type": "integer" },
"productName": { "type": "string" },
"price": { "type": "number" },
"tags": {
"type": "array",
"items": { "type": "string" }
},
"dimensions": {
"type": "object",
"properties": {
"length": { "type": "number" },
"width": { "type": "number" },
"height": { "type": "number" }
}
}
}
}
</pre>
</td>
<td>
<pre>
case class Product (
productId: Integer,
productName: String,
price: Double,
tags: Option[List[String]],
dimensions: Option[Dimensions]
)
</pre>
<pre>
case class Dimensions (
length: Double,
width: Double,
height: Double
)
</pre>
</td>
</tr>
</table>

### Features
1. Resolve local as well as remote schema references.
Expand Down Expand Up @@ -82,8 +138,9 @@ All contributions, enhancements, and bug-fixes are welcome. Open an issue or cre

##### Short-term Goals
1. Handle `allOf`/ `anyOf`/ `oneOf`.
2. Add support for converting YAML.
3. Update online editor to add more examples to choose from.
2. Support validations on class parameters. Eg. `exclusiveMinimum`, `minItems`, `uniqueItems`.
3. Add support for converting YAML.
4. Update online editor to add more examples to choose from.

##### Building locally
1. Clone the repo <br />
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-schema-to-case-class",
"version": "1.1.8",
"version": "1.1.9",
"description": "A library that converts JSON Schema to Scala Case Classes",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
9 changes: 1 addition & 8 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import map from 'lodash/map';

import * as allTextCases from 'change-case';

// import { parse, scope } from 'jsonref';
// import axios from 'axios';
import $RefParser from '@apidevtools/json-schema-ref-parser/lib/index';
import { Config } from './config';
import { format } from './formatter';
Expand Down Expand Up @@ -81,11 +79,6 @@ const stripSchema = async (schema: any, config: IConfigResolved) : Promise<ICase
* @param schema
*/
const resolveRefs = async (schema: any): Promise<IResolveRefsResult> => {

// return await parse(schema, { scope: '', retriever: ( url => axios(url) ) })
// .then( result => { return { error: null, schema: result } } )
// .catch( err => { return { error: err, schema: null } } );

return $RefParser
.dereference(schema, { dereference: { circular: 'ignore' } })
.then(result => { return { error: null, schema: result } })
Expand Down Expand Up @@ -143,7 +136,7 @@ const stripSchemaObject = (schemaObject: any, currentDepth: number, entityTitle:
} else {
if (paramObject.type === 'object') {
paramType = config.defaultGenericType;
} else {
} else if (paramObject.type === 'array') {
paramType += `[${config.defaultGenericType}]`;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const formatParamName = (param: ICaseClassDefParams): string => {
* @param config
*/
const formatParamType = (param: ICaseClassDefParams, config: IConfigResolved): string => {
return (config.optionSetting === 'useOptions' && param.isRequired) || config.optionSetting === 'useOptionsForAll'
return (config.optionSetting === 'useOptions' && !param.isRequired) || config.optionSetting === 'useOptionsForAll'
? `Option[${param.paramType}]`
: param.paramType;
};
Expand Down
2 changes: 1 addition & 1 deletion tests/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Function convert()', () => {
expect(result).to.contain('case class PersonInfo');
expect(result).to.contain('case class BillingAddress');
expect(result).to.contain('case class ShippingAddress');
expect(result).to.contain('Option[');
expect(result).to.contain('zip: Option[Integer]');

});

Expand Down
24 changes: 24 additions & 0 deletions tests/strip-schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from 'lodash/get';
import find from 'lodash/find';
import { expect } from 'chai';

import { stripSchema } from '../src'
Expand Down Expand Up @@ -73,4 +74,27 @@ describe('Function stripSchema()', () => {

});

it('should parse parameter types as expected for maxDepth less than the total depth', async () => {

const config = {
maxDepth: 1,
optionSetting: 'useOptions',
classNameTextCase: 'pascalCase',
classParamsTextCase: 'snakeCase',
topLevelCaseClassName: 'PersonInfo',
defaultGenericType: 'Any',
parseRefs: true,
generateComments: false
};

const result = await stripSchema(nestedSchema, Config.resolve(config));

expect(result).to.be.an('object');
expect(result.entityName).to.eql(nestedSchema.title);
expect(get(find(result.parameters, { paramName: 'product_id' }), 'paramType')).to.eql('Integer');
expect(get(find(result.parameters, { paramName: 'tags' }), 'paramType')).to.eql('List[Any]');
expect(get(find(result.parameters, { paramName: 'dimensions' }), 'paramType')).to.eql('Any');

});

});
3 changes: 2 additions & 1 deletion tests/test-data/local-ref-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
"state": { "type": "string" },
"zip": { "type": "integer" }
},
"required": ["street_address", "city", "state"]
}
Expand Down

0 comments on commit b3c0773

Please sign in to comment.