Skip to content

Commit

Permalink
fix(cli): fern definition respects extra-properties when validating e…
Browse files Browse the repository at this point in the history
…xamples (#5520)
  • Loading branch information
dsinghvi authored Jan 3, 2025
1 parent 6dbbd8e commit 557e276
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 3 deletions.
41 changes: 41 additions & 0 deletions fern/pages/changelogs/cli/2025-01-03.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## 0.46.18
**`(fix):`** If an object or request is annotated with `extra-properties: true` then the user can provide an example that includes
extra properties that are no longer in the schema.

For example, check out this fern definition

```yml service.yml
types:
Item:
extra-properties: true
properties:
id: string


service:
auth: false
base-path: ""
endpoints:
create:
method: POST
path: /item
request:
name: CreateItemRequest
body:
extra-properties: true
properties:
id: string
response:
type: Item
examples:
- name: "Item"
request:
id: "123"
foo: "bar" # extra property in the example
response:
body:
id: "123"
foo: "bar" # extra property in the example
```
44 changes: 44 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
- changelogEntry:
- summary: |
If an object or request is annotated with `extra-properties: true` then the user can provide an example that includes
extra properties that are no longer in the schema.
For example, check out this fern definition
```yml service.yml
types:
Item:
extra-properties: true
properties:
id: string
service:
auth: false
base-path: ""
endpoints:
create:
method: POST
path: /item
request:
name: CreateItemRequest
body:
extra-properties: true
properties:
id: string
response:
type: Item
examples:
- name: "Item"
request:
id: "123"
foo: "bar" # extra property in the example
response:
body:
id: "123"
foo: "bar" # extra property in the example
```
type: fix
irVersion: 53
version: 0.46.18

- changelogEntry:
- summary: |
Support parsing string values for boolean defaults in OpenAPI schemas.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: extra-properties
auth: bearer
default-environment: Production
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
types:
Item:
extra-properties: true
properties:
id: string


service:
auth: false
base-path: ""
endpoints:
create:
method: POST
path: /item
request:
name: CreateItemRequest
body:
extra-properties: true
properties:
id: string
response:
type: Item
examples:
- name: "Item"
request:
id: "123"
foo: "bar"
response:
body:
id: "123"
foo: "bar"

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,19 @@ describe("valid-example-endpoint-call", () => {

expect(violations).toEqual(expectedViolations);
});

it("extra-properties", async () => {
const violations = await getViolationsForRule({
rule: ValidExampleEndpointCallRule,
absolutePathToWorkspace: join(
AbsoluteFilePath.of(__dirname),
RelativeFilePath.of("fixtures"),
RelativeFilePath.of("extra-properties")
)
});

const expectedViolations: ValidationViolation[] = [];

expect(violations).toEqual(expectedViolations);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExampleResolver, ExampleValidators, FernFileContext, TypeResolver } from "@fern-api/ir-generator";
import { FernWorkspace } from "@fern-api/api-workspace-commons";
import { isInlineRequestBody, RawSchemas } from "@fern-api/fern-definition-schema";
import { ExampleResolver, ExampleValidators, FernFileContext, TypeResolver } from "@fern-api/ir-generator";
import { RuleViolation } from "../../Rule";

export function validateRequest({
Expand Down Expand Up @@ -35,6 +35,7 @@ export function validateRequest({
typeName: undefined,
typeNameForBreadcrumb: "<Inlined Request>",
rawObject: {
"extra-properties": body["extra-properties"],
extends: body.extends,
properties: body.properties ?? {}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isPlainObject } from "@fern-api/core-utils";
import { FernWorkspace, getDefinitionFile } from "@fern-api/api-workspace-commons";
import { isPlainObject } from "@fern-api/core-utils";
import { RawSchemas } from "@fern-api/fern-definition-schema";
import { keyBy } from "lodash-es";
import { constructFernFileContext, FernFileContext } from "../FernFileContext";
Expand Down Expand Up @@ -80,7 +80,9 @@ export function validateObjectExample({
// check properties on example
for (const [exampleKey, exampleValue] of Object.entries(example)) {
const propertyWithPath = allPropertiesByWireKey[exampleKey];
if (propertyWithPath == null) {
if (rawObject["extra-properties"]) {
continue;
} else if (propertyWithPath == null) {
violations.push({
message: `Unexpected property "${exampleKey}"`
});
Expand Down

0 comments on commit 557e276

Please sign in to comment.