microsoft / microsoft/typespec

[Bug]: json-schema and http-server-js - Implicit relation between modules produces the repetition of examples on json-schema

Open
#7,837 1 comment 1 reaction 1 assignee Claimed by @witemple-msft View on GitHub
bug compiler:core triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

I've provided an example of the model `Employee` using the `@example` decorator. The generated json-schema repeats the example the same number of times I've used the model on route operations.

There's an implicit condition between `@typespec/http-server-js` and `@typespec/json-schema` that causes this bug, because if the `emit` order is changed, the bug disappears.

### Reproduction

~~~yaml
# tspconfig.yaml
emit:
# Changing the order "fixes" the bug
- "@typespec/http-server-js"
- "@typespec/json-schema"
options:
"@typespec/openapi3":
emitter-output-dir: "{output-dir}/schema"
openapi-versions:
- 3.1.0
"@typespec/json-schema":
emitter-output-dir: "{output-dir}/json-schema"
file-type: "yaml"
bundleId: schema.yml
~~~

~~~typespec
// main.tsp
import "@typespec/http";
import "@typespec/json-schema";

using Http;

@JsonSchema.jsonSchema
@service()
namespace EmployeeManagement;

@example(#{
id: "asdfasdf-asdf-asdf-asdfasdfasdf",
name: "asdf",
})
model Employee {
id: string;
name: string;
}

@route("employees")
interface EmployeeAPI {
// Comment a operation and the number of repetitions decrease
@get employee(@path id: string): Employee;
@get allEmployees(): Employee[];
}
~~~

The output is:

~~~yaml
$schema: https://json-schema.org/draft/2020-12/schema
$id: schema.yml
$defs:
Employee:
$schema: https://json-schema.org/draft/2020-12/schema
$id: Employee.yaml
type: object
properties:
id:
type: string
name:
type: string
required:
- id
- name
examples:
- id: asdfasdf-asdf-asdf-asdfasdfasdf
name: asdf
- id: asdfasdf-asdf-asdf-asdfasdfasdf
name: asdf
- id: asdfasdf-asdf-asdf-asdfasdfasdf
name: asdf
~~~

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/Microsoft/typespec/discussions).
- [x] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.