Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 284d269

Browse files
Use JSON field names in required list when json_fieldnames is used (#101)
Currently, the required fields list will always contain only the protobuf names of fields, even if the json_fieldnames option is used. In this commit, the behavior is changed to instead use JSON names if the json_fieldnames option is used and proto names in all the other cases.
1 parent c8aad7b commit 284d269

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

internal/converter/testdata/json_fields.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const JSONFields = `{
55
"$ref": "#/definitions/JSONFields",
66
"definitions": {
77
"JSONFields": {
8+
"required": [
9+
"otherNumb"
10+
],
811
"properties": {
912
"name": {
1013
"type": "string"
@@ -23,6 +26,9 @@ const JSONFields = `{
2326
},
2427
"snakeNumb": {
2528
"type": "string"
29+
},
30+
"otherNumb": {
31+
"type": "integer"
2632
}
2733
},
2834
"additionalProperties": true,
@@ -31,6 +37,6 @@ const JSONFields = `{
3137
}
3238
}`
3339

34-
const JSONFieldsFail = `{"someThing": "onetwothree"}`
40+
const JSONFieldsFail = `{"someThing": "onetwothree", "other_numb": 123}`
3541

36-
const JSONFieldsPass = `{"someThing": 12345}`
42+
const JSONFieldsPass = `{"someThing": 12345, "otherNumb": 123}`

internal/converter/testdata/proto/JSONFields.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
syntax = "proto3";
22
package samples;
3+
import "options.proto";
34

45
message JSONFields {
56
string name = 1;
@@ -8,4 +9,5 @@ message JSONFields {
89
float some_thing = 4 [json_name="someThing"];
910
bool complete = 5;
1011
int64 snake_numb = 6;
12+
int32 other_numb = 7 [(protoc.gen.jsonschema.field_options).required = true];
1113
}

internal/converter/types.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,11 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msgDesc *d
525525
// "Required" fields are added to the list of required attributes in our schema:
526526
if fieldOptions.GetRequired() {
527527
c.logger.WithField("field_name", fieldDesc.GetName()).WithField("message_name", msgDesc.GetName()).Debug("Marking required field")
528-
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetName())
528+
if c.Flags.UseJSONFieldnamesOnly {
529+
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetJsonName())
530+
} else {
531+
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetName())
532+
}
529533
}
530534
}
531535
}
@@ -564,7 +568,11 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msgDesc *d
564568

565569
// Look for required fields by the proto2 "required" flag:
566570
if fieldDesc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REQUIRED && fieldDesc.OneofIndex == nil {
567-
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetName())
571+
if c.Flags.UseJSONFieldnamesOnly {
572+
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetJsonName())
573+
} else {
574+
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetName())
575+
}
568576
}
569577
}
570578

0 commit comments

Comments
 (0)