@@ -85,54 +85,73 @@ module.exports = class Operation {
8585 const rawResponse = rawResponses [ responseCode ]
8686 const httpStatusCode = responseCode
8787 const httpStatusMessage = httpStatusCodes . getMessage ( Number ( responseCode ) )
88+ const responseDescription = rawResponse . description
8889
8990 const cleanResponses = [ ]
9091
91- // responses can have zero, one, or multiple examples
92- const rawExample = get ( rawResponse , 'content.application/json.example' )
93- const rawExamples = get ( rawResponse , 'content.application/json.examples' )
94-
95- // first handle responses with multiple examples
96- if ( rawExamples ) {
97- for ( const rawExampleKey of Object . keys ( rawExamples ) ) {
98- const rawExample = rawExamples [ rawExampleKey ]
99- const cleanResponse = { }
100-
101- cleanResponse . httpStatusCode = httpStatusCode
102- cleanResponse . httpStatusMessage = httpStatusMessage
92+ /* Responses can have zero, one, or multiple examples. The `examples`
93+ * property often only contains one example object. Both the `example`
94+ * and `examples` properties can be used in the OpenAPI but `example`
95+ * doesn't work with `$ref`.
96+ * This works:
97+ * schema:
98+ * '$ref': '../../components/schemas/foo.yaml'
99+ * example:
100+ * id: 10
101+ * description: This is a summary
102+ * foo: bar
103+ *
104+ * This doesn't
105+ * schema:
106+ * '$ref': '../../components/schemas/foo.yaml'
107+ * example:
108+ * '$ref': '../../components/examples/bar.yaml'
109+ */
110+ const examplesProperty = get ( rawResponse , 'content.application/json.examples' )
111+ const exampleProperty = get ( rawResponse , 'content.application/json.example' )
112+
113+ // Return early if the response doesn't have an example payload
114+ if ( ! exampleProperty && ! examplesProperty ) {
115+ return [ {
116+ httpStatusCode,
117+ httpStatusMessage,
118+ description : responseDescription
119+ } ]
120+ }
103121
104- // a handful of examples don't have summary properties with a description,
105- // so we can sentence case the property name as a fallback
106- cleanResponse . description = rawExample . summary || sentenceCase ( rawExampleKey )
122+ // Use the same format for `example` as `examples` property so that all
123+ // examples can be handled the same way.
124+ const normalizedExampleProperty = {
125+ default : {
126+ value : exampleProperty
127+ }
128+ }
107129
108- const payloadMarkdown = createCodeBlock ( rawExample . value , 'json' )
109- cleanResponse . payload = await renderContent ( payloadMarkdown )
130+ const rawExamples = examplesProperty || normalizedExampleProperty
131+ const rawExampleKeys = Object . keys ( rawExamples )
110132
111- cleanResponses . push ( cleanResponse )
133+ for ( const exampleKey of rawExampleKeys ) {
134+ const exampleValue = rawExamples [ exampleKey ] . value
135+ const exampleSummary = rawExamples [ exampleKey ] . summary
136+ const cleanResponse = {
137+ httpStatusCode,
138+ httpStatusMessage
112139 }
113- } else { // then handle responses with either one or zero examples
114- const cleanResponse = { }
115140
116- cleanResponse . httpStatusCode = responseCode
117- cleanResponse . httpStatusMessage = httpStatusCodes . getMessage ( Number ( responseCode ) )
118- cleanResponse . description = sentenceCase ( rawResponse . description )
141+ // If there is only one example, use the response description
142+ // property. For cases with more than one example, some don't have
143+ // summary properties with a description, so we can sentence case
144+ // the property name as a fallback
145+ cleanResponse . description = rawExampleKeys . length === 1
146+ ? exampleSummary || responseDescription
147+ : exampleSummary || sentenceCase ( exampleKey )
119148
120- if ( rawExample ) {
121- const payloadMarkdown = createCodeBlock ( rawExample , 'json' )
122- cleanResponse . payload = await renderContent ( payloadMarkdown )
123- }
149+ const payloadMarkdown = createCodeBlock ( exampleValue , 'json' )
150+ cleanResponse . payload = await renderContent ( payloadMarkdown )
124151
125152 cleanResponses . push ( cleanResponse )
126153 }
127-
128- // tidy up descriptions
129- return cleanResponses . map ( response => {
130- response . description = response . description
131- . replace ( 'Example of' , 'Response for' )
132- . replace ( 'Empty response' , 'Default Response' )
133- . replace ( / ^ D e f a u l t $ / , 'Default response' )
134- return response
135- } )
154+ return cleanResponses
136155 } ) )
137156
138157 // flatten child arrays
0 commit comments