console: fix issues when generating actions from open API

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7414
GitOrigin-RevId: 381a8e0eaa658cb93bd2947fd167129469d715f8
This commit is contained in:
Daniele Cammareri 2023-01-09 10:36:34 +01:00 committed by hasura-bot
parent 4baf9542c1
commit 0ec4de1733
3 changed files with 20 additions and 9 deletions

View File

@ -393,7 +393,7 @@ const AddAction: React.FC<AddActionProps> = ({
transformDispatch(setRequestSampleInput(sampleInput));
requestMethodOnChange(method);
requestUrlTransformOnChange(true);
requestUrlOnChange(path.replace(/\{([^}]+)\}/g, '{{$body.input.$1}}'));
requestUrlOnChange(path);
requestQueryParamsOnChange(
queryParams.map(name => ({
name,

View File

@ -115,15 +115,15 @@ export const OasGeneratorForm = (props: {
localParsedOas = YAML.load(oas) as Oas3;
} catch (e2) {
setError('oas', {
message: 'Invalid spec',
message: 'Invalid JSON or YAML format',
});
}
}
}
try {
if (localParsedOas) {
if (!url && localParsedOas.servers?.[0].url) {
setValue('url', localParsedOas.servers?.[0].url);
if (!url && localParsedOas.servers?.[0]?.url) {
setValue('url', localParsedOas.servers?.[0]?.url);
}
const ops = await getOperations(localParsedOas);
setOperations(ops);
@ -148,6 +148,7 @@ export const OasGeneratorForm = (props: {
);
}
} catch (e) {
console.error(e);
setError('oas', {
message: `Invalid spec: ${(e as Error).message}`,
});

View File

@ -339,8 +339,8 @@ export const translateAction = (
.replace(/"""[^]*?"""/g, '')
.replace(/type Query {[^]*?}/g, '')
.replace(/type Mutation {[^]*?}/g, '')
.replace('type Query', '')
.replace('type Mutation', '')
.replace(/type Query\s+/, '')
.replace(/type Mutation\s+/, '')
);
let sampleInput = JSON.parse(
@ -385,7 +385,12 @@ export const translateAction = (
description: operation?.operation?.description ?? '',
method: parseRequestMethod(operation?.method),
baseUrl: graphqlSchema.data?.oass?.[0]?.servers?.[0]?.url ?? '',
path: operation.path,
// replace the regex /\{([^}]+)\}/g occurrences with {{$body.input.$1}} with the first letter lowercased
// e.g. /user/{UserId} -> /user/{{$body.input.userId}}
path: operation.path.replace(
/\{([^}]+)\}/g,
(_, p1) => `{{$body.input.${p1[0].toLowerCase()}${p1.slice(1)}}}`
),
requestTransforms: createRequestTransform(operation) ?? '',
responseTransforms: createResponseTransform(operation) ?? '',
sampleInput: JSON.stringify(sampleInput, null, 2),
@ -400,14 +405,19 @@ const applyWorkarounds = (properties: (SchemaObject | ReferenceObject)[]) => {
if (!('$ref' in property)) {
// fix boolean enum issue
if (property.type === 'boolean') {
console.log(property);
delete property.enum;
}
// fix empty type issue
if (
property.type === 'string' &&
(property.enum || []).some(v => v === 'true' || v === 'false')
) {
delete property.enum;
}
if (
property.type === 'object' &&
JSON.stringify(property.properties) === '{}'
) {
// fix empty type issue
property.type = 'string';
delete property.properties;
}