Skip to content

Commit

Permalink
remove leading comma in JTD serialisation result, fixes #2001, fixes #…
Browse files Browse the repository at this point in the history
…2171, fixes #2181 (#2190)

* fixes #2001

fixes #2001

* reduce diff

* prettier

* JTD only optional properties test

* Test case

* simplify, avoid run time code changes when possible

* fix test

* style

* style

* prettier

Co-authored-by: Anton Piliugin <87300344+piliugin-anton@users.noreply.github.com>
Co-authored-by: Anton Piliugin <anton.piliugin@icloud.com>
  • Loading branch information
3 people committed Jan 2, 2023
1 parent 35034b6 commit 5c72864
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/compile/jtd/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function serializeValues(cxt: SerializeCxt): void {
gen.add(N.json, str`}`)
}

function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first: Name): void {
function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first?: Name): void {
const {gen, data} = cxt
addComma(cxt, first)
serializeString({...cxt, data: key})
Expand Down Expand Up @@ -156,20 +156,24 @@ function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): v
const optProps = keys(optionalProperties)
const allProps = allProperties(props.concat(optProps))
let first = !discriminator
let firstProp: Name | undefined

for (const key of props) {
if (first) first = false
else gen.add(N.json, str`,`)
serializeProperty(key, properties[key], keyValue(key))
}
if (first) firstProp = gen.let("first", true)
for (const key of optProps) {
const value = keyValue(key)
gen.if(and(_`${value} !== undefined`, isOwnProperty(gen, data, key)), () =>
gen.if(and(_`${value} !== undefined`, isOwnProperty(gen, data, key)), () => {
addComma(cxt, firstProp)
serializeProperty(key, optionalProperties[key], value)
)
})
}
if (schema.additionalProperties) {
gen.forIn("key", data, (key) =>
gen.if(isAdditional(key, allProps), () =>
serializeKeyValue(cxt, key, {}, gen.let("first", first))
)
gen.if(isAdditional(key, allProps), () => serializeKeyValue(cxt, key, {}, firstProp))
)
}

Expand All @@ -190,8 +194,6 @@ function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): v
}

function serializeProperty(key: string, propSchema: SchemaObject, value: Name): void {
if (first) first = false
else gen.add(N.json, str`,`)
gen.add(N.json, str`${JSON.stringify(key)}:`)
serializeCode({...cxt, schema: propSchema, data: value})
}
Expand Down Expand Up @@ -251,10 +253,14 @@ function serializeEmpty({gen, data}: SerializeCxt): void {
gen.add(N.json, _`JSON.stringify(${data})`)
}

function addComma({gen}: SerializeCxt, first: Name): void {
gen.if(
first,
() => gen.assign(first, false),
() => gen.add(N.json, str`,`)
)
function addComma({gen}: SerializeCxt, first?: Name): void {
if (first) {
gen.if(
first,
() => gen.assign(first, false),
() => gen.add(N.json, str`,`)
)
} else {
gen.add(N.json, str`,`)
}
}
24 changes: 24 additions & 0 deletions spec/issues/2001_jtd_only_optional_properties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import _Ajv from "../ajv_jtd"
import * as assert from "assert"

describe("schema with optional/additional properties only", () => {
const ajv = new _Ajv()

it("should correctly serialize optional properties", () => {
const schema = {
optionalProperties: {
prop0: {type: "uint16"},
prop1: {type: "uint16"},
prop2: {type: "uint16"},
},
additionalProperties: true,
}
const serialize = ajv.compileSerializer(schema)
const test = (data, json) => assert.strictEqual(serialize(data), json)
test({prop0: 0, prop1: 1, prop2: 2}, '{"prop0":0,"prop1":1,"prop2":2}')
test({prop1: 1, prop2: 2}, '{"prop1":1,"prop2":2}')
test({prop0: 0, prop1: 1, prop2: 2, foo: "bar"}, '{"prop0":0,"prop1":1,"prop2":2,"foo":"bar"}')
test({prop1: 1, prop2: 2, foo: "bar"}, '{"prop1":1,"prop2":2,"foo":"bar"}')
test({foo: "bar"}, '{"foo":"bar"}')
})
})

0 comments on commit 5c72864

Please sign in to comment.