Skip to content

Commit

Permalink
double support (#328)
Browse files Browse the repository at this point in the history
Added support for `Number` and `Double` data types
  • Loading branch information
lfportal authored Jul 11, 2019
1 parent eaaf19c commit 60d355c
Show file tree
Hide file tree
Showing 17 changed files with 508 additions and 437 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ exports[`OpenAPI 2 generator produces valid code: json 1`] = `
\\"type\\": \\"string\\"
},
\\"age\\": {
\\"type\\": \\"number\\"
\\"type\\": \\"number\\",
\\"format\\": \\"float\\"
},
\\"email\\": {
\\"$ref\\": \\"#/definitions/Email\\"
Expand Down Expand Up @@ -387,6 +388,7 @@ definitions:
type: string
age:
type: number
format: float
email:
$ref: '#/definitions/Email'
address:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ exports[`OpenAPI 3 generator produces valid code: json 1`] = `
\\"type\\": \\"string\\"
},
\\"age\\": {
\\"type\\": \\"number\\"
\\"type\\": \\"number\\",
\\"format\\": \\"float\\"
},
\\"email\\": {
\\"$ref\\": \\"#/components/schemas/Email\\"
Expand Down Expand Up @@ -454,6 +455,7 @@ components:
type: string
age:
type: number
format: float
email:
$ref: '#/components/schemas/Email'
address:
Expand Down
1 change: 1 addition & 0 deletions lib/src/generators/contract/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function jsonTypeSchema(type: DataType): JsonSchemaType {
const: type.value
};
case TypeKind.FLOAT:
case TypeKind.DOUBLE:
return {
type: "number"
};
Expand Down
273 changes: 138 additions & 135 deletions lib/src/generators/contract/openapi2-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,131 +26,132 @@ describe("OpenAPI 2 generator", () => {

test("boolean", () => {
expect(openApi2TypeSchema(BOOLEAN)).toMatchInlineSnapshot(`
Object {
"type": "boolean",
}
`);
Object {
"type": "boolean",
}
`);
});

test("boolean literal", () => {
expect(openApi2TypeSchema(booleanLiteral(true))).toMatchInlineSnapshot(`
Object {
"enum": Array [
true,
],
"type": "boolean",
}
`);
Object {
"enum": Array [
true,
],
"type": "boolean",
}
`);
expect(openApi2TypeSchema(booleanLiteral(false))).toMatchInlineSnapshot(`
Object {
"enum": Array [
false,
],
"type": "boolean",
}
`);
Object {
"enum": Array [
false,
],
"type": "boolean",
}
`);
});

test("string", () => {
expect(openApi2TypeSchema(STRING)).toMatchInlineSnapshot(`
Object {
"type": "string",
}
`);
Object {
"type": "string",
}
`);
});

test("string literal", () => {
expect(openApi2TypeSchema(stringLiteral("some literal")))
.toMatchInlineSnapshot(`
Object {
"enum": Array [
"some literal",
],
"type": "string",
}
`);
Object {
"enum": Array [
"some literal",
],
"type": "string",
}
`);
});

test("float", () => {
expect(openApi2TypeSchema(FLOAT)).toMatchInlineSnapshot(`
Object {
"type": "number",
}
`);
Object {
"format": "float",
"type": "number",
}
`);
});

test("number literal", () => {
expect(openApi2TypeSchema(numberLiteral(1.5))).toMatchInlineSnapshot(`
Object {
"enum": Array [
1.5,
],
"type": "number",
}
`);
Object {
"enum": Array [
1.5,
],
"type": "number",
}
`);
expect(openApi2TypeSchema(numberLiteral(-23.1))).toMatchInlineSnapshot(`
Object {
"enum": Array [
-23.1,
],
"type": "number",
}
`);
Object {
"enum": Array [
-23.1,
],
"type": "number",
}
`);
});

test("int32", () => {
expect(openApi2TypeSchema(INT32)).toMatchInlineSnapshot(`
Object {
"format": "int32",
"type": "integer",
}
`);
Object {
"format": "int32",
"type": "integer",
}
`);
});

test("int64", () => {
expect(openApi2TypeSchema(INT64)).toMatchInlineSnapshot(`
Object {
"format": "int64",
"type": "integer",
}
`);
Object {
"format": "int64",
"type": "integer",
}
`);
});

test("integer literal", () => {
expect(openApi2TypeSchema(numberLiteral(0))).toMatchInlineSnapshot(`
Object {
"enum": Array [
0,
],
"type": "integer",
}
`);
Object {
"enum": Array [
0,
],
"type": "integer",
}
`);
expect(openApi2TypeSchema(numberLiteral(122))).toMatchInlineSnapshot(`
Object {
"enum": Array [
122,
],
"type": "integer",
}
`);
Object {
"enum": Array [
122,
],
"type": "integer",
}
`);
expect(openApi2TypeSchema(numberLiteral(-1000))).toMatchInlineSnapshot(`
Object {
"enum": Array [
-1000,
],
"type": "integer",
}
`);
Object {
"enum": Array [
-1000,
],
"type": "integer",
}
`);
});

test("object", () => {
expect(openApi2TypeSchema(objectType([]))).toMatchInlineSnapshot(`
Object {
"properties": Object {},
"required": Array [],
"type": "object",
}
`);
Object {
"properties": Object {},
"required": Array [],
"type": "object",
}
`);
expect(
openApi2TypeSchema(
objectType([
Expand All @@ -162,18 +163,19 @@ Object {
])
)
).toMatchInlineSnapshot(`
Object {
"properties": Object {
"singleField": Object {
"type": "number",
},
},
"required": Array [
"singleField",
],
"type": "object",
}
`);
Object {
"properties": Object {
"singleField": Object {
"format": "float",
"type": "number",
},
},
"required": Array [
"singleField",
],
"type": "object",
}
`);
expect(
openApi2TypeSchema(
objectType([
Expand All @@ -195,51 +197,52 @@ Object {
])
)
).toMatchInlineSnapshot(`
Object {
"properties": Object {
"field1": Object {
"type": "number",
},
"field2": Object {
"type": "string",
},
"field3": Object {
"type": "boolean",
},
},
"required": Array [
"field1",
"field2",
],
"type": "object",
}
`);
Object {
"properties": Object {
"field1": Object {
"format": "float",
"type": "number",
},
"field2": Object {
"type": "string",
},
"field3": Object {
"type": "boolean",
},
},
"required": Array [
"field1",
"field2",
],
"type": "object",
}
`);
});

test("array", () => {
expect(openApi2TypeSchema(arrayType(STRING))).toMatchInlineSnapshot(`
Object {
"items": Object {
"type": "string",
},
"type": "array",
}
`);
Object {
"items": Object {
"type": "string",
},
"type": "array",
}
`);
});

test("union", () => {
expect(openApi2TypeSchema(unionType([STRING]))).toMatchInlineSnapshot(`
Object {
"type": "string",
}
`);
Object {
"type": "string",
}
`);
expect(openApi2TypeSchema(unionType([STRING, NULL])))
.toMatchInlineSnapshot(`
Object {
"type": "string",
"x-nullable": true,
}
`);
Object {
"type": "string",
"x-nullable": true,
}
`);
expect(() =>
openApi2TypeSchema(unionType([STRING, FLOAT, BOOLEAN]))
).toThrowError("Unions are not supported in OpenAPI 2");
Expand All @@ -251,10 +254,10 @@ Object {
referenceType("OtherType", "location", TypeKind.STRING)
)
).toMatchInlineSnapshot(`
Object {
"$ref": "#/definitions/OtherType",
}
`);
Object {
"$ref": "#/definitions/OtherType",
}
`);
});
});
});
Loading

0 comments on commit 60d355c

Please sign in to comment.