Skip to content

Commit

Permalink
chore: tidy defaultValue and introduce test for default value on refe…
Browse files Browse the repository at this point in the history
…rence
  • Loading branch information
karlvr committed Sep 13, 2024
1 parent b34784a commit 92c2e7b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
20 changes: 20 additions & 0 deletions packages/core/src/__tests__/references.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ test('description on $ref', async() => {
expect(valueProperty?.description).toEqual('Description on $ref')
expect(valueProperty?.schema.description).toEqual('MyObject description')
})

test('default value on $ref', async() => {
const result = await createTestDocument('references/default-value.yml')
const op = result.groups[0].operations[0]
expect(op).toBeDefined()

const responseSchema = op.defaultResponse?.defaultContent?.schema as CodegenObjectSchema
expect(responseSchema).toBeDefined()

const properties = responseSchema.properties
expect(properties).toBeTruthy()

const valueProperty = idx.get(properties!, 'value')
expect(valueProperty).toBeTruthy()

const defaultValue = valueProperty?.defaultValue
expect(defaultValue).not.toBeNull()
expect(typeof defaultValue?.value).toBe('object')
expect((defaultValue?.value as Record<string, string>).prop1).toEqual('Test 1')
})
31 changes: 31 additions & 0 deletions packages/core/src/__tests__/references/default-value.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
openapi: 3.0.3
info:
title: Default value on reference
version: '1.0.1'
paths:
/test1:
get:
responses:
200:
description: OK
content:
application/json:
schema:
type: object
properties:
value:
$ref: '#/components/schemas/MyObject'
default:
prop1: Test 1
prop2: Test 2
components:
schemas:
MyObject:
type: object
description: MyObject description
properties:
prop1:
type: string
description: Prop1 description
prop2:
type: string
9 changes: 4 additions & 5 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ export interface CodegenSchemaUsage<T extends CodegenSchema = CodegenSchema> ext
schema: T
required: boolean
examples: CodegenExamples | null

/**
* The default value of the usage from the spec.
*/
defaultValue: CodegenValue | null
}

Expand All @@ -525,11 +529,6 @@ export interface CodegenProperty extends CodegenSchemaUsage {
*/
initialValue: CodegenValue | null

/**
* The default value of the property from the spec.
*/
defaultValue: CodegenValue | null

/** The discriminators that this property is used by, if this property is used by a discriminator.
* We usually remove properties used for discriminators, but sometimes they are left for
* interface comformance.
Expand Down

0 comments on commit 92c2e7b

Please sign in to comment.