Skip to content

Commit

Permalink
fix(cli): Prefer application/json Content-Type (#5743)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Jan 24, 2025
1 parent 83641f3 commit 18ff859
Show file tree
Hide file tree
Showing 13 changed files with 1,247 additions and 990 deletions.
5 changes: 5 additions & 0 deletions fern/pages/changelogs/cli/2025-01-24.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 0.51.3
**`(fix):`** The OpenAPI parser now prefers the JSON Content-Type variant over
others (e.g. application/x-www-form-urlencoded).


4 changes: 4 additions & 0 deletions fern/pages/changelogs/openapi/2025-01-24.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 0.1.7
**`(fix):`** The generator now correctly handles different success response codes.


Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,6 @@ export function convertRequest({

const jsonMediaObject = getApplicationJsonSchemaMediaObject(resolvedRequestBody.content, context);

// convert as application/x-www-form-urlencoded
if (urlEncodedRequest != null && urlEncodedRequest.schema != null) {
const convertedUrlEncodedSchema = convertSchema(
urlEncodedRequest.schema,
false,
context,
requestBreadcrumbs,
source,
namespace
);
return RequestWithExample.json({
schema: convertedUrlEncodedSchema,
description: resolvedRequestBody.description,
contentType: urlEncodedRequest.contentType,
source,
fullExamples: urlEncodedRequest.examples,
additionalProperties: false
});
}

// convert as application/octet-stream
if (isOctetStreamRequest(resolvedRequestBody)) {
return RequestWithExample.octetStream({
Expand Down Expand Up @@ -238,29 +218,49 @@ export function convertRequest({
});
}

// otherwise, convert as json request.
if (jsonMediaObject == null) {
return undefined;
// convert as application/json
if (jsonMediaObject != null) {
const requestSchema = convertSchema(
jsonMediaObject.schema,
false,
context,
requestBreadcrumbs,
source,
namespace,
true
);
return RequestWithExample.json({
description: undefined,
schema: requestSchema,
contentType: jsonMediaObject.contentType,
fullExamples: jsonMediaObject.examples,
additionalProperties:
!isReferenceObject(jsonMediaObject.schema) &&
isAdditionalPropertiesAny(jsonMediaObject.schema.additionalProperties),
source
});
}

// convert as application/x-www-form-urlencoded
if (urlEncodedRequest != null && urlEncodedRequest.schema != null) {
const convertedUrlEncodedSchema = convertSchema(
urlEncodedRequest.schema,
false,
context,
requestBreadcrumbs,
source,
namespace
);
return RequestWithExample.json({
schema: convertedUrlEncodedSchema,
description: resolvedRequestBody.description,
contentType: urlEncodedRequest.contentType,
source,
fullExamples: urlEncodedRequest.examples,
additionalProperties: false
});
}
const requestSchema = convertSchema(
jsonMediaObject.schema,
false,
context,
requestBreadcrumbs,
source,
namespace,
true
);
return RequestWithExample.json({
description: undefined,
schema: requestSchema,
contentType: jsonMediaObject.contentType,
fullExamples: jsonMediaObject.examples,
additionalProperties:
!isReferenceObject(jsonMediaObject.schema) &&
isAdditionalPropertiesAny(jsonMediaObject.schema.additionalProperties),
source
});
return undefined;
}

interface ResolvedSchema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,54 @@
"openapi": "../openapi.yml",
},
},
"testWithDualContentTypeRequestTypes": {
"auth": false,
"display-name": "Test with dual Content-Type request types",
"docs": "Test request with dual content types.
",
"examples": [
{
"path-parameters": {
"corpus_key": "corpus_key",
},
"request": {},
"response": {
"body": "string",
},
},
],
"method": "POST",
"pagination": undefined,
"path": "/example",
"path-parameters": {
"corpus_key": {
"docs": "The unique key",
"type": "string",
},
},
"request": {
"body": {
"properties": {
"metadata": {
"docs": "Arbitrary object that will be attached as document metadata to the extracted document.",
"type": "optional<map<string, unknown>>",
},
},
},
"content-type": "application/json",
"headers": undefined,
"name": "PostExampleRequest",
"path-parameters": undefined,
"query-parameters": undefined,
},
"response": {
"docs": "The sample response",
"type": "string",
},
"source": {
"openapi": "../openapi.yml",
},
},
},
"source": {
"openapi": "../openapi.yml",
Expand Down Expand Up @@ -119,6 +167,38 @@
request: {}
response:
body: string
testWithDualContentTypeRequestTypes:
path: /example
method: POST
auth: false
docs: |
Test request with dual content types.
source:
openapi: ../openapi.yml
path-parameters:
corpus_key:
type: string
docs: The unique key
display-name: Test with dual Content-Type request types
request:
name: PostExampleRequest
body:
properties:
metadata:
type: optional<map<string, unknown>>
docs: >-
Arbitrary object that will be attached as document metadata to
the extracted document.
content-type: application/json
response:
docs: The sample response
type: string
examples:
- path-parameters:
corpus_key: corpus_key
request: {}
response:
body: string
source:
openapi: ../openapi.yml
",
Expand Down
Loading

0 comments on commit 18ff859

Please sign in to comment.